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

Question: Is it possible to allow minimum_should_match on all OR operations? #45

Closed
ztawil opened this issue Jan 9, 2020 · 2 comments
Closed

Comments

@ztawil
Copy link

ztawil commented Jan 9, 2020

Is it possible to force having a minimum_should_match value on any bool values when an OR operation is present?

Currently I have something like:

search_content = "(a OR b) AND (b OR c)"
tree = parser.parse(search_content)
query = ES_BUILDER(tree)

And the query yields:

{
  "bool": {
    "must": [
      {
        "bool": {
          "should": [
            {
              "match": {
                "content": {
                  "query": "a",
                  "zero_terms_query": "none"
                }
              }
            },
            {
              "match": {
                "content": {
                  "query": "b",
                  "zero_terms_query": "none"
                }
              }
            }
          ]
        }
      },
      {
        "bool": {
          "should": [
            {
              "match": {
                "content": {
                  "query": "b",
                  "zero_terms_query": "none"
                }
              }
            },
            {
              "match": {
                "content": {
                  "query": "c",
                  "zero_terms_query": "none"
                }
              }
            }
          ]
        }
      }
    ]
  }
}

Whereas I'd like it to be:

{
  "bool": {
    "must": [
      {
        "bool": {
          "minimum_should_match": 1,
          "should": [
            {
              "match": {
                "content": {
                  "query": "a",
                  "zero_terms_query": "none"
                }
              }
            },
            {
              "match": {
                "content": {
                  "query": "b",
                  "zero_terms_query": "none"
                }
              }
            }
          ]
        }
      },
      {
        "bool": {
          "minimum_should_match": 1,
          "should": [
            {
              "match": {
                "content": {
                  "query": "b",
                  "zero_terms_query": "none"
                }
              }
            },
            {
              "match": {
                "content": {
                  "query": "c",
                  "zero_terms_query": "none"
                }
              }
            }
          ]
        }
      }
    ]
  }
}

But perhaps I'm doing something wrong?

@ztawil ztawil changed the title Question: Force minimum_should_match on all OR operations Question: Is it possible to allow minimum_should_match on all OR operations? Jan 9, 2020
@wouterweerkamp
Copy link

Given that both bool (OR) queries only have should clauses, Elasticsearch uses a minimum_should_match of 1 by default. Check https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#bool-min-should-match

If the bool query includes at least one should clause and no must or filter clauses, the default value is 1. Otherwise, the default value is 0.

So the expected output you gave is in practice the same as the one that is generated.

@alexgarel
Copy link
Member

Hi @ztawil as told by @wouterweerkamp you probably don't need minimum_should_match. However I added some support for passing options to operations. If you need it, you should subclass ElasticsearchQueryBuilder and specialize _should_operation to pass options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants