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
To follow this example, you must set up a text embedding model. For more information, see Choosing a model. If you have already generated text embeddings, ingest the embeddings into an index and skip to Step 4.

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.

Example: Combining a neural query and a match query

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. "passage_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. }

copy

Example: Combining a match query and a term query

The following example request combines two query clauses—a match query and a term 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. "passage_text": "hello"
  14. }
  15. },
  16. {
  17. "term":{
  18. "passage_text":{
  19. "value":"planet"
  20. }
  21. }
  22. }
  23. ]
  24. }
  25. }
  26. }

copy

The response contains the matching documents:

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

copy

Hybrid search with post-filtering

Introduced 2.13

You can perform post-filtering on hybrid search results by providing the post_filter parameter in your query.

The post_filter clause is applied after the search results have been retrieved. Post-filtering is useful for applying additional filters to the search results without impacting the scoring or the order of the results.

Post-filtering does not impact document relevance scores or aggregation results.

Example: Post-filtering

The following example request combines two query clauses—a term query and a match query. This is the same query as in the preceding example, but it contains a post_filter:

  1. GET /my-nlp-index/_search?search_pipeline=nlp-search-pipeline
  2. {
  3. "query": {
  4. "hybrid":{
  5. "queries":[
  6. {
  7. "match":{
  8. "passage_text": "hello"
  9. }
  10. },
  11. {
  12. "term":{
  13. "passage_text":{
  14. "value":"planet"
  15. }
  16. }
  17. }
  18. ]
  19. }
  20. },
  21. "post_filter":{
  22. "match": { "passage_text": "world" }
  23. }
  24. }

copy

Compare the results to the results without post-filtering in the preceding example. Unlike the preceding example response, which contains two documents, the response in this example contains one document because the second document is filtered using post-filtering:

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

Combining hybrid search and aggregations

Introduced 2.13

You can enhance search results by combining a hybrid query clause with any aggregation that OpenSearch supports. Aggregations allow you to use OpenSearch as an analytics engine. For more information about aggregations, see Aggregations.

Most aggregations are performed on the subset of documents that is returned by a hybrid query. The only aggregation that operates on all documents is the global aggregation.

To use aggregations with a hybrid query, first create an index. Aggregations are typically used on fields of special types, like keyword or integer. The following example creates an index with several such fields:

  1. PUT /my-nlp-index
  2. {
  3. "settings": {
  4. "number_of_shards": 2
  5. },
  6. "mappings": {
  7. "properties": {
  8. "doc_index": {
  9. "type": "integer"
  10. },
  11. "doc_keyword": {
  12. "type": "keyword"
  13. },
  14. "category": {
  15. "type": "keyword"
  16. }
  17. }
  18. }
  19. }

copy

The following request ingests six documents into your new index:

  1. POST /_bulk
  2. { "index": { "_index": "my-nlp-index" } }
  3. { "category": "permission", "doc_keyword": "workable", "doc_index": 4976, "doc_price": 100}
  4. { "index": { "_index": "my-nlp-index" } }
  5. { "category": "sister", "doc_keyword": "angry", "doc_index": 2231, "doc_price": 200 }
  6. { "index": { "_index": "my-nlp-index" } }
  7. { "category": "hair", "doc_keyword": "likeable", "doc_price": 25 }
  8. { "index": { "_index": "my-nlp-index" } }
  9. { "category": "editor", "doc_index": 9871, "doc_price": 30 }
  10. { "index": { "_index": "my-nlp-index" } }
  11. { "category": "statement", "doc_keyword": "entire", "doc_index": 8242, "doc_price": 350 }
  12. { "index": { "_index": "my-nlp-index" } }
  13. { "category": "statement", "doc_keyword": "idea", "doc_index": 5212, "doc_price": 200 }
  14. { "index": { "_index": "index-test" } }
  15. { "category": "editor", "doc_keyword": "bubble", "doc_index": 1298, "doc_price": 130 }
  16. { "index": { "_index": "index-test" } }
  17. { "category": "editor", "doc_keyword": "bubble", "doc_index": 521, "doc_price": 75 }

copy

Now you can combine a hybrid query clause with a min aggregation:

  1. GET /my-nlp-index/_search?search_pipeline=nlp-search-pipeline
  2. {
  3. "query": {
  4. "hybrid": {
  5. "queries": [
  6. {
  7. "term": {
  8. "category": "permission"
  9. }
  10. },
  11. {
  12. "bool": {
  13. "should": [
  14. {
  15. "term": {
  16. "category": "editor"
  17. }
  18. },
  19. {
  20. "term": {
  21. "category": "statement"
  22. }
  23. }
  24. ]
  25. }
  26. }
  27. ]
  28. }
  29. },
  30. "aggs": {
  31. "total_price": {
  32. "sum": {
  33. "field": "doc_price"
  34. }
  35. },
  36. "keywords": {
  37. "terms": {
  38. "field": "doc_keyword",
  39. "size": 10
  40. }
  41. }
  42. }
  43. }

copy

The response contains the matching documents and the aggregation results:

  1. {
  2. "took": 9,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 2,
  6. "successful": 2,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 4,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.5,
  16. "hits": [
  17. {
  18. "_index": "my-nlp-index",
  19. "_id": "mHRPNY4BlN82W_Ar9UMY",
  20. "_score": 0.5,
  21. "_source": {
  22. "doc_price": 100,
  23. "doc_index": 4976,
  24. "doc_keyword": "workable",
  25. "category": "permission"
  26. }
  27. },
  28. {
  29. "_index": "my-nlp-index",
  30. "_id": "m3RPNY4BlN82W_Ar9UMY",
  31. "_score": 0.5,
  32. "_source": {
  33. "doc_price": 30,
  34. "doc_index": 9871,
  35. "category": "editor"
  36. }
  37. },
  38. {
  39. "_index": "my-nlp-index",
  40. "_id": "nXRPNY4BlN82W_Ar9UMY",
  41. "_score": 0.5,
  42. "_source": {
  43. "doc_price": 200,
  44. "doc_index": 5212,
  45. "doc_keyword": "idea",
  46. "category": "statement"
  47. }
  48. },
  49. {
  50. "_index": "my-nlp-index",
  51. "_id": "nHRPNY4BlN82W_Ar9UMY",
  52. "_score": 0.5,
  53. "_source": {
  54. "doc_price": 350,
  55. "doc_index": 8242,
  56. "doc_keyword": "entire",
  57. "category": "statement"
  58. }
  59. }
  60. ]
  61. },
  62. "aggregations": {
  63. "total_price": {
  64. "value": 680
  65. },
  66. "doc_keywords": {
  67. "doc_count_error_upper_bound": 0,
  68. "sum_other_doc_count": 0,
  69. "buckets": [
  70. {
  71. "key": "entire",
  72. "doc_count": 1
  73. },
  74. {
  75. "key": "idea",
  76. "doc_count": 1
  77. },
  78. {
  79. "key": "workable",
  80. "doc_count": 1
  81. }
  82. ]
  83. }
  84. }
  85. }

Using sorting with a hybrid query

Introduced 2.16

By default, hybrid search returns results ordered by scores in descending order. You can apply sorting to hybrid query results by providing the sort criteria in the search request. For more information about sort criteria, see Sort results. When sorting is applied to a hybrid search, results are fetched from the shards based on the specified sort criteria. As a result, the search results are sorted accordingly, and the document scores are null. Scores are only present in the hybrid search sorting results if documents are sorted by _score.

In the following example, sorting is applied by doc_price in the hybrid query search request:

  1. GET /my-nlp-index/_search?search_pipeline=nlp-search-pipeline
  2. {
  3. "query": {
  4. "hybrid": {
  5. "queries": [
  6. {
  7. "term": {
  8. "category": "permission"
  9. }
  10. },
  11. {
  12. "bool": {
  13. "should": [
  14. {
  15. "term": {
  16. "category": "editor"
  17. }
  18. },
  19. {
  20. "term": {
  21. "category": "statement"
  22. }
  23. }
  24. ]
  25. }
  26. }
  27. ]
  28. }
  29. },
  30. "sort":[
  31. {
  32. "doc_price": {
  33. "order": "desc"
  34. }
  35. }
  36. ]
  37. }

copy

The response contains the matching documents sorted by doc_price in descending order:

  1. {
  2. "took": 35,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 3,
  6. "successful": 3,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 4,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.5,
  16. "hits": [
  17. {
  18. "_index": "my-nlp-index",
  19. "_id": "7yaM4JABZkI1FQv8AwoN",
  20. "_score": null,
  21. "_source": {
  22. "category": "statement",
  23. "doc_keyword": "entire",
  24. "doc_index": 8242,
  25. "doc_price": 350
  26. },
  27. "sort": [
  28. 350
  29. ]
  30. },
  31. {
  32. "_index": "my-nlp-index",
  33. "_id": "8CaM4JABZkI1FQv8AwoN",
  34. "_score": null,
  35. "_source": {
  36. "category": "statement",
  37. "doc_keyword": "idea",
  38. "doc_index": 5212,
  39. "doc_price": 200
  40. },
  41. "sort": [
  42. 200
  43. ]
  44. },
  45. {
  46. "_index": "my-nlp-index",
  47. "_id": "6yaM4JABZkI1FQv8AwoM",
  48. "_score": null,
  49. "_source": {
  50. "category": "permission",
  51. "doc_keyword": "workable",
  52. "doc_index": 4976,
  53. "doc_price": 100
  54. },
  55. "sort": [
  56. 100
  57. ]
  58. },
  59. {
  60. "_index": "my-nlp-index",
  61. "_id": "7iaM4JABZkI1FQv8AwoN",
  62. "_score": null,
  63. "_source": {
  64. "category": "editor",
  65. "doc_index": 9871,
  66. "doc_price": 30
  67. },
  68. "sort": [
  69. 30
  70. ]
  71. }
  72. ]
  73. }
  74. }

In the following example, sorting is applied by _id:

  1. GET /my-nlp-index/_search?search_pipeline=nlp-search-pipeline
  2. {
  3. "query": {
  4. "hybrid": {
  5. "queries": [
  6. {
  7. "term": {
  8. "category": "permission"
  9. }
  10. },
  11. {
  12. "bool": {
  13. "should": [
  14. {
  15. "term": {
  16. "category": "editor"
  17. }
  18. },
  19. {
  20. "term": {
  21. "category": "statement"
  22. }
  23. }
  24. ]
  25. }
  26. }
  27. ]
  28. }
  29. },
  30. "sort":[
  31. {
  32. "_id": {
  33. "order": "desc"
  34. }
  35. }
  36. ]
  37. }

copy

The response contains the matching documents sorted by _id in descending order:

  1. {
  2. "took": 33,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 3,
  6. "successful": 3,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 4,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.5,
  16. "hits": [
  17. {
  18. "_index": "my-nlp-index",
  19. "_id": "8CaM4JABZkI1FQv8AwoN",
  20. "_score": null,
  21. "_source": {
  22. "category": "statement",
  23. "doc_keyword": "idea",
  24. "doc_index": 5212,
  25. "doc_price": 200
  26. },
  27. "sort": [
  28. "8CaM4JABZkI1FQv8AwoN"
  29. ]
  30. },
  31. {
  32. "_index": "my-nlp-index",
  33. "_id": "7yaM4JABZkI1FQv8AwoN",
  34. "_score": null,
  35. "_source": {
  36. "category": "statement",
  37. "doc_keyword": "entire",
  38. "doc_index": 8242,
  39. "doc_price": 350
  40. },
  41. "sort": [
  42. "7yaM4JABZkI1FQv8AwoN"
  43. ]
  44. },
  45. {
  46. "_index": "my-nlp-index",
  47. "_id": "7iaM4JABZkI1FQv8AwoN",
  48. "_score": null,
  49. "_source": {
  50. "category": "editor",
  51. "doc_index": 9871,
  52. "doc_price": 30
  53. },
  54. "sort": [
  55. "7iaM4JABZkI1FQv8AwoN"
  56. ]
  57. },
  58. {
  59. "_index": "my-nlp-index",
  60. "_id": "6yaM4JABZkI1FQv8AwoM",
  61. "_score": null,
  62. "_source": {
  63. "category": "permission",
  64. "doc_keyword": "workable",
  65. "doc_index": 4976,
  66. "doc_price": 100
  67. },
  68. "sort": [
  69. "6yaM4JABZkI1FQv8AwoM"
  70. ]
  71. }
  72. ]
  73. }
  74. }

Hybrid search with search_after

Introduced 2.16

You can control sorting results by applying a search_after condition that provides a live cursor and uses the previous page’s results to obtain the next page’s results. For more information about search_after, see The search_after parameter.

You can paginate the sorted results by applying a search_after condition in the sort queries.

In the following example, sorting is applied by doc_price with a search_after condition:

  1. GET /my-nlp-index/_search?search_pipeline=nlp-search-pipeline
  2. {
  3. "query": {
  4. "hybrid": {
  5. "queries": [
  6. {
  7. "term": {
  8. "category": "permission"
  9. }
  10. },
  11. {
  12. "bool": {
  13. "should": [
  14. {
  15. "term": {
  16. "category": "editor"
  17. }
  18. },
  19. {
  20. "term": {
  21. "category": "statement"
  22. }
  23. }
  24. ]
  25. }
  26. }
  27. ]
  28. }
  29. },
  30. "sort":[
  31. {
  32. "_id": {
  33. "order": "desc"
  34. }
  35. }
  36. ],
  37. "search_after":[200]
  38. }

copy

The response contains the matching documents that are listed after the 200 sort value, sorted by doc_price in descending order:

  1. {
  2. "took": 8,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 3,
  6. "successful": 3,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 4,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.5,
  16. "hits": [
  17. {
  18. "_index": "my-nlp-index",
  19. "_id": "6yaM4JABZkI1FQv8AwoM",
  20. "_score": null,
  21. "_source": {
  22. "category": "permission",
  23. "doc_keyword": "workable",
  24. "doc_index": 4976,
  25. "doc_price": 100
  26. },
  27. "sort": [
  28. 100
  29. ]
  30. },
  31. {
  32. "_index": "my-nlp-index",
  33. "_id": "7iaM4JABZkI1FQv8AwoN",
  34. "_score": null,
  35. "_source": {
  36. "category": "editor",
  37. "doc_index": 9871,
  38. "doc_price": 30
  39. },
  40. "sort": [
  41. 30
  42. ]
  43. }
  44. ]
  45. }
  46. }

In the following example, sorting is applied by id with a search_after condition:

  1. GET /my-nlp-index/_search?search_pipeline=nlp-search-pipeline
  2. {
  3. "query": {
  4. "hybrid": {
  5. "queries": [
  6. {
  7. "term": {
  8. "category": "permission"
  9. }
  10. },
  11. {
  12. "bool": {
  13. "should": [
  14. {
  15. "term": {
  16. "category": "editor"
  17. }
  18. },
  19. {
  20. "term": {
  21. "category": "statement"
  22. }
  23. }
  24. ]
  25. }
  26. }
  27. ]
  28. }
  29. },
  30. "sort":[
  31. {
  32. "_id": {
  33. "order": "desc"
  34. }
  35. }
  36. ],
  37. "search_after":["7yaM4JABZkI1FQv8AwoN"]
  38. }

copy

The response contains the matching documents that are listed after the 7yaM4JABZkI1FQv8AwoN sort value, sorted by id in descending order:

  1. {
  2. "took": 17,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 3,
  6. "successful": 3,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 4,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.5,
  16. "hits": [
  17. {
  18. "_index": "my-nlp-index",
  19. "_id": "7iaM4JABZkI1FQv8AwoN",
  20. "_score": null,
  21. "_source": {
  22. "category": "editor",
  23. "doc_index": 9871,
  24. "doc_price": 30
  25. },
  26. "sort": [
  27. "7iaM4JABZkI1FQv8AwoN"
  28. ]
  29. },
  30. {
  31. "_index": "my-nlp-index",
  32. "_id": "6yaM4JABZkI1FQv8AwoM",
  33. "_score": null,
  34. "_source": {
  35. "category": "permission",
  36. "doc_keyword": "workable",
  37. "doc_index": 4976,
  38. "doc_price": 100
  39. },
  40. "sort": [
  41. "6yaM4JABZkI1FQv8AwoM"
  42. ]
  43. }
  44. ]
  45. }
  46. }