Elasticsearch connect range and term to same array item

假如想象 提交于 2019-12-13 22:18:43

问题


I have a user document with a field called experiences which is an array of objects, like:

{
  "experiences": [
    {
      "end_date": "2017-03-02",
      "is_valid": false
    },
    {
      "end_date": "2015-03-02",
      "is_valid": true
    }
  ]
}

With this document I have to search users where end date is in last year and is_valid is true. At this time I have a query -> bool and I add two must there, one range for the end_date and one term for the is_valid.

{
  "query": {
    "bool": {
      "must": {
        "term": {
          "experiences.is_valid": true
        },
        "range": {
          "experiences.end_date": {
            "gte": "now-1y",
            "lte": "now"
          }
        },
      }
    }
  }
}

The result is that this user is selected because he has an end_date in the last year (the first exp.) and another exp. with is_valid true. Of course this is not what I need, because I need that end_date and is_valid must be referenced to the same object, but how can we do this on Elasticsearch?

Mapping:

"experiences": {
  "properties": {
    "comment": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "end_date": {
      "type": "date"
    },
    "id": {
      "type": "long"
    },
    "is_valid": {
      "type": "boolean"
    },
    "start_date": {
      "type": "date"
    }
  }
}

回答1:


You need to change experiences type to Nested data type.

Then apply nested query :

 {
 "query": {
  "nested": {
     "path": "experiences",
     "query": {
        "bool": {
           "must": [
              {
                 "term": {
                    "experiences.is_valid": true
                 }
              },
              {
                 "range": {
                    "experiences.end_date": {
                       "gte": "now-1y",
                       "lte": "now"
                    }
                 }
              }
           ]
        }
     }
   }
  }
 }

This is due to the way arrays of objects are flattened in Elasticsearch. Study more here



来源:https://stackoverflow.com/questions/44266252/elasticsearch-connect-range-and-term-to-same-array-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!