mirror of
https://github.com/thomiceli/opengist.git
synced 2025-03-12 02:21:45 -05:00
wip
This commit is contained in:
parent
d98f766361
commit
a13d4a656f
1 changed files with 76 additions and 2 deletions
|
@ -2,9 +2,11 @@ package index
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/meilisearch/meilisearch-go"
|
||||
"github.com/rs/zerolog/log"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MeiliIndexer struct {
|
||||
|
@ -38,6 +40,12 @@ func (i *MeiliIndexer) Init() {
|
|||
func (i *MeiliIndexer) open() (meilisearch.IndexManager, error) {
|
||||
client := meilisearch.New(i.host, meilisearch.WithAPIKey(i.apikey))
|
||||
index, err := client.GetIndex(i.indexName)
|
||||
_, _ = index.IndexManager.UpdateSettings(&meilisearch.Settings{
|
||||
FilterableAttributes: []string{"Username", "Title", "Filenames", "Extensions", "Languages", "Topics"},
|
||||
DisplayedAttributes: []string{"GistID"},
|
||||
Pagination: &meilisearch.Pagination{MaxTotalHits: 5000},
|
||||
SearchableAttributes: []string{"Content", "Username", "Title", "Filenames", "Extensions", "Languages", "Topics"},
|
||||
})
|
||||
if err == nil {
|
||||
return index.IndexManager, nil
|
||||
}
|
||||
|
@ -49,7 +57,9 @@ func (i *MeiliIndexer) open() (meilisearch.IndexManager, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return client.Index(i.indexName), nil
|
||||
indexManager := client.Index(i.indexName)
|
||||
|
||||
return indexManager, nil
|
||||
}
|
||||
|
||||
func (i *MeiliIndexer) Close() {
|
||||
|
@ -73,5 +83,69 @@ func (i *MeiliIndexer) Remove(gistID uint) error {
|
|||
}
|
||||
|
||||
func (i *MeiliIndexer) Search(queryStr string, queryMetadata SearchGistMetadata, gistsIds []uint, page int) ([]uint, uint64, map[string]int, error) {
|
||||
return nil, 0, nil, nil
|
||||
searchRequest := &meilisearch.SearchRequest{
|
||||
Offset: int64((page - 1) * 10),
|
||||
Limit: 11, // 10 + 1 to check if there are more results
|
||||
AttributesToRetrieve: []string{"GistID"},
|
||||
}
|
||||
|
||||
if queryStr != "" {
|
||||
searchRequest.AttributesToSearchOn = []string{"Content"}
|
||||
}
|
||||
|
||||
var filters []string
|
||||
|
||||
// Add metadata filters
|
||||
if queryMetadata.Username != "" && queryMetadata.Username != "." {
|
||||
filters = append(filters, fmt.Sprintf("Username = \"%s\"", queryMetadata.Username))
|
||||
}
|
||||
|
||||
if queryMetadata.Title != "" && queryMetadata.Title != "." {
|
||||
filters = append(filters, fmt.Sprintf("Title = \"%s\"", queryMetadata.Title))
|
||||
}
|
||||
|
||||
if queryMetadata.Filename != "" && queryMetadata.Filename != "." {
|
||||
filters = append(filters, fmt.Sprintf("Filenames = \"%s\"", queryMetadata.Filename))
|
||||
}
|
||||
|
||||
if queryMetadata.Extension != "" && queryMetadata.Extension != "." {
|
||||
filters = append(filters, fmt.Sprintf("Extensions = \".%s\"", queryMetadata.Extension))
|
||||
}
|
||||
|
||||
if queryMetadata.Language != "" && queryMetadata.Language != "." {
|
||||
filters = append(filters, fmt.Sprintf("Languages = \"%s\"", queryMetadata.Language))
|
||||
}
|
||||
|
||||
if queryMetadata.Topic != "" && queryMetadata.Topic != "." {
|
||||
filters = append(filters, fmt.Sprintf("Topics = \"%s\"", queryMetadata.Topic))
|
||||
}
|
||||
|
||||
// Combine all filters with AND
|
||||
if len(filters) > 0 {
|
||||
fmt.Println("filters", strings.Join(filters, " AND "))
|
||||
searchRequest.Filter = strings.Join(filters, " AND ")
|
||||
}
|
||||
|
||||
var response *meilisearch.SearchResponse
|
||||
var err error
|
||||
|
||||
if queryStr != "" {
|
||||
fmt.Println("Query string:", queryStr)
|
||||
response, err = i.index.Search(queryStr, searchRequest)
|
||||
} else {
|
||||
response, err = i.index.Search("", searchRequest)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
|
||||
fmt.Println(response)
|
||||
fmt.Println(response.EstimatedTotalHits)
|
||||
gistIds := make([]uint, 0, len(response.Hits))
|
||||
for _, hit := range response.Hits {
|
||||
gistIds = append(gistIds, uint(hit.(map[string]interface{})["GistID"].(float64)))
|
||||
}
|
||||
|
||||
return gistIds, uint64(response.EstimatedTotalHits), nil, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue