简体   繁体   English

弹性词查询的意外结果

[英]Unexpected result of Elastic term query

I have Elastic 2.4 running on http://localhost:9200 only for test. 我只有在http:// localhost:9200上运行的Elastic 2.4才用于测试。

Setup 设定

As fresh start, I created 1 and only 1 item in the index. 作为新的开始,我在索引中创建了1个项目,但只有1个项目。

$ curl -s -XPUT "http://localhost:9200/movies/movie/1" -d'
{
    "title": "The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972,
    "genres": ["Crime", "Drama"]
}'

Returns 退货

{"_index":"movies","_type":"movie","_id":"1","_version":3,"_shards":{"total":2,"successful":1,"failed":0},"created":false}

I then run this command to confirm the index works: 然后,我运行以下命令以确认索引有效:

$ curl -s -XPOST "http://localhost:9200/movies/_search" -d'
{
    "query": {
        "query_string": {
            "query": "Godfather"
        }
    }
}'

Returns 退货

{"took":8,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.095891505,"hits":[{"_index":"movies","_type":"movie","_id":"1","_score":0.095891505,"_source":
{
    "title": "The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972,
    "genres": ["Crime", "Drama"]
}}]}}

The Problem 问题

I tried to run term query like this: 我试图像这样运行术语查询:

$ curl -s -XPOST "http://localhost:9200/movies/_search" -d'
{
    "query": {
        "term": {"title": "The Godfather"}
    }
}'

I was expected to get 1 result, instead I got this: 我应该得到1个结果,相反我得到了:

{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

What did I got wrong? 我怎么了

Either match_phrase like jay suggested or you need to create a not_analyzed sub-field (eg title.raw ), like this: 像杰伊建议的match_phrase或者您需要创建一个not_analyzed子字段(例如title.raw ),如下所示:

$ curl -s -XPUT "http://localhost:9200/movies/_mapping/movie" -d'
{
    "properties": {
        "title": {
            "type": "string",
            "fields": {
               "raw": {
                   "type": "string",
                   "index": "not_analyzed"
               }
            }
        }
    }
}'

Then you can reindex your document to populate the title.raw : 然后,您可以为文档重新索引以填充title.raw

$ curl -s -XPUT "http://localhost:9200/movies/movie/1" -d'
{
    "title": "The Godfather",
    "director": "Francis Ford Coppola",
    "year": 1972,
    "genres": ["Crime", "Drama"]
}'

And finally, your term query will work on the title.raw sub-field: 最后,您的字词查询将在title.raw子字段上工作:

$ curl -s -XPOST "http://localhost:9200/movies/_search" -d'
{
    "query": {
        "term": {"title.raw": "The Godfather"}
    }
}'

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

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