Hybrid search

Introduced 2.11

Hybrid search combines keyword and neural search to improve search relevance. To implement hybrid search, you need to set up a search pipeline that runs at search time. The search pipeline you’ll configure intercepts search results at an intermediate stage and applies the normalization_processor to them. The normalization_processor normalizes and combines the document scores from multiple query clauses, rescoring the documents according to the chosen normalization and combination techniques.

PREREQUISITE
Before using hybrid search, you must set up a text embedding model. For more information, see Choosing a model.

To use hybrid search, follow these steps:

  1. Create an ingest pipeline.
  2. Create an index for ingestion.
  3. Ingest documents into the index.
  4. Configure a search pipeline.
  5. Search the index using hybrid search.

Step 1: Create an ingest pipeline

To generate vector embeddings, you need to create an ingest pipeline that contains a text_embedding processor, which will convert the text in a document field to vector embeddings. The processor’s field_map determines the input fields from which to generate vector embeddings and the output fields in which to store the embeddings.

The following example request creates an ingest pipeline that converts the text from passage_text to text embeddings and stores the embeddings in passage_embedding:

  1. PUT /_ingest/pipeline/nlp-ingest-pipeline
  2. {
  3. "description": "A text embedding pipeline",
  4. "processors": [
  5. {
  6. "text_embedding": {
  7. "model_id": "bQ1J8ooBpBj3wT4HVUsb",
  8. "field_map": {
  9. "passage_text": "passage_embedding"
  10. }
  11. }
  12. }
  13. ]
  14. }

copy

Step 2: Create an index for ingestion

In order to use the text embedding processor defined in your pipeline, create a k-NN index, adding the pipeline created in the previous step as the default pipeline. Ensure that the fields defined in the field_map are mapped as correct types. Continuing with the example, the passage_embedding field must be mapped as a k-NN vector with a dimension that matches the model dimension. Similarly, the passage_text field should be mapped as text.

The following example request creates a k-NN index that is set up with a default ingest pipeline:

  1. PUT /my-nlp-index
  2. {
  3. "settings": {
  4. "index.knn": true,
  5. "default_pipeline": "nlp-ingest-pipeline"
  6. },
  7. "mappings": {
  8. "properties": {
  9. "id": {
  10. "type": "text"
  11. },
  12. "passage_embedding": {
  13. "type": "knn_vector",
  14. "dimension": 768,
  15. "method": {
  16. "engine": "lucene",
  17. "space_type": "l2",
  18. "name": "hnsw",
  19. "parameters": {}
  20. }
  21. },
  22. "passage_text": {
  23. "type": "text"
  24. }
  25. }
  26. }
  27. }

copy

For more information about creating a k-NN index and using supported methods, see k-NN index.

Step 3: Ingest documents into the index

To ingest documents into the index created in the previous step, send the following requests:

  1. PUT /my-nlp-index/_doc/1
  2. {
  3. "passage_text": "Hello world",
  4. "id": "s1"
  5. }

copy

  1. PUT /my-nlp-index/_doc/2
  2. {
  3. "passage_text": "Hi planet",
  4. "id": "s2"
  5. }

copy

Before the document is ingested into the index, the ingest pipeline runs the text_embedding processor on the document, generating text embeddings for the passage_text field. The indexed document includes the passage_text field, which contains the original text, and the passage_embedding field, which contains the vector embeddings.

Step 4: Configure a search pipeline

To configure a search pipeline with a normalization-processor, use the following request. The normalization technique in the processor is set to min_max, and the combination technique is set to arithmetic_mean. The weights array specifies the weights assigned to each query clause as decimal percentages:

  1. PUT /_search/pipeline/nlp-search-pipeline
  2. {
  3. "description": "Post processor for hybrid search",
  4. "phase_results_processors": [
  5. {
  6. "normalization-processor": {
  7. "normalization": {
  8. "technique": "min_max"
  9. },
  10. "combination": {
  11. "technique": "arithmetic_mean",
  12. "parameters": {
  13. "weights": [
  14. 0.3,
  15. 0.7
  16. ]
  17. }
  18. }
  19. }
  20. }
  21. ]
  22. }

copy

To perform hybrid search on your index, use the hybrid query, which combines the results of keyword and semantic search.

The following example request combines two query clauses—a neural query and a match query. It specifies the search pipeline created in the previous step as a query parameter:

  1. GET /my-nlp-index/_search?search_pipeline=nlp-search-pipeline
  2. {
  3. "_source": {
  4. "exclude": [
  5. "passage_embedding"
  6. ]
  7. },
  8. "query": {
  9. "hybrid": {
  10. "queries": [
  11. {
  12. "match": {
  13. "text": {
  14. "query": "Hi world"
  15. }
  16. }
  17. },
  18. {
  19. "neural": {
  20. "passage_embedding": {
  21. "query_text": "Hi world",
  22. "model_id": "aVeif4oB5Vm0Tdw8zYO2",
  23. "k": 5
  24. }
  25. }
  26. }
  27. ]
  28. }
  29. }
  30. }

copy

Alternatively, you can set a default search pipeline for the my-nlp-index index. For more information, see Default search pipeline.

The response contains the matching document:

  1. {
  2. "took" : 36,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.2251667,
  16. "hits" : [
  17. {
  18. "_index" : "my-nlp-index",
  19. "_id" : "1",
  20. "_score" : 1.2251667,
  21. "_source" : {
  22. "passage_text" : "Hello world",
  23. "id" : "s1"
  24. }
  25. }
  26. ]
  27. }
  28. }