0

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.

surjay
  • 123
  • 1
  • 8

1 Answers1

0

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
          }
        }
      ]
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • That might be close, but it still uses the 'missing' query which has been removed in favor of 'should_not: exists', etc. That said, it seems like with all of the clauses moving to the top level that the results won't be the same. Not to mention, things that used all be nested within the filter query are now outside, so isn't that going to drastically alter the search score? – surjay Nov 17 '16 at 17:51
  • I've modified it slightly to better reflect your original query. Now the only thing affecting the score is the `must/query_string`. Everything else is in the `filter` or `must_not` parts which are not affecting the score. – Val Nov 17 '16 at 18:00
  • Ok, I think that is close. `{ "missing": { "field": "workgroup_ids" } }` needs to be replaced with something like this for 5 I believe: ` { bool: { must_not: { exists: { field: :workgroup_ids } } } } ` – surjay Nov 17 '16 at 18:38
  • Yes, `missing` should simply be replaced by a `bool/must_not/exists` query. I've updated my answer – Val Nov 18 '16 at 05:12