# Do elasticsearch doument participate in all OR conditions in bool query.?

**URL:** https://discuss.elastic.co/t/do-elasticsearch-doument-participate-in-all-or-conditions-in-bool-query/133031
**Category:** Elasticsearch
**Created:** [May 23, 2018, 5:12pm UTC](https://discuss.elastic.co/t/do-elasticsearch-doument-participate-in-all-or-conditions-in-bool-query/133031 "2018-05-23T17:12:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![san88922](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@san88922](https://discuss.elastic.co/u/san88922)
#### Post date: [May 23, 2018, 5:12pm UTC](https://discuss.elastic.co/t/do-elasticsearch-doument-participate-in-all-or-conditions-in-bool-query/133031/1 "2018-05-23T17:12:34Z")

</div>

I am new to Elasticsearch, And we are working on a requirement,where document in the elasticsearch will be fetched based on the **match types** like **fuzzyMatch,Wordmatch** etc...

We are facing performance issues here, when document is matched for one match type, document should come out of the **"bool"** query, but it is participating in all match types and giving me the all match types result, I tried **nested bool query** to make it participate in single match types and exit when matched else move to next match types.

I tried following this blog.

> **[Boolean Operations and Filters in the Bool Query in Elasticsearch](https://www.elastic.co/blog/lost-in-translation-boolean-operations-and-filters-in-the-bool-query)**
>
> Some query types deprecated in 2.x will be removed in 5.0. Many of them are replaced by functionality of the bool query, so here’s a quick guide on bool query.

Even this is giv me the same Result..

Please find the below sample code.

```
 {
  "size": 10000,
  "query": {
    "function_score": {
      "score_mode": "sum",
      "functions": [
        {
          "filter": {
            "bool": {
              "should": [
                {
                  "bool": {
                    "should": [
                      {
                        "match": {
                          "4": {
                            "fuzziness": "AUTO",
                            "query": "kavan",
                            "minimum_should_match": "50%",
                            "_name": "4.fuzzyMatch"
                          }
                        }
                      },
                      {
                        "match": {
                          "4": {
                            "query": "kavan",
                            "minimum_should_match": "50%",
                            "_name": "4.wordMatch"
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "bool": {
                    "should": [
                      {
                        "match": {
                          "4.subStringMatch": {
                            "query": "kavan",
                            "minimum_should_match": "50%",
                            "_name": "4.subStringMatch"
                          }
                        }
                      },
                      {
                        "match": {
                          "4.phoneticMatch": {
                            "query": "kavan",
                            "minimum_should_match": "50%",
                            "_name": "4.phoneticMatch"
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "match": {
                    "4.exactMatch": "kavan"
                  }
                }
              ]
            }
          },
          "weight": 50
        }
      ]
    }
  }
}

```

Actual output is:

```
"hits": {
    "total": 94,
    "max_score": 50.0,
    "hits": [{
      "_index": "6_1028",
      "_type": "6_1028",
      "_id": "14",
      "_score": 50.0,
      "_source": {
        "1": 14,
        "@timestamp": "2018-05-18T06:57:02.540Z",
        "4": "kavana",
        "6": "Vastrad",
        "7": "Indiranagar",
        "@version": "1",
        "type": "1028",
        "10": "Bangalore"
      },
      "matched_queries": ["4.phoneticMatch", "4.fuzzyMatch", "4.subStringMatch"]
    }, {
      "_index": "6_1028",
      "_type": "6_1028",
      "_id": "52",
      "_score": 50.0,
      "_source": {
        "1": 52,
        "@timestamp": "2018-05-18T06:57:02.559Z",
        "4": "kavana",
        "6": "Vastrad",
        "7": "Indiranagar",
        "@version": "1",
        "type": "1028",
        "10": "Bangalore"
      },
      "matched_queries": ["4.phoneticMatch", "4.fuzzyMatch", "4.subStringMatch"]
    }, {
      "_index": "6_1028",
      "_type": "6_1028",
      "_id": "53",
      "_score": 50.0,
      "_source": {
        "1": 53,
        "@timestamp": "2018-05-18T06:57:02.559Z",
        "4": "kavana",
        "6": "Vastrad",
        "7": "Indiranagar",
        "@version": "1",
        "type": "1028",
        "10": "Bangalore"
      },
      "matched_queries": ["4.phoneticMatch", "4.fuzzyMatch", "4.subStringMatch"]
    }
}

```

List item

Expected output is:

```
     "hits": {
        "total": 94,
        "max_score": 50.0,
        "hits": [{
          "_index": "6_1028",
          "_type": "6_1028",
          "_id": "14",
          "_score": 50.0,
          "_source": {
            "1": 14,
            "@timestamp": "2018-05-18T06:57:02.540Z",
            "4": "kavana",
            "6": "Vastrad",
            "7": "Indiranagar",
            "@version": "1",
            "type": "1028",
            "10": "Bangalore"
          },
          "matched_queries": ["4.phoneticMatch"]
        }

```

With only one match type in **matched\_queries**

Note : Match types can be seen in **matched\_queries** block.

Any suggestions in this regard are appreciated..

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [May 23, 2018, 7:35pm UTC](https://discuss.elastic.co/t/do-elasticsearch-doument-participate-in-all-or-conditions-in-bool-query/133031/2 "2018-05-23T19:35:41Z")

</div>

Is the issue that the query is slow? Or that too many documents are matching the query?

If the query is too slow, I'd recommend not using the Function Score query. You aren't using any of the functions of the function score, so the entire assembly of boolean queries can be expressed without the function\_score. Which will be much faster.

If your query is too permissive -- it matches too many documents -- you need to change how the query is constructed. Right now you have a large amount of `should` clauses, which are basically optional clauses. A document can match one or more of the clauses, but may match multiple.

---

<div class="post-metadata">

### Author: ![san88922](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@san88922](https://discuss.elastic.co/u/san88922)
#### Post date: [May 24, 2018, 2:36am UTC](https://discuss.elastic.co/t/do-elasticsearch-doument-participate-in-all-or-conditions-in-bool-query/133031/3 "2018-05-24T02:36:06Z")

</div>

yes @polyfractal We are seeing performance issue, we are using functions of the function score that i have not posted here, Problem is each document is performing all matches .i.e its going through all the match clauses, which is a overload, we want only one match type in the **should clause** to appear in the result, so document should not participate in the remaining match clause when it find one true match clause.

Can you please suggest a solution, where match clauses in the **should clause** are executed on condition?

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [May 24, 2018, 2:37pm UTC](https://discuss.elastic.co/t/do-elasticsearch-doument-participate-in-all-or-conditions-in-bool-query/133031/4 "2018-05-24T14:37:25Z")

</div>

If you only want the first matching query to apply, you can set the function\_score's `score_mode` to `first`. You'll need to re-arrange so that there is a single list of filters, instead of the nested `boolean` setup you have now.

There is no way to tell a `boolean` query to only match the first clause. They are not designed to do that... they will try to match all the clauses according to the boolean logic (must/must\_not/should).

Note: if you only want the first to match purely for performance reasons, I think this is misguided. Internally, Lucene uses a leapfrog iterator model, where the sparsest query clause iterates forward, the next query advances it's iterator to the same or farther position, etc. Only when all the iterators line up is a match recorded.

So by attempting to only allow one query to match, you may be making performance worse.

If you don't need that behavior for functional reasons, I would suggest just removing the `function_score` entirely. It is much slower than a simple `bool` query.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [June 21, 2018, 2:37pm UTC](https://discuss.elastic.co/t/do-elasticsearch-doument-participate-in-all-or-conditions-in-bool-query/133031/5 "2018-06-21T14:37:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
