Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions index_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This CropMarker can be null?

Copy link
Contributor Author

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 string and not as *string. In go if 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 to nil.
But it would have been much less convenient for the declaration so I preferred to make it easier for the user.

searchPostRequestParams["cropMarker"] = request.CropMarker
}
if request.HighlightPreTag != "" {
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}
Expand Down
109 changes: 98 additions & 11 deletions index_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give some idea about this float64(...)? Why do you need to convert it to float?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:
bool, for JSON booleans
float64, for JSON numbers
string, for JSON strings
[]interface{}, for JSON arrays
map[string]interface{}, for JSON objects
nil for JSON null

And by default, every number declare are int so in:

	map[string]interface{}{
		"book_id": 1,
	}

book_id will be an int that is why I convert it. I didn't find a better way to do it.

},
},
NbHits: 20,
Expand Down Expand Up @@ -164,13 +164,16 @@ func TestIndex_Search(t *testing.T) {
query: "to",
request: SearchRequest{
AttributesToCrop: []string{"title"},
CropLength: 7,
CropLength: 2,
},
},
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,
Expand All @@ -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: "TestIndexSearchWithCustomPreAndPostHighlightTags",
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{
Expand All @@ -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",
},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ type SearchRequest struct {
AttributesToRetrieve []string
AttributesToCrop []string
CropLength int64
CropMarker string
AttributesToHighlight []string
HighlightPreTag string
HighlightPostTag string
Filter interface{}
Matches bool
FacetsDistribution []string
Expand Down
21 changes: 21 additions & 0 deletions types_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.