-
Couldn't load subscription status.
- Fork 104
Add new search parameters highlightPreTag, highlightPostTag and cropMarker #291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,15 @@ func (i Index) Search(query string, request *SearchRequest) (*SearchResponse, er | |
| if request.CropLength != 0 { | ||
| searchPostRequestParams["cropLength"] = request.CropLength | ||
| } | ||
| if request.CropMarker != "" { | ||
| searchPostRequestParams["cropMarker"] = request.CropMarker | ||
| } | ||
| if request.HighlightPreTag != "" { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a way to automate this kind of thing, like serialize somehow? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would love it! I didn't find any other ways |
||
| searchPostRequestParams["highlightPreTag"] = request.HighlightPreTag | ||
| } | ||
| if request.HighlightPostTag != "" { | ||
| searchPostRequestParams["highlightPostTag"] = request.HighlightPostTag | ||
| } | ||
| if len(request.AttributesToRetrieve) != 0 { | ||
| searchPostRequestParams["attributesToRetrieve"] = request.AttributesToRetrieve | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,7 +100,7 @@ func TestIndex_Search(t *testing.T) { | |
| want: &SearchResponse{ | ||
| Hits: []interface{}{ | ||
| map[string]interface{}{ | ||
| "book_id": float64(1), "title": "Alice In Wonderland", | ||
| "book_id": float64(123), "title": "Pride and Prejudice", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you give some idea about this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the default behavior of the deserializer in golang you can read in the documentation
And by default, every number declare are
|
||
| }, | ||
| }, | ||
| NbHits: 20, | ||
|
|
@@ -164,13 +164,16 @@ func TestIndex_Search(t *testing.T) { | |
| query: "to", | ||
| request: SearchRequest{ | ||
| AttributesToCrop: []string{"title"}, | ||
| CropLength: 7, | ||
| CropLength: 2, | ||
alallema marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }, | ||
| want: &SearchResponse{ | ||
| Hits: []interface{}{ | ||
| map[string]interface{}{ | ||
| "book_id": float64(42), "title": "The Hitchhiker's Guide to the Galaxy", | ||
| "_formatted": map[string]interface{}{ | ||
| "book_id": "42", "tag": "Epic fantasy", "title": "…Guide to…", "year": "1978", | ||
| }, | ||
| }, | ||
| }, | ||
| NbHits: 1, | ||
|
|
@@ -179,6 +182,87 @@ func TestIndex_Search(t *testing.T) { | |
| ExhaustiveNbHits: false, | ||
| }, | ||
| }, | ||
| { | ||
| name: "TestIndexSearchWithAttributesToCropAndCustomCropMarker", | ||
| args: args{ | ||
| UID: "indexUID", | ||
| client: defaultClient, | ||
| query: "to", | ||
| request: SearchRequest{ | ||
| AttributesToCrop: []string{"title"}, | ||
| CropLength: 2, | ||
| CropMarker: "(ꈍᴗꈍ)", | ||
| }, | ||
| }, | ||
| want: &SearchResponse{ | ||
| Hits: []interface{}{ | ||
| map[string]interface{}{ | ||
| "book_id": float64(42), "title": "The Hitchhiker's Guide to the Galaxy", | ||
| "_formatted": map[string]interface{}{ | ||
| "book_id": "42", "tag": "Epic fantasy", "title": "(ꈍᴗꈍ)Guide to(ꈍᴗꈍ)", "year": "1978", | ||
| }, | ||
| }, | ||
| }, | ||
| NbHits: 1, | ||
| Offset: 0, | ||
| Limit: 20, | ||
| ExhaustiveNbHits: false, | ||
| }, | ||
| }, | ||
| { | ||
| name: "TestIndexSearchWithAttributeToHighlight", | ||
| args: args{ | ||
| UID: "indexUID", | ||
| client: defaultClient, | ||
| query: "prince", | ||
| request: SearchRequest{ | ||
| Limit: 1, | ||
| AttributesToHighlight: []string{"*"}, | ||
| }, | ||
| }, | ||
| want: &SearchResponse{ | ||
| Hits: []interface{}{ | ||
| map[string]interface{}{ | ||
| "book_id": float64(456), "title": "Le Petit Prince", | ||
| "_formatted": map[string]interface{}{ | ||
| "book_id": "456", "tag": "Tale", "title": "Le Petit <em>Prince</em>", "year": "1943", | ||
| }, | ||
| }, | ||
| }, | ||
| NbHits: 2, | ||
| Offset: 0, | ||
| Limit: 1, | ||
| ExhaustiveNbHits: false, | ||
| }, | ||
| }, | ||
| { | ||
| name: "TestIndexSearchWithAttributeToHighlightWithCustomPreAndPostTag", | ||
alallema marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| args: args{ | ||
| UID: "indexUID", | ||
| client: defaultClient, | ||
| query: "prince", | ||
| request: SearchRequest{ | ||
| Limit: 1, | ||
| AttributesToHighlight: []string{"*"}, | ||
| HighlightPreTag: "(⊃。•́‿•̀。)⊃ ", | ||
| HighlightPostTag: " ⊂(´• ω •`⊂)", | ||
| }, | ||
| }, | ||
| want: &SearchResponse{ | ||
| Hits: []interface{}{ | ||
| map[string]interface{}{ | ||
| "book_id": float64(456), "title": "Le Petit Prince", | ||
| "_formatted": map[string]interface{}{ | ||
| "book_id": "456", "tag": "Tale", "title": "Le Petit (⊃。•́‿•̀。)⊃ Prince ⊂(´• ω •`⊂)", "year": "1943", | ||
| }, | ||
| }, | ||
| }, | ||
| NbHits: 2, | ||
| Offset: 0, | ||
| Limit: 1, | ||
| ExhaustiveNbHits: false, | ||
| }, | ||
| }, | ||
| { | ||
| name: "TestIndexSearchWithMatches", | ||
| args: args{ | ||
|
|
@@ -191,15 +275,15 @@ func TestIndex_Search(t *testing.T) { | |
| }, | ||
| want: &SearchResponse{ | ||
| Hits: []interface{}{ | ||
| map[string]interface{}{ | ||
| "book_id": float64(1032), "title": "Crime and Punishment", | ||
| }, | ||
| map[string]interface{}{ | ||
| "book_id": float64(123), "title": "Pride and Prejudice", | ||
| }, | ||
| map[string]interface{}{ | ||
| "book_id": float64(730), "title": "War and Peace", | ||
| }, | ||
| map[string]interface{}{ | ||
| "book_id": float64(1032), "title": "Crime and Punishment", | ||
| }, | ||
| map[string]interface{}{ | ||
| "book_id": float64(4), "title": "Harry Potter and the Half-Blood Prince", | ||
| }, | ||
|
|
@@ -245,6 +329,9 @@ func TestIndex_Search(t *testing.T) { | |
| require.Equal(t, tt.want.Hits[len].(map[string]interface{})["title"], got.Hits[len].(map[string]interface{})["title"]) | ||
| require.Equal(t, tt.want.Hits[len].(map[string]interface{})["book_id"], got.Hits[len].(map[string]interface{})["book_id"]) | ||
| } | ||
| if tt.want.Hits[0].(map[string]interface{})["_formatted"] != nil { | ||
| require.Equal(t, tt.want.Hits[0].(map[string]interface{})["_formatted"], got.Hits[0].(map[string]interface{})["_formatted"]) | ||
| } | ||
| require.Equal(t, tt.want.NbHits, got.NbHits) | ||
| require.Equal(t, tt.want.Offset, got.Offset) | ||
| require.Equal(t, tt.want.Limit, got.Limit) | ||
|
|
@@ -532,13 +619,13 @@ func TestIndex_SearchWithFilters(t *testing.T) { | |
| want: &SearchResponse{ | ||
| Hits: []interface{}{ | ||
| map[string]interface{}{ | ||
| "book_id": float64(17), "title": "In Search of Lost Time", | ||
| "book_id": float64(742), "title": "The Great Gatsby", | ||
| }, | ||
| map[string]interface{}{ | ||
| "book_id": float64(204), "title": "Ulysses", | ||
| "book_id": float64(17), "title": "In Search of Lost Time", | ||
| }, | ||
| map[string]interface{}{ | ||
| "book_id": float64(742), "title": "The Great Gatsby", | ||
| "book_id": float64(204), "title": "Ulysses", | ||
| }, | ||
| }, | ||
| NbHits: 3, | ||
|
|
@@ -590,13 +677,13 @@ func TestIndex_SearchWithFilters(t *testing.T) { | |
| want: &SearchResponse{ | ||
| Hits: []interface{}{ | ||
| map[string]interface{}{ | ||
| "book_id": float64(1), "title": "Alice In Wonderland", | ||
| "book_id": float64(456), "title": "Le Petit Prince", | ||
| }, | ||
| map[string]interface{}{ | ||
| "book_id": float64(4), "title": "Harry Potter and the Half-Blood Prince", | ||
| "book_id": float64(1), "title": "Alice In Wonderland", | ||
| }, | ||
| map[string]interface{}{ | ||
| "book_id": float64(456), "title": "Le Petit Prince", | ||
| "book_id": float64(4), "title": "Harry Potter and the Half-Blood Prince", | ||
| }, | ||
| }, | ||
| NbHits: 3, | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
CropMarkercan be null?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I declare it as
stringand not as*string. Ingoif you create a struct all fields exist. This is why I check it.I had thought to create every field as
*so that when instantiating the structure they would be set tonil.But it would have been much less convenient for the declaration so I preferred to make it easier for the user.