Skip to content

Commit

Permalink
Merge branch 'main' into add-GetMemoHandler-after-fixed-around-tags
Browse files Browse the repository at this point in the history
  • Loading branch information
blancnoir256 authored Aug 21, 2024
2 parents 78b2eec + 427c966 commit 6c030fd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/scraper/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (s *Scraper) GetSodanMessages(bot *traqwsbot.Bot) {

s.updateWikisContent()
s.setSodanTags()
s.setIndexing()
}

func (s *Scraper) GetSodanSubMessages(bot *traqwsbot.Bot, channelId string, offset int, limit int) {
Expand Down
9 changes: 9 additions & 0 deletions src/search/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,13 @@ func Indexing(data []IndexData) {
return
}
log.Printf("[From search engine] Finish index successfully. doc count: %d\n\n", docCount)

err = index.Close()
if err != nil {
log.Printf("[Error from search engine] failed to close index: %v\n", err)
return
}

res := Search("windows", 10, 0)
log.Println(res)
}
12 changes: 9 additions & 3 deletions src/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,36 @@ import (
// return array of ID
// return empty array if no result or error
func Search(query string, limit int, offset int) []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.")
}

index, err := bleve.Open("index.bleve")
if err != nil {
log.Printf("failed to open index: %v\n", err)
log.Printf("[Error from search] failed to open index: %v\n", err)
}

bleveQuery := bleve.NewMatchQuery(query)
bleveQuery.SetField("Content")
search := bleve.NewSearchRequest(bleveQuery)
searchResults, err := index.Search(search)
if err != nil {
log.Printf("failed to search by query\"%s\": %v\n", query, err)
log.Printf("[Error from search] failed to search by query\"%s\": %v\n", query, err)
return []int{}
}

err = index.Close()
if err != nil {
log.Printf("[Error from search] failed to close index: %v\n", err)
}

returnCount := min(limit, int(searchResults.Total)-offset)
res := make([]int, returnCount)
for i := offset; i < offset+returnCount; i++ {
res[i-offset], err = strconv.Atoi(searchResults.Hits[i].ID)
if err != nil {
log.Printf("failed to convert ID to int: %v\n", err)
log.Printf("[Error from search] failed to convert ID to int: %v\n", err)
return []int{}
}
}
Expand Down
13 changes: 9 additions & 4 deletions test/blevesearch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ type IndexData struct {
}

func main() {
index := setting()
setting()
index, err := bleve.Open("index.bleve")
if err != nil {
log.Println(err)
return
}

// search for some text
query := bleve.NewMatchQuery("作る 並行")
Expand All @@ -32,9 +37,10 @@ func main() {
for _, hit := range searchResults.Hits {
log.Printf("ID: %s, Score: %f\n", hit.ID, hit.Score)
}
index.Close()
}

func setting() bleve.Index {
func setting() {
// open a new index
typeMapping := bleve.NewTextFieldMapping()
typeMapping.Analyzer = keyword.Name
Expand Down Expand Up @@ -130,6 +136,5 @@ func setting() bleve.Index {
panic(err)
}
log.Printf("doc count: %d\n\n", docCount)

return index
index.Close()
}
3 changes: 3 additions & 0 deletions test/db.http
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ Content-Type: application/json
{
"name": "test"
}

###
GET http://localhost:8080/search

0 comments on commit 6c030fd

Please sign in to comment.