繁体   English   中英

按特定字段搜索时,ElasticSearch 不返回任何结果

[英]ElasticSearch returning no result when search by a particular field

我有两个字段的文档: idcode code也用作主键( _id )。 当我按id搜索时,我可以获得结果,但如果我按code搜索,则不会返回任何内容。 这是有效的,但没有理由或更改停止工作。

弹性搜索版本:7.1

文件:

{
  "id": 123,
  "code": "AAAEEXXX",
  "name": "Sample name"
}

id搜索查询有效。

{
    "query": {
        "bool": {
            "must": {
                "term": {
                    "id": 123
                }
            }
        }
    }
}

id查询的结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "test-index",
                "_type": "_doc",
                "_id": "AAAEEXXX",
                "_score": 1.0,
                "_source": {
                    "id": 123,
                    "code": "AAAEEXXX",
                    "name": "Sample name"
                }
            }
        ]
    }
}

但是,如果我按code搜索不起作用。

按代码查询:

{
    "query": {
        "bool": {
            "must": {
                "term": {
                    "code": "AAAEEXXX"
                }
            }
        }
    }
}

code查询结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

使用.keyword映射进行精确匹配,如下所示:

{
    "query": {
        "bool": {
            "must": {
                "term": {
                    "code.keyword": "AAAEEXXX"   <--
                }
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM