# Query search based on condition (Elasticsearch v6.84) - please advice

**URL:** <https://discuss.elastic.co/t/query-search-based-on-condition-elasticsearch-v6-84-please-advice/213611>\
**Category:** Elasticsearch\
**Created:** [January 2, 2020, 6:19pm UTC](https://discuss.elastic.co/t/query-search-based-on-condition-elasticsearch-v6-84-please-advice/213611 "2020-01-02T18:19:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chetan\_Ramaiah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chetan_ramaiah/32/48571_2.png) [@Chetan\_Ramaiah](https://discuss.elastic.co/u/Chetan_Ramaiah)\
**Post date:** [January 2, 2020, 6:19pm UTC](https://discuss.elastic.co/t/query-search-based-on-condition-elasticsearch-v6-84-please-advice/213611/1 "2020-01-02T18:19:57Z")

</div>

Hi,

Searching based on multiple fields such as name, country, company, 30days\_flag, 60days\_flag, 90days\_flag. I am trying to fetch all companies which took part in a survey from the last 30, 60 and 90 days from the specified countries.

If companies have taken part in a survey with below range

(\> 90 days) then we store 'Y' for 90days\_flag and 'N' for 30days\_flag and 60days\_flag field. so user will not get result if they search for 30 days or 60 days.

(\< 90 days and \> 60 days) then we store 'Y' for 90days\_flag and 60days\_flag (reason if user searches for 90 days then user is searching for a range between 1 to 90 days). And 'N' for 30days\_flag field. i.e., user will not get result if they search for 30 days.

(\< 90 days and \< 60 days and \> 30 days) then we store 'Y' for 90days\_flag, 60days\_flag and 30days\_flag (reason if user searches for 90 days then user is searching for a range between 1 to 90 days).

Below is the JSON:

```
       {
      "bool" : {
        "must" : [
          {
            "multi_match" : {
              "query" : "dan",
              "fields" : [
                "email^1.0",
                "name^1.0"
              ],
              "type" : "best_fields",
              "operator" : "OR",
              "analyzer" : "standard",
              "slop" : 0,
              "fuzziness" : "AUTO",
              "prefix_length" : 0,
              "max_expansions" : 50,
              "zero_terms_query" : "NONE",
              "auto_generate_synonyms_phrase_query" : true,
              "fuzzy_transpositions" : true,
              "boost" : 1.0
            }
          }
        ],
        "filter" : [
          {
            "terms" : {
              "comp_name.keyword" : [
                "Coodo"
              ],
              "boost" : 1.0
            }
          },
          {
            "terms" : {
              "countries" : [
                "Switzerland"
              ],
              "boost" : 1.0
            }
          },
          {
            "multi_match" : {
              "query" : "Y",
              "fields" : [
                "ninty_day_flag^1.0",
                "sixty_day_flag^1.0",
                "thirty_day_flag^1.0"
              ],
              "type" : "best_fields",
              "operator" : "OR",
              "slop" : 0,
              "prefix_length" : 0,
              "max_expansions" : 50,
              "zero_terms_query" : "NONE",
              "auto_generate_synonyms_phrase_query" : true,
              "fuzzy_transpositions" : true,
              "boost" : 1.0
            }
          }
        ],
        "adjust_pure_negative" : true,
        "boost" : 1.0
      }
    }

```

Above json works if all 3 flags are 'Y' and fails to return data if any of the field is 'N'. But, our requirement is to get result even if any of the flag is 'N'.

Let's say 90days\_flag = 'Y', 60days\_flag = 'Y' and 30days\_flag = 'N', I should be able to get data for those 90 and 60 days. and one more scenario, let's say 90days\_flag = 'Y', 60days\_flag = 'N' and 30days\_flag = 'N', then I should be able to get data for those 90 days only.

Please advice.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Emanuil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emanuil/32/36783_2.png) [@Emanuil](https://discuss.elastic.co/u/Emanuil)\
**Post date:** [January 3, 2020, 12:51am UTC](https://discuss.elastic.co/t/query-search-based-on-condition-elasticsearch-v6-84-please-advice/213611/2 "2020-01-03T00:51:23Z")

</div>

Is `ninty_day_flag` from filter.multi\_match.fields a copy/paste typo or an actual spelling error in the query? (missing `e`)

---

<div class="post-metadata">

**Author:** ![Chetan\_Ramaiah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chetan_ramaiah/32/48571_2.png) [@Chetan\_Ramaiah](https://discuss.elastic.co/u/Chetan_Ramaiah)\
**Post date:** [January 3, 2020, 1:31am UTC](https://discuss.elastic.co/t/query-search-based-on-condition-elasticsearch-v6-84-please-advice/213611/3 "2020-01-03T01:31:26Z")

</div>

Hi Emanuil, it's actual spelling error but it's used consistently through out the application 🙂

---

<div class="post-metadata">

**Author:** ![Emanuil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/emanuil/32/36783_2.png) [@Emanuil](https://discuss.elastic.co/u/Emanuil)\
**Post date:** [January 4, 2020, 1:33am UTC](https://discuss.elastic.co/t/query-search-based-on-condition-elasticsearch-v6-84-please-advice/213611/4 "2020-01-04T01:33:30Z")

</div>

Ah, one of those situations, I sympathise 😄.

Apologies for going straight for another question, I just had the idea. Any reason why you don't store when a company took the survey as a date and time? Then you can simply use a range query rather than this set of flags. You [can even do convenient date math](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html#ranges-on-dates) with that query. You can express "greater than 90 days", "between 60 and 90", "between 30 and 60" and "under 30" with it and get rid of the flags entirely.

If that is not possible, take a look [at this warning](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#operator-min). You're using `"type" : "best_fields"` in the `multi_match` filter which means that the match is evaluated on a per field basis - likely not what you want, as the warning says. For a quick fix, try changing the `multi_match` filter to use [cross\_fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-cross-fields) instead of `best_fields`. I still think a Range query would be superior if you can start recording the survey date.

---

<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:** [February 1, 2020, 1:47am UTC](https://discuss.elastic.co/t/query-search-based-on-condition-elasticsearch-v6-84-please-advice/213611/5 "2020-02-01T01:47:34Z")

</div>

Closed on February 1, 2020, 1:47am UTC

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