首页 > 解决方案 > Elasticsearch“搜索查询词”为 _doc 类型索引中的嵌套对象键返回 0 个提示

问题描述

我在 elasticsearch 中搜索时遇到问题。每当我尝试按以下 curl 之类的术语进行搜索时,都会得到我想要的结果。

curl --location --request GET 'http://xx.xx.xx.xx:xxxx/XXXXXX/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "term": {
            "time": {
                "value": "2021-02-09T00:09:49.457Z",
                "boost": 1.0
            }
        }
    }
}
'

响应示例。该类型表示 _doc,因此嵌套查询应该以相同的方式工作。

{"hits": [
            {
                "_index": "XXXX",
                "_type": "_doc",
                "_id": "xxxxx",
                "_score": 1.0,
                "_source": { ... }
]}

但是,当我尝试搜索嵌套键时,响应中的命中率为 0。

curl --location --request GET 'http://xx.xx.xx.xx:xxxx/XXXXXX/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "term": {
            "code.type": {
                "value": "animals",
                "boost": 1.0
            }
        }
    }
}

知道可能出了什么问题吗?

标签: elasticsearchdslelasticsearch-query

解决方案


如果您确定您尝试搜索的文档存在,请确保您的字段的索引映射以及“code”字段是否是使用“code.keyword”进行关键字搜索,如果没有,请删除“boost”字段不能再次更改查询语法和策略,例如叶子查询

curl -XGET "http://xx.xx.xx.xx:xxxx/XXXXXX/_search" -H 'Content-Type: application/json' -d'{  "query": {"match": {    "code.type": "animals"  }}}'

如果您的字段是关键字

    curl -XGET "http://xx.xx.xx.xx:xxxx/XXXXXX/_search" -H 'Content-Type: application/json' -d'{  "query": {"match": {    "code.type": "animals"  }}}'

随时更新对话和评论。


推荐阅读