Java driver - Filter queries migration

I need to translate my filter / query syntax (Elasticsearch 1.4) to Elasticsearch 7.x syntax. For FilterBuilders.andFilter I found that I should use boolquery() with filter clause and it works. But what about FilterBuilders.orFilter?

FilterBuilders.boolFilter().must(nestedFilters) translated to QueryBuilders.boolQuery().must(nestedQueries)

FilterBuilders.andFilter(nestedFilters)) translated to QueryBuilders.boolQuery().filter(nestedQueries)

FilterBuilders.boolFilter().should(nestedFilters) translated to QueryBuilders.boolQuery().should(nestedQueries)

FilterBuilders.orFilter(nestedFilters) translated to ?

Thanks

The trick is to have a bool query, that does not contain anything in the must, must_not and filter parts, but only in the should clause. In that case this is considered an or... like this

DELETE test 

PUT test/_doc/1
{
  "brand" : "Nike"
}

PUT test/_doc/2
{
  "brand" : "Puma"
}

GET test/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should" : [
              { "term" : {"brand.keyword":"Nike"}},
              { "term" : {"brand.keyword":"Puma"}}
            ]
          }
        }
      ]
    }
  }
}

Thanks Alexander. My understanding is that the following clause will be scored by ES since should is executed in query context. Is this true?

"bool": {
            "should" : [
              { "term" : {"brand.keyword":"Nike"}},
              { "term" : {"brand.keyword":"Puma"}}
            ]
          }

Just run the above and check the scores... it's within the filter context already (that's why there are two bool queries).

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