Docs
Guides
Search Relevance

Out the box, Searchkit provides a Elasticsearch query that is designed to work well for most use cases. However, you may want to customise the query to improve the relevance of the search results.

This example shows how to use the getQuery function to customise the query for a given request.

Below is an example of using the combined_fields query (opens in a new tab) instead.

const results = await apiClient.handleRequest(req.body, {
  getQuery: (query, search_attributes) => {
    return [
      {
        combined_fields: {
          query,
          fields: search_attributes,
        },
      },
    ];
  },
});

You can also use the hooks to customise the query. For example, to add a rescore query to the default query.

Read more about hooks in api-documentation.

Request hooks

Request hooks are functions that are called before and after the search request is sent to Elasticsearch. They can be used to modify the search request and response before it is sent to Elasticsearch and to the UI.

Request hooks allow you to add custom logic to the search request and response. For example, you can add a rescore query to the default query, or add a custom query to the request.

Rescore Query Example

Could be also used for Open source connections Learn to rank plugin (opens in a new tab)

  const results = await client.handleRequest(req.body, {
    hooks: {
      beforeSearch: (searchRequests) => {
        const uiRequest = searchRequests[0]
 
        return [
          {
            ...uiRequest,
            body: {
              ...uiRequest.body,
              rescore: {
                window_size: 100,
                query: {
                  rescore_query: {
                    match: {
                      plot: {
                        query: uiRequest.body.query,
                        operator: "and",
                      },
                    },
                  },
                  query_weight: 1,
                  rescore_query_weight: 10,
                }
              }
            }
          },
          searchRequests.slice(1, searchRequests.length)
        ]
        
      },
    }
  });

Example of doing a KNN search

An example of extending the query to do a hybrid search with KNN search and text search. Retrieves the search query provided in the UI and uses it to retrieve an image embedding from a custom image search API. Then extends the query to do a hybrid search with KNN search (with embedding) and text search.

  const results = await client.handleRequest(req.body, {
    getKnnQuery(query, search_attributes, config) {
      return {
        field: 'dense-vector-field',
        k: 10,
        num_candidates: 100,
        // supported in latest version of Elasticsearch
        query_vector_builder: { 
          text_embedding: {
            model_id: 'cookie_model',
            model_text: query
          }
        }
      }
    }
  });

Apache 2.0 2024 © Joseph McElroy.
Need help? Join discord