# BoolQueryBuilders.should goes to inside filter

**URL:** https://discuss.elastic.co/t/boolquerybuilders-should-goes-to-inside-filter/224942
**Category:** Elasticsearch
**Created:** [March 25, 2020, 5:24am UTC](https://discuss.elastic.co/t/boolquerybuilders-should-goes-to-inside-filter/224942 "2020-03-25T05:24:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ghs](https://avatars.discourse-cdn.com/v4/letter/g/96bed5/32.png) [@ghs](https://discuss.elastic.co/u/ghs)
#### Post date: [March 25, 2020, 5:24am UTC](https://discuss.elastic.co/t/boolquerybuilders-should-goes-to-inside-filter/224942/1 "2020-03-25T05:24:24Z")

</div>

I'm using ES 6.6 and I use Java RestClient  
I try to create query like this.

```auto
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
            .should(
                QueryBuilders.multiMatchQuery...
            )
            .must(
                QueryBuilders.disMaxQuery()
                    .tieBreaker(0.0f)
                    .add(//multi match)
                    .add(//multi match)
                    
            )
            .filter(
                QueryBuilders.functionScoreQuery(
                    QueryBuilders.disMaxQuery()
                        .tieBreaker(0.0f)
                        .add(//multi match)
                        .add(//multi match)
                ).setMinScore(0.5f)
            )
            .filter(
                QueryBuilders.boolQuery().mustNot(
                    JoinQueryBuilders.hasParentQuery("parent",
                        //multi match, false)
                )
            );

```

I expected JSON like this

```auto
{
  "bool" : {
    "should" : [
      {
        "multi_match" : {
          "query" : "PLACE",
          "fields" : [
            "anames.*^25.0"
          ],
          "type" : "cross_fields",
          "operator" : "OR",
          "slop" : 0,
          "prefix_length" : 0,
          "max_expansions" : 50,
          "zero_terms_query" : "NONE",
          "auto_generate_synonyms_phrase_query" : false,
          "fuzzy_transpositions" : false,
          "boost" : 1.0
        }
      }
    ],
    "must" : [
      {
        "dis_max" : {
          "tie_breaker" : 0.0,
          "queries" : [
            {
              "multi_match" : {
                "query" : "TARGET",
                "fields" : [
                  "anames.analyze*^5.0",
                  "class_name*^25.0",
                  "fname.analyze*^5.0"
                ],
                "type" : "best_fields",
                "operator" : "AND",
                "slop" : 0,
                "prefix_length" : 0,
                "max_expansions" : 50,
                "zero_terms_query" : "NONE",
                "auto_generate_synonyms_phrase_query" : false,
                "fuzzy_transpositions" : false,
                "boost" : 1.0
              }
            },
            {
              "nested" : {
                "query" : {
                  "multi_match" : {
                    "query" : "TARGET",
                    "fields" : [
                      "aliases.alias*^10.0"
                    ],
                    "type" : "best_fields",
                    "operator" : "AND",
                    "slop" : 0,
                    "prefix_length" : 0,
                    "max_expansions" : 50,
                    "zero_terms_query" : "NONE",
                    "auto_generate_synonyms_phrase_query" : false,
                    "fuzzy_transpositions" : false,
                    "boost" : 1.0
                  }
                },
                "path" : "aliases",
                "ignore_unmapped" : false,
                "score_mode" : "max",
                "boost" : 1.0
              }
            }
          ],
          "boost" : 1.0
        }
      }
    ],
    "filter" : [
      {
        "function_score" : {
          "query" : {
            "dis_max" : {
              "tie_breaker" : 0.0,
              "queries" : [
                {
                  "multi_match" : {
                    "query" : "TARGET",
                    "fields" : [
                      "anames.analyze^1.0"
                    ],
                    "type" : "best_fields",
                    "operator" : "OR",
                    "slop" : 0,
                    "prefix_length" : 0,
                    "max_expansions" : 50,
                    "zero_terms_query" : "NONE",
                    "auto_generate_synonyms_phrase_query" : false,
                    "fuzzy_transpositions" : false,
                    "boost" : 1.0
                  }
                },
                {
                  "nested" : {
                    "query" : {
                      "multi_match" : {
                        "query" : "TARGET",
                        "fields" : [
                          "aliases.alias.analyze^1.0"
                        ],
                        "type" : "best_fields",
                        "operator" : "AND",
                        "slop" : 0,
                        "prefix_length" : 0,
                        "max_expansions" : 50,
                        "zero_terms_query" : "NONE",
                        "auto_generate_synonyms_phrase_query" : false,
                        "fuzzy_transpositions" : false,
                        "boost" : 1.0
                      }
                    },
                    "path" : "aliases",
                    "ignore_unmapped" : false,
                    "score_mode" : "max",
                    "boost" : 1.0
                  }
                }
              ],
              "boost" : 1.0
            }
          },
          "functions" : [],
          "score_mode" : "multiply",
          "max_boost" : 3.4028235E38,
          "min_score" : 0.5,
          "boost" : 1.0
        }
      },
      {
        "bool" : {
          "must_not" : [
            {
              "has_parent" : {
                "query" : {
                  "multi_match" : {
                    "query" : "TARGET",
                    "fields" : [
                      "class_name*^1.0"
                    ],
                    "type" : "best_fields",
                    "operator" : "AND",
                    "slop" : 0,
                    "prefix_length" : 0,
                    "max_expansions" : 50,
                    "zero_terms_query" : "NONE",
                    "auto_generate_synonyms_phrase_query" : false,
                    "fuzzy_transpositions" : false,
                    "boost" : 1.0
                  }
                },
                "parent_type" : "parent",
                "score" : false,
                "ignore_unmapped" : false,
                "boost" : 1.0
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

```

but, actually created query is this.

```auto
{
  "bool" : {
    "must" : [
      {
        "dis_max" : {
          "tie_breaker" : 0.0,
          "queries" : [
            {
              "multi_match" : {
                "query" : "TARGET",
                "fields" : [
                  "anames.analyze*^5.0",
                  "class_name*^25.0",
                  "fname.analyze*^5.0"
                ],
                "type" : "best_fields",
                "operator" : "AND",
                "slop" : 0,
                "prefix_length" : 0,
                "max_expansions" : 50,
                "zero_terms_query" : "NONE",
                "auto_generate_synonyms_phrase_query" : false,
                "fuzzy_transpositions" : false,
                "boost" : 1.0
              }
            },
            {
              "nested" : {
                "query" : {
                  "multi_match" : {
                    "query" : "TARGET",
                    "fields" : [
                      "aliases.alias*^10.0"
                    ],
                    "type" : "best_fields",
                    "operator" : "AND",
                    "slop" : 0,
                    "prefix_length" : 0,
                    "max_expansions" : 50,
                    "zero_terms_query" : "NONE",
                    "auto_generate_synonyms_phrase_query" : false,
                    "fuzzy_transpositions" : false,
                    "boost" : 1.0
                  }
                },
                "path" : "aliases",
                "ignore_unmapped" : false,
                "score_mode" : "max",
                "boost" : 1.0
              }
            }
          ],
          "boost" : 1.0
        }
      }
    ],
    "filter" : [
      {
        "function_score" : {
          "query" : {
            "dis_max" : {
              "tie_breaker" : 0.0,
              "queries" : [
                {
                  "multi_match" : {
                    "query" : "TARGET",
                    "fields" : [
                      "anames.analyze^1.0"
                    ],
                    "type" : "best_fields",
                    "operator" : "OR",
                    "slop" : 0,
                    "prefix_length" : 0,
                    "max_expansions" : 50,
                    "zero_terms_query" : "NONE",
                    "auto_generate_synonyms_phrase_query" : false,
                    "fuzzy_transpositions" : false,
                    "boost" : 1.0
                  }
                },
                {
                  "nested" : {
                    "query" : {
                      "multi_match" : {
                        "query" : "TARGET",
                        "fields" : [
                          "aliases.alias.analyze^1.0"
                        ],
                        "type" : "best_fields",
                        "operator" : "AND",
                        "slop" : 0,
                        "prefix_length" : 0,
                        "max_expansions" : 50,
                        "zero_terms_query" : "NONE",
                        "auto_generate_synonyms_phrase_query" : false,
                        "fuzzy_transpositions" : false,
                        "boost" : 1.0
                      }
                    },
                    "path" : "aliases",
                    "ignore_unmapped" : false,
                    "score_mode" : "max",
                    "boost" : 1.0
                  }
                }
              ],
              "boost" : 1.0
            }
          },
          "functions" : [],
          "score_mode" : "multiply",
          "max_boost" : 3.4028235E38,
          "min_score" : 0.5,
          "boost" : 1.0
        }
      },
      {
        "bool" : {
          "must_not" : [
            {
              "has_parent" : {
                "query" : {
                  "multi_match" : {
                    "query" : "TARGET",
                    "fields" : [
                      "class_name*^1.0"
                    ],
                    "type" : "best_fields",
                    "operator" : "AND",
                    "slop" : 0,
                    "prefix_length" : 0,
                    "max_expansions" : 50,
                    "zero_terms_query" : "NONE",
                    "auto_generate_synonyms_phrase_query" : false,
                    "fuzzy_transpositions" : false,
                    "boost" : 1.0
                  }
                },
                "parent_type" : "parent",
                "score" : false,
                "ignore_unmapped" : false,
                "boost" : 1.0
              }
            }
          ],
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "should" : [
      {
        "multi_match" : {
          "query" : "PLACE",
          "fields" : [
            "anames.*^25.0"
          ],
          "type" : "cross_fields",
          "operator" : "OR",
          "slop" : 0,
          "prefix_length" : 0,
          "max_expansions" : 50,
          "zero_terms_query" : "NONE",
          "auto_generate_synonyms_phrase_query" : false,
          "fuzzy_transpositions" : false,
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

```

these two queries search different results.

**should** query goes to inside of filter.

---

<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: [April 22, 2020, 5:24am UTC](https://discuss.elastic.co/t/boolquerybuilders-should-goes-to-inside-filter/224942/2 "2020-04-22T05:24:36Z")

</div>

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