Elasticsearch在匹配查询中提供了不必要的记录

时间:2017-11-10 06:36:50

标签: elasticsearch

我想获得dateofbirth有子串“-11-09”的所有文件。

这是我的弹性搜索查询:

{ "query": { "bool" : { "must": { "match": { "dobdata": ".*-11-09.*"} } } } }

我得到的结果是

{
 "took": 8,
 "timed_out": false,
 "_shards": {
  "total": 5,
  "successful": 5,
  "failed": 0
 },
 "hits": {
  "total": 4,
  "max_score": 5.0782137,
  "hits": [
   {
     "_index": "userindexv1",
     "_type": "usertype",
     "_id": "58f9a9d1acf8c47037000038",
     "_score": 5.0782137,
     "_source": {
      "fullname": "Eshwar ",
      "fullname_raw": "Eshwar ",
      "mobile1": "7222222256",
      "uid": "UIDS1010",
      "mobile2": "",
      "classname": "Class 5",
      "classname_raw": "Class 5",
      "divid": 63,
      "category": "S",
      "dobdata": "2010-11-09"
     }
   },
   {
    "_index": "userindexv1",
    "_type": "usertype",
    "_id": "57960b35acf8c4c43000002c",
    "_score": 1.259227,
    "_source": {
     "fullname": "Sindhu ",
     "fullname_raw": "Sindhu ",
     "mobile1": "9467952335",
     "uid": "UIDS1006",
     "mobile2": "",
     "classname": "class 1s Group for class g",
     "classname_raw": "class 1s Group for class g",
     "divid": 63,
     "category": "S",
     "dobdata": "2012-11-08"
    }
   },
   {
    "_index": "userindexv1",
    "_type": "usertype",
    "_id": "58eb62d2acf8c4d43300002f",
    "_score": 1.1471639,
    "_source": {
     "fullname": "Himanshu ",
     "fullname_raw": "Himanshu ",
     "mobile1": "9898785484",
     "uid": "",
     "mobile2": "",
     "classname": "Play Group",
     "classname_raw": "Play Group",
     "divid": 63,
     "category": "S",
     "dobdata": "2012-11-08"
    }
   },
   {
    "_index": "userindexv1",
    "_type": "usertype",
    "_id": "580dbe5bacf8c4b82300002a",
    "_score": 1.1471639,
    "_source": {
     "fullname": "Sai Bhargav ",
     "fullname_raw": "Sai Bhargav ",
     "mobile1": "9739477159",
     "uid": "",
     "mobile2": "7396226318",
     "classname": "class 1s Group for class g",
     "classname_raw": "class 1s Group for class g",
     "divid": 63,
     "category": "S",
     "dobdata": "2012-11-07"
    }
   }
 ]
}}

我正在获取dateofbirth不包含字符串“-11-09”的记录。我试图解决它。我无法找到灵魂。

我是elasticsearch的新手。我只想要第一张唱片。任何人都可以帮助我。抱歉英文不好。

3 个答案:

答案 0 :(得分:1)

即使我遇到同样的问题,我也做了两件事来解决它。 1.将出生日期的格式从Y-m-d更改为YMd,并将索引设为not_analyzed。 2.使用通配符查询而不是匹配查询

{
"query": {
    "wildcard": {
       "dobdata": {
          "value": "*Nov09*"
       }
    }
  }
}

它解决了我的问题。希望这也能解决你的问题。

答案 1 :(得分:0)

要获取月份日期= 11-09的结果,请使用以下查询。 dobdata的映射是

   border-left: 160px solid transparent;
    border-right: 160px solid transparent;
    width: 84%;

查询是:

"dobdata": {
        "type": "text",

        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }

同样使用多字段映射而不是_raw字段请参阅here

答案 2 :(得分:0)

@K Sathish所以你使用elasticsearch 2.x?日期的字符串类型不那么舒适。我建议您在日期类型中更改字符串中的数据类型,这样您也可以获得范围查询。在日期类型中更改后,您可以通过以下方式在弹性2.x中进行查询:

{
  "query": {
    "filtered": {
      "filter": {
        "script": {
          "script": "doc.dobdata.date.monthOfYear == 11 && doc.dobdata.date.dayOfMonth == 9"
        }
      }
    }
  }
}