Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Context Suggester: Add support for AND logic between multiple contexts #17681

Open
tkykenmt opened this issue Mar 25, 2025 · 1 comment
Labels
enhancement Enhancement or improvement to existing feature or request Search:Query Capabilities

Comments

@tkykenmt
Copy link

Is your feature request related to a problem? Please describe

The Context Suggester in OpenSearch lacks the ability to process multiple contexts as AND conditions. In the
current implementation, when multiple contexts are specified in a single Suggest query, they are processed as OR
conditions.

For example, if you want to get suggestions that match both a "cafe" category AND a specific geographic
location, the current Context Suggester will return items that match either the "cafe" category OR the specified
geographic location. This behavior is counterintuitive, as in many use cases, users typically want to combine
multiple contexts with AND conditions.

Currently, to work around this limitation, we need to use approaches such as:

  1. Preparing multiple Suggest fields and including separate contexts in each field
  2. Specifying multiple Suggest conditions within a single query and processing the results on the client side

These workarounds make implementation complex and require additional processing on the client side, which is not
an ideal solution.

Describe the solution you'd like

I would like to add functionality to the Context Suggester to process multiple contexts as either AND or OR
conditions. Specifically, the following features are needed:

  1. Provide an option to process multiple contexts specified in a single Suggest query as AND conditions
  2. Allow explicit specification of logical operators (AND/OR) between contexts

For example, it would be beneficial to have syntax like:

{
  "suggest": {
    "place_suggestion": {
      "prefix": "sta",
      "completion": {
        "field": "suggest",
        "size": 10,
        "contexts": {
          "context_operator": "AND",  // New option: "AND" or "OR" (default is "OR" to maintain backward compatibility)
          "place_type": [ "cafe" ],
          "location": {
            "lat": 43.662,
            "lon": -79.380
          }
        }
      }
    }
  }
}

This improvement would allow users to execute suggest queries that combine multiple context conditions
intuitively and efficiently without using complex workarounds. This would be particularly useful when
implementing suggestion features that consider both geographic location and categories.

Related component

Search:Query Capabilities

Describe alternatives you've considered

I've explored several workarounds to address the current limitation:

  1. Multiple Suggest Fields Approach: Creating separate completion fields for each context type and querying them

individually. This requires maintaining multiple fields with redundant data and performing client-side
intersection of results.

   {
     "mappings": {
       "properties": {
         "name_suggest": {
           "type": "completion",
           "contexts": { "name": "place_type" }
         },
         "location_suggest": {
           "type": "completion",
           "contexts": { "name": "location" }
         }
       }
     }
   }
  1. Multiple Suggest Queries in a Single Request: Using multiple suggest queries within the same request, each
    with a different context, and then intersecting the results client-side.
   {
     "suggest": {
       "category_suggestion": {
         "prefix": "sta",
         "completion": {
           "field": "suggest",
           "contexts": { "place_type": ["cafe"] }
         }
       },
       "location_suggestion": {
         "prefix": "sta",
         "completion": {
           "field": "suggest",
           "contexts": { "location": {"lat": 43.662, "lon": -79.380} }
         }
       }
     }
   }
  1. Implements new search pipeline processor to transform results of suggest API.

Additional context

elastic/elasticsearch#24421
elastic/elasticsearch#21291
elastic/elasticsearch#26407

@tkykenmt tkykenmt added enhancement Enhancement or improvement to existing feature or request untriaged labels Mar 25, 2025
@tkykenmt tkykenmt changed the title [Feature Request] <title> [Feature Request] Context Suggester: Add support for AND logic between multiple contexts Mar 25, 2025
@sandeshkr419
Copy link
Contributor

[Search Triage] @mingshl (Search Pipelines) @dhwanilpatel (Indexing) Do you guys have some context on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement or improvement to existing feature or request Search:Query Capabilities
Projects
Status: 🆕 New
Development

No branches or pull requests

2 participants