2014-01-13 32 views
1

我建立了以下內容:ElasticSearch:雪球不工作?

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次點擊:grey,greyed和​​。爲什麼這不起作用?請注意,我對將搜索添加模糊不感興趣,因爲默認情況下匹配灰色(但不灰色)。

我在做什麼錯在這裏?

回答

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的方法。在這種情況下,嘗試使用匹配:

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

+0

完美 - 感謝這個! –