【问题标题】:Elasticsearch is giving unnecessary records on match queryElasticsearch 在匹配查询中给出了不必要的记录
【发布时间】:2017-11-13 20:43:47
【问题描述】:

我想获取所有出生日期包含子字符串“-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"
    }
   }
 ]
}}

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

我是弹性搜索的新手。我只想要第一张唱片。谁能帮帮我。抱歉英语不好。

【问题讨论】:

标签: elasticsearch


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    查询是:

    curl -XGET "http://localhost:9200/userindexv1/_search" -H 'Content-Type: 
    application/json' -d'
    {
      "query": {
        "wildcard": {
          "dobdata.keyword":"*-11-09*"
        }
      }
    }'
    

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-29
        • 2012-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-20
        • 1970-01-01
        • 2016-08-19
        相关资源
        最近更新 更多