Files
search-go/index_search.go
7745deccf0 Refactor the library (#557)
* docs: add package documentation

* fix: refactor fasthttp client to http client

* chore: add general errors

* feat: add option pattern for make optional client

* docs: update documentation of types

* chore: add example test for function New and Connect

* docs: update readme for new meilisearch

* chore: add helper function with test

* feat: add service manager to manage index, key, task and other operation

* feat: add index manager to manage index and search

* fix: update module version to 1.17 for fix issue testify

* fix: update go module

* fix: change testify version to stable and support go 1.16

* docs: update example documentation

* chore: update many for coverage

* fix: improved coverage test of client

* fix: client test except on pool

* fix: change old client example to new

* fix: add test for RawType to improve coverage

* fix: add update documents test for improve coverage

* fix: improved coverage test of client with some scenarios

---------

Co-authored-by: Javad <[email protected]>

Co-authored-by: Hanzo Dev <[email protected]>
2024-08-14 14:55:28 +02:00

133 lines
3.3 KiB
Go

package meilisearch
import (
"context"
"encoding/json"
"net/http"
)
func (i *index) Search(query string, request *SearchRequest) (*SearchResponse, error) {
return i.SearchWithContext(context.Background(), query, request)
}
func (i *index) SearchWithContext(ctx context.Context, query string, request *SearchRequest) (*SearchResponse, error) {
if request == nil {
return nil, ErrNoSearchRequest
}
if query != "" {
request.Query = query
}
if request.IndexUID != "" {
request.IndexUID = ""
}
request.validate()
resp := new(SearchResponse)
req := &internalRequest{
endpoint: "/indexes/" + i.uid + "/search",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "Search",
}
if err := i.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (i *index) SearchRaw(query string, request *SearchRequest) (*json.RawMessage, error) {
return i.SearchRawWithContext(context.Background(), query, request)
}
func (i *index) SearchRawWithContext(ctx context.Context, query string, request *SearchRequest) (*json.RawMessage, error) {
if request == nil {
return nil, ErrNoSearchRequest
}
if query != "" {
request.Query = query
}
if request.IndexUID != "" {
request.IndexUID = ""
}
request.validate()
resp := new(json.RawMessage)
req := &internalRequest{
endpoint: "/indexes/" + i.uid + "/search",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "SearchRaw",
}
if err := i.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (i *index) FacetSearch(request *FacetSearchRequest) (*json.RawMessage, error) {
return i.FacetSearchWithContext(context.Background(), request)
}
func (i *index) FacetSearchWithContext(ctx context.Context, request *FacetSearchRequest) (*json.RawMessage, error) {
if request == nil {
return nil, ErrNoFacetSearchRequest
}
resp := new(json.RawMessage)
req := &internalRequest{
endpoint: "/indexes/" + i.uid + "/facet-search",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "FacetSearch",
}
if err := i.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (i *index) SearchSimilarDocuments(param *SimilarDocumentQuery, resp *SimilarDocumentResult) error {
return i.SearchSimilarDocumentsWithContext(context.Background(), param, resp)
}
func (i *index) SearchSimilarDocumentsWithContext(ctx context.Context, param *SimilarDocumentQuery, resp *SimilarDocumentResult) error {
req := &internalRequest{
endpoint: "/indexes/" + i.uid + "/similar",
method: http.MethodPost,
withRequest: param,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "SearchSimilarDocuments",
contentType: contentTypeJSON,
}
if err := i.client.executeRequest(ctx, req); err != nil {
return err
}
return nil
}