Skip to content

Commit

Permalink
fix search
Browse files Browse the repository at this point in the history
  • Loading branch information
kavos113 committed Sep 5, 2024
1 parent 1545525 commit 57e2139
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/scraper/newMessages.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (s *Scraper) addMessageIndex(wikiId int) {
Title: wiki.Name,
OwnerTraqID: wiki.OwnerTraqID,
MessageContent: wiki.Content,
CreateAt: wiki.CreatedAt,
CreatedAt: wiki.CreatedAt,
},
}

Expand Down
2 changes: 1 addition & 1 deletion src/scraper/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (s *Scraper) SetIndexing() {
Title: wiki.Name,
OwnerTraqID: wiki.OwnerTraqID,
MessageContent: ProcessLink(removeNewLine(removeCodeBlock(removeTeX(wiki.Content)))),
CreateAt: wiki.CreatedAt,
CreatedAt: wiki.CreatedAt,
})
}

Expand Down
7 changes: 5 additions & 2 deletions src/search/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type IndexData struct {
Title string
OwnerTraqID string
MessageContent string
CreateAt time.Time
CreatedAt time.Time
}

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

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

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

// return array of ID
// Search return array of ID
// return empty array if no result or error
// sort: "createdAt_oldest", "createdAt_newest", "none"
func Search(query string, limit int, offset int, sort string) []int {
Expand Down Expand Up @@ -35,9 +35,13 @@ func Search(query string, limit int, offset int, sort string) []int {
search := bleve.NewSearchRequestOptions(bleveQuery, limit, offset, false)
switch sort {
case "createdAt_oldest":
search.SortBy([]string{"CreatedAt"})
search.SortBy([]string{"CreatedAt", "-_score"})
break
case "createdAt_newest":
search.SortBy([]string{"-CreatedAt"})
search.SortBy([]string{"-CreatedAt", "-_score"})
break
default:
search.SortBy([]string{"_score"})
}
searchResults, err := index.Search(search)
if err != nil {
Expand Down
28 changes: 14 additions & 14 deletions test/blevesearch/mec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,51 @@ func main() {
data := []IndexDatas{
{
ID: 1,
Type: "type1",
Type: "type",
Title: "title1",
OwnerTraqID: "owner1",
MessageContent: "GoはGoogleによって作られたオープンソースのプログラミング言語です。",
CreateAt: time.Now(),
CreatedAt: time.Now(),
},
{
ID: 2,
Type: "type2",
Type: "type",
Title: "title2",
OwnerTraqID: "owner2",
MessageContent: "swiftはGoogleによって作られたオープンソースのプログラミング言語です。",
CreateAt: time.Now().Add(-48 * time.Hour),
CreatedAt: time.Now().Add(-48 * time.Hour),
},
{
ID: 3,
Type: "type3",
Type: "type",
Title: "title3",
OwnerTraqID: "owner3",
MessageContent: "PythonはGuido van Rossumによって作られたオープンソースのプログラミング言語です。",
CreateAt: time.Now().Add(-24 * time.Hour),
CreatedAt: time.Now().Add(-24 * time.Hour),
},
{
ID: 4,
Type: "type4",
Type: "type",
Title: "title4",
OwnerTraqID: "owner4",
MessageContent: "RubyはYukihiro Matsumotoによって作られたオープンソースのプログラミング言語です。",
CreateAt: time.Now().Add(-72 * time.Hour),
CreatedAt: time.Now().Add(-72 * time.Hour),
},
{
ID: 5,
Type: "type5",
Type: "type",
Title: "title5",
OwnerTraqID: "owner5",
MessageContent: "JavaはSun Microsystemsによって作られたオープンソースのプログラミング言語です。",
CreateAt: time.Now().Add(-96 * time.Hour),
CreatedAt: time.Now().Add(-96 * time.Hour),
},
{
ID: 6,
Type: "type6",
Type: "type",
Title: "title6",
OwnerTraqID: "owner6",
MessageContent: "C++はBjarne Stroustrupによって作られたオープンソースのプログラミング言語です。",
CreateAt: time.Now().Add(-120 * time.Hour),
CreatedAt: time.Now().Add(-120 * time.Hour),
},
}

Expand Down Expand Up @@ -113,7 +113,7 @@ func main() {
bleveQuery := bleve.NewMatchQuery("プログラミング言語")
bleveQuery.SetField("MessageContent")
search := bleve.NewSearchRequestOptions(bleveQuery, 10, 0, false)
search.SortBy([]string{"-CreateAt"})
search.SortBy([]string{"CreatedAt", "-_score"})
searchResults, err := index.Search(search)
if err != nil {
log.Printf("[Error from search] failed to search by query\"%s\": %v\n", "プログラミング言語", err)
Expand All @@ -138,5 +138,5 @@ type IndexDatas struct {
Title string
OwnerTraqID string
MessageContent string
CreateAt time.Time
CreatedAt time.Time
}

0 comments on commit 57e2139

Please sign in to comment.