elasticsearch查询没有结果

n53p2ov0  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(366)

我是elasticsearch的新手,在查询时遇到了一些问题。我似乎无法基于“userid”字段执行查询。这是我索引的Map。

{
    "testindex" : {
       "mappings" : {
          "properties" : {
             "timestamp" : {
                "type" : "date"
             },
             "content" : {
                "fields" : {
                   "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                   }
                },
                "type" : "text"
             },
             "userid" : {
                "fields" : {
                   "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                   }
                },
                "type" : "text"
             }
          }
       },
       "aliases" : {},
       "settings" : {
          "index" : {
             "creation_date" : "1605323501230",
             "number_of_shards" : "1",
             "number_of_replicas" : "1",
             "version" : {
                "created" : "7090399"
             },
             "uuid" : "<removed>",
             "provided_name" : "testindex"
          }
       }
    }
 }

这里是我的集群中的一个文档,当通过文档id检索时。

{
    "_index":"testindex",
    "_type":"_doc",
    "_id":"d757422a-acf4-4ab0-89f5-bb79b2e2c699",
    "_version":1,
    "_seq_no":1,
    "_primary_term":1,
    "found":True,
    "_source":{
       "userid":"9fe3ba41-780f-448d-8c99-c0440a7ba3f0",
       "content":"testing, testing 12",
       "timestamp":"2020-11-15T05:33:06.615631+00:00"
    }
 }

下面是一个基于userid字段的查询。

{
   "query":{
      "bool":{
         "should":{
            "term":{
               "userid":"9fe3ba41-780f-448d-8c99-c0440a7ba3f0"
            }
         }
      }
   }
}

我没有得到任何结果,解释输出也没有帮助。

{
   "_index":"testindex",
   "_type":"_doc",
   "_id":"d757422a-acf4-4ab0-89f5-bb79b2e2c699",
   "matched":False,
   "explanation":{
      "value":0.0,
      "description":"no matching term",
      "details":[

      ]
   }
}

我做错什么了?

gzszwxb4

gzszwxb41#

这个 term 查询正在查找您需要使用的完全匹配项 keyword 领域
搜索查询:

{
   "query":{
      "bool":{
         "should":{
            "term":{
               "userid.keyword":"9fe3ba41-780f-448d-8c99-c0440a7ba3f0"
            }
         }
      }
   }
}

搜索结果:

"hits": [
      {
        "_index": "64845089",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "userid": "9fe3ba41-780f-448d-8c99-c0440a7ba3f0",
          "content": "testing, testing 12",
          "timestamp": "2020-11-15T05:33:06.615631+00:00"
        }
      }
    ]

使用匹配查询搜索查询:
这个 userid 字段很可能用 standard 将生成以下令牌的分析器:

{
  "tokens": [
    {
      "token": "9fe3ba41",
      "start_offset": 0,
      "end_offset": 8,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "780f",
      "start_offset": 9,
      "end_offset": 13,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "448d",
      "start_offset": 14,
      "end_offset": 18,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "8c99",
      "start_offset": 19,
      "end_offset": 23,
      "type": "<ALPHANUM>",
      "position": 3
    },
    {
      "token": "c0440a7ba3f0",
      "start_offset": 24,
      "end_offset": 36,
      "type": "<ALPHANUM>",
      "position": 4
    }
  ]
}

所以你可以用上面的 term 查询完全匹配,或者您可以使用 match 查询如下图所示

{
       "query":{
          "bool":{
             "should":{
                "match":{
                   "userid":"9fe3ba41-780f-448d-8c99-c0440a7ba3f0"
                }
             }
          }
       }
    }

相关问题