Skip to content

Commit

Permalink
Merge #483
Browse files Browse the repository at this point in the history
483: Add support for vectorSearch (v1.3) r=alallema a=luigibarbato

# Pull Request

## Related issue
Fixes #465 
## What does this PR do?
- [x] Add the ability receive a new param in the search request called vector.
- [x] Add the ability handle a vector key in the response.
- [x] Add integration tests
## PR checklist
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?


Co-authored-by: Luigi Barbato <[email protected]>
  • Loading branch information
meili-bors[bot] and luigibarbato authored Sep 12, 2023
2 parents 3a6572a + 9bc1fde commit ca77aa0
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 212 deletions.
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- "types_easyjson.go"
4 changes: 4 additions & 0 deletions index_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,9 @@ func searchPostRequestParams(query string, request *SearchRequest) map[string]in
params["sort"] = request.Sort
}

if request.Vector != nil && len(request.Vector) > 0 {
params["vector"] = request.Vector
}

return params
}
36 changes: 36 additions & 0 deletions index_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1599,3 +1599,39 @@ func TestIndex_SearchWithShowRankingScore(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, got.Hits[0].(map[string]interface{})["_rankingScore"])
}

func TestIndex_SearchWithVectorStore(t *testing.T) {
type args struct {
UID string
PrimaryKey string
client *Client
query string
request SearchRequest
}
testArg := args{
UID: "indexUID",
client: defaultClient,
query: "Pride and Prejudice",
request: SearchRequest{
Vector: []float64{0.1, 0.2, 0.3},
},
}

i, err := SetUpIndexWithVector(testArg.UID)
if err != nil{
t.Fatal(err)
}

c := testArg.client
t.Cleanup(cleanup(c))

got, err := i.Search(testArg.query, &testArg.request)
require.NoError(t, err)
require.NotNil(t, got.Hits[0].(map[string]interface{})["_vectors"])

vectorsRes := got.Hits[0].(map[string]interface{})["_vectors"].([]interface{})

for i := range vectorsRes {
require.Equal(t, testArg.request.Vector[i], vectorsRes[i])
}
}
41 changes: 41 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package meilisearch
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -128,6 +129,46 @@ func SetUpEmptyIndex(index *IndexConfig) (resp *Index, err error) {
return client.GetIndex(index.Uid)
}

func SetUpIndexWithVector(indexUID string) (resp *Index, err error) {
client := NewClient(ClientConfig{
Host: getenv("MEILISEARCH_URL", "http://localhost:7700"),
APIKey: masterKey,
})

req := internalRequest{
endpoint: "/experimental-features",
method: http.MethodPatch,
contentType: "application/json",
withRequest: map[string]interface{}{
"vectorStore": true,
},
}

if err := client.executeRequest(req); err != nil {
return nil, err
}

index := client.Index(indexUID)

documents := []map[string]interface{}{
{"book_id": 123, "title": "Pride and Prejudice", "_vectors": []float64{0.1, 0.2, 0.3}},
{"book_id": 456, "title": "Le Petit Prince", "_vectors": []float64{2.4, 8.5, 1.6}},
}

task, err := index.AddDocuments(documents)
if err != nil {
fmt.Println(err)
return nil, err
}

finalTask, _ := index.WaitForTask(task.TaskUID)
if finalTask.Status != "succeeded" {
os.Exit(1)
}

return client.GetIndex(indexUID)
}

func SetUpBasicIndex(indexUID string) {
client := NewClient(ClientConfig{
Host: getenv("MEILISEARCH_URL", "http://localhost:7700"),
Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ type SearchRequest struct {
Facets []string
PlaceholderSearch bool
Sort []string
Vector []float64
HitsPerPage int64
Page int64
IndexUID string
Expand Down
Loading

0 comments on commit ca77aa0

Please sign in to comment.