1

我构建以下内容:

curl -XDELETE "http://localhost:9200/testindex"
curl -XPOST "http://localhost:9200/testindex" -d'
{
  "mappings" : {
    "article" : {
      "dynamic" : false,
      "properties" : {
            "text" : {
              "type" : "string",
          "analyzer" : "snowball"
        }
      }
    }
  }
}'

...我填充以下内容:

curl -XPUT "http://localhost:9200/testindex/article/1" -d'{"text": "grey"}'
curl -XPUT "http://localhost:9200/testindex/article/2" -d'{"text": "gray"}'
curl -XPUT "http://localhost:9200/testindex/article/3" -d'{"text": "greyed"}'
curl -XPUT "http://localhost:9200/testindex/article/4" -d'{"text": "greying"}'

...我在搜索时看到以下内容:

curl -XPOST "http://localhost:9200/testindex/_search" -d'
{
     "query": {
         "query_string": {
             "query": "grey",
             "analyzer" : "snowball"
         }
     }
}'

结果是

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.30685282,
    "hits": [
      {
        "_index": "testindex",
        "_type": "article",
        "_id": "1",
        "_score": 0.30685282,
        "_source": {
          "text": "grey"
        }
      }
    ]
  }
}

...我期待 3 次点击:greygreyedgreying. 为什么这不起作用?请注意,我对在搜索中添加模糊性不感兴趣,因为默认情况下它将匹配灰色(但不是灰色)。

我在这里做错了什么?

4

1 回答 1

3

您的问题是您使用的是 query_string 而不是定义 default_field,因此它正在搜索使用您的默认分析器(最有可能是标准)的 _all 字段。

要解决此问题,请执行以下操作:

curl -XPOST "http://localhost:9200/testindex/_search" -d'
{
     "query": {
         "query_string": {
             "default_field": "text",
             "query": "grey"}
         }
     }
}'

{"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":0.30685282,"hits":[{"_index":"testindex","_type":"article","_id":"4","_score":0.30685282, "_source" : {"text": "greying"}},{"_index":"testindex","_type":"article","_id":"1","_score":0.30685282, "_source" : {"text": "grey"}},{"_index":"testindex","_type":"article","_id":"3","_score":0.30685282, "_source" : {"text": "greyed"}}]}}

我尽量远离 query_string 搜索,除非我真的无法避免。有时,来自 solr 的人喜欢这种通过搜索 dsl 进行搜索的方法。在这种情况下,请尝试使用 match:

curl -XPOST "http://localhost:9200/testindex/_search" -d'
> {
>      "query": {
>          "match": {
>              "text": "grey"
>          }
>      }
> }'
{"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":0.30685282,"hits":[{"_index":"testindex","_type":"article","_id":"4","_score":0.30685282, "_source" : {"text": "greying"}},{"_index":"testindex","_type":"article","_id":"1","_score":0.30685282, "_source" : {"text": "grey"}},{"_index":"testindex","_type":"article","_id":"3","_score":0.30685282, "_source" : {"text": "greyed"}}]}}

但无论哪种方式都会产生正确的结果。

有关 query_string,请参阅此处的文档:

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

于 2014-01-13T16:16:55.677 回答