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"
        }
      }
    ]
  }
}

... greygreyed、およびの 3 つのヒットが予想されgreyingます。なぜこれが機能しないのですか?検索にあいまいさを追加することに興味がないことに注意してください。これは、デフォルトで灰色に一致するためです(ただし、灰色にはなりません)。

私がここで間違っていることは何ですか?

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 を検索するこの方法を好みます。この場合、マッチを使用してみてください:

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 に答える