Search code examples
elasticsearchelasticsearch-railselasticsearch-5

Having trouble converting a filtered query into a bool query for Elasticsearch 5


I'm in the process of upgrading from elasticsearch 1.7 to 5.0. One of the changes is the removal of filtered query in favor of a bool query. For example, I have this search hash being used in the older version of ES:

{
 "sort": [ { "updated_at" => "desc" } ],
 "query": {
          "filtered": {
                      "filter": {
                                "bool": {
                                    "must": [
                                                { "term": { "account_id" => 2 } },
                                                { "bool": {
                                                            "should": [
                                                                        { "missing": { "field": "workgroup_ids" } },
                                                                        { "term": { "visibility": 2 } }
                                                                      ]
                                                          }
                                                }
                                            ],
                                            "must_not": [
                                                          { "range": { "expires_at": { "lte": "2016-11-17T16:27:22Z" } } },
                                                          { "range": { "published_at": { "gte": "2016-11-17T16:27:22Z" } } }
                                                        ]
                                        }
                                },
                      "query": {
                                "bool": {
                                          "must": [
                                                    { "query_string": { "query": "name:(*orang.jpg*)" } }
                                                  ]
                                        }
                              }
                      }
          }}

So, I know this needs to be more in the format of:

{ "query": "bool": [{ term: { account_id: 2 } }]} 

I also know the missing query needs to be replaced with an must_not exists query in 5. That said, I'm having trouble figuring out how to convert this heavily nested hash so does anybody know how I would structure this correctly for ES5?

I'm also using/accessing ES in Rails utilizing the elasticsearch-rails gem, fyi.


Solution

  • I think you can simply change it to this and it should work:

    {
      "sort": [
        {
          "updated_at": "desc"
        }
      ],
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "query": "name:(*orang.jpg*)"
              }
            }
          ],
          "must_not": [
            {
              "range": {
                "expires_at": {
                  "lte": "2016-11-17T16:27:22Z"
                }
              }
            },
            {
              "range": {
                "published_at": {
                  "gte": "2016-11-17T16:27:22Z"
                }
              }
            }
          ],
          "filter": [
            {
              "bool": {
                "minimum_should_match": 1,
                "should": [
                  {
                    "bool": {
                      "must_not": {
                        "exists": {
                          "field": "workgroup_ids"
                        }
                      }
                    }
                  },
                  {
                    "term": {
                      "visibility": 2
                    }
                  }
                ]
              }
            },
            {
              "term": {
                "account_id": 2
              }
            }
          ]
        }
      }
    }