Skip to content

Commit

Permalink
Merge pull request #123 from traP-jp/fix/search-sort
Browse files Browse the repository at this point in the history
search sort by date
  • Loading branch information
kavos113 authored Sep 6, 2024
2 parents 96bb5fd + 57e2139 commit 52aa015
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 187 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ build/
UNKNOWN*/
!requirements.txt
*.bleve
sql/
sql/
!stoplist_ja.txt
4 changes: 4 additions & 0 deletions docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/searchRequest'
description: sortはcreatedAt_oldest, createdAt_newest
responses:
'200':
description: OK
Expand Down Expand Up @@ -660,6 +661,9 @@ components:
from:
type: integer
example: 0
sort:
type: string
example: createdAt_oldest
lectureWithoutId:
type: object
properties:
Expand Down
54 changes: 0 additions & 54 deletions src/go.mod

This file was deleted.

117 changes: 0 additions & 117 deletions src/go.sum

This file was deleted.

2 changes: 1 addition & 1 deletion src/handler/wikiHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (h *Handler) SearchHandler(c echo.Context) error {
// query検索
var searchResults_Query []int
if request.Query != "" {
searchResults_Query = search.Search(request.Query, request.ResultCount, request.From)
searchResults_Query = search.Search(request.Query, request.ResultCount, request.From, request.Sort)
if len(searchResults_Query) == 0 {
return echo.NewHTTPError(http.StatusNotFound, "No results were found matching that query.")
}
Expand Down
1 change: 1 addition & 0 deletions src/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ type WikiSearchBody struct {
Tags []string `json:"tags"`
From int `json:"from"`
ResultCount int `json:"resultCount"`
Sort string `json:"sort"`
}

type Tag_Post struct {
Expand Down
3 changes: 2 additions & 1 deletion src/scraper/newMessages.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func (s *Scraper) addMessageIndex(wikiId int) {
Title: wiki.Name,
OwnerTraqID: wiki.OwnerTraqID,
MessageContent: wiki.Content,
CreatedAt: wiki.CreatedAt,
},
}

Expand Down Expand Up @@ -251,7 +252,7 @@ func (s *Scraper) removeMentionSingle(wikiId int) {

for _, m := range messages {
text := ProcessMention(m.MessageContent)
_, err = s.db.Exec("UPDATE messages SET message_content = ? WHERE id = ?", text, m.ID)
_, err = s.db.Exec("UPDATE messages SET content = ? WHERE id = ?", text, m.ID)
if err != nil {
log.Println("failed to update message")
log.Println(err)
Expand Down
1 change: 1 addition & 0 deletions src/scraper/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (s *Scraper) SetIndexing() {
Title: wiki.Name,
OwnerTraqID: wiki.OwnerTraqID,
MessageContent: ProcessLink(removeNewLine(removeCodeBlock(removeTeX(wiki.Content)))),
CreatedAt: wiki.CreatedAt,
})
}

Expand Down
7 changes: 6 additions & 1 deletion src/search/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"strconv"
"time"
)

type IndexData struct {
Expand All @@ -14,6 +15,7 @@ type IndexData struct {
Title string
OwnerTraqID string
MessageContent string
CreatedAt time.Time
}

func Indexing(data []IndexData) {
Expand Down Expand Up @@ -72,6 +74,9 @@ func Indexing(data []IndexData) {
return
}

res := Search("windows", 20, 0)
res := Search("windows", 20, 0, "createdAt_oldest")
log.Println(res)

res = Search("windows", 20, 0, "createdAt_newest")
log.Println(res)
}
15 changes: 13 additions & 2 deletions src/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"strconv"
)

// return array of ID
// Search return array of ID
// return empty array if no result or error
func Search(query string, limit int, offset int) []int {
// sort: "createdAt_oldest", "createdAt_newest", "none"
func Search(query string, limit int, offset int, sort string) []int {
log.Println("[Info from search] Searching by query: ", query)
if _, err := os.Stat("index.bleve"); err != nil {
log.Println("[Error from search] Index file does not exist. Please create index file first.")
Expand All @@ -32,6 +33,16 @@ func Search(query string, limit int, offset int) []int {
bleveQuery := bleve.NewMatchQuery(query)
bleveQuery.SetField("MessageContent")
search := bleve.NewSearchRequestOptions(bleveQuery, limit, offset, false)
switch sort {
case "createdAt_oldest":
search.SortBy([]string{"CreatedAt", "-_score"})
break
case "createdAt_newest":
search.SortBy([]string{"-CreatedAt", "-_score"})
break
default:
search.SortBy([]string{"_score"})
}
searchResults, err := index.Search(search)
if err != nil {
log.Printf("[Error from search] failed to search by query\"%s\": %v\n", query, err)
Expand Down
6 changes: 2 additions & 4 deletions test/blevesearch/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package main

import (
"github.com/blevesearch/bleve/v2"
"github.com/blevesearch/bleve/v2/analysis/analyzer/custom"
"github.com/blevesearch/bleve/v2/analysis/analyzer/keyword"
"github.com/blevesearch/bleve/v2/analysis/token/lowercase"
"github.com/ikawaha/bleveplugin/analysis/lang/ja"
"github.com/shogo82148/go-mecab"
"log"
)

Expand Down Expand Up @@ -40,10 +38,10 @@ func main() {
}
index.Close()

settinrMecab()
settingMecab()
}

func settinrMecab() {
func settingMecab() {
tagger, err := mecab.New(map[string]string{"output-format-type": "wakati"})
if err != nil {
log.Println(err)
Expand Down
Loading

0 comments on commit 52aa015

Please sign in to comment.