ElasticSearch:雪球无法正常工作?

时间:2014-01-13 15:57:03

标签: elasticsearch query-string stemming snowball

我构建以下内容:

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。为什么这不起作用?请注意,我没有兴趣在搜索中添加模糊性,因为默认情况下它会匹配灰色(但不会变灰)。

我在这里做错了什么?

1 个答案:

答案 0 :(得分: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