2017-03-27 5 views
1

私はインデックスを持っていると仮定すると、私はこの文を使用して、いくつかのドキュメントを追加しました:ElasticsearchのQUERY_STRING - 正確なフレーズ問題

POST test/item/_bulk 
{"id": 1, "text": "one two"} 
{"id": 2, "text": "one two three"} 
{"id": 3, "text": "three one two"} 
{"id": 4, "text": "three one two four"} 
{"id": 5, "text": "one two|"} 
{"id": 6, "text": "|one two"} 
{"id": 7, "text": "|one two|"} 
{"id": 8, "text": "one|two"} 
{"id": 9, "text": "one| two"} 
{"id": 10, "text": "one |two"} 
{"id": 11, "text": "one | two"} 

私はこの検索をしたい:

GET test/item/_search 
{ 
    "query": 
    { 
     "query_string": 
     { 
      "query": "\"one two\"", 
      "fields": ["text"], 
      "analyze_wildcard": "true", 
      "allow_leading_wildcard": "true", 
      "default_operator": "AND" 
     } 
    } 
} 

文書1を返すように-7

ドキュメントとクエリの両方で、さまざまなアナライザとトークナイザ(std、whitespaceなど)を試しましたが、どれも私に希望の結果を与えませんでした。

たとえば、stdアナライザはすべてのドキュメントを返し、空白アナライザは1〜4だけを返しました。

希望の結果を返すアナライザー/トークナイザー/パラメーターはありますか?

:だけ明確にするために、私のデータは短いとなしの一般的な特性を持つ非常に長い文字列の両方で構成されています。例として挙げた単語(1,2,3,4)と記号(|)は便宜上のものであり、他の単語や単語以外の文字に置き換えることができます。

答えて

0

データを分析するためにパターンアナライザを試してください。 RegExを指定して、テキストを分割するパターンを定義することができます。

カスタムアナライザを作成し、データをトークン化するパターンを定義する必要があります。

https://www.elastic.co/guide/en/elasticsearch/guide/master/custom-analyzers.html

+0

非常に長い文字列でパターントークナイザを使用すると、パフォーマンスに問題が発生する可能性はありますか? – AviadG

+0

私はそれを確認することはできませんが、おそらくそこにあります。しかし、私は、それをプログラムで行うのではなく、elasticsearchで行う方が良いと思います。 –

0

すみません、私は昨日、あなたを理解していません。 最初にあなたは、動的なテンプレートを作成する必要があり、そして、あなたのフィールドnot_analyzedモードに設定します: は、私はわからない、一つの解決策は存在し、それが最適化され

curl -XPOST 'localhost:9200/_template/template1' -d ' 
{ 
    "template":"test_*", 
    "mappings": { 
     "item": { 
      "dynamic_templates": [ 
       { 
        "strings": { 
         "mapping": { 
          "index": "not_analyzed", 
          "type": "string" 
         }, 
         "match_mapping_type": "string" 
        } 
       } 
      ] 
     } 
    }, 
    "aliases": {} 
}' 

それから私は、次の行を挿入:

curl -XPOST localhost:9200/test_1/item/1 -d '{ "text": "one two"}' 
curl -XPOST localhost:9200/test_1/item/2 -d '{ "text": "one two three"}' 
curl -XPOST localhost:9200/test_1/item/3 -d '{ "text": "three one two"}' 
curl -XPOST localhost:9200/test_1/item/4 -d '{ "text": "three one two four"}' 
curl -XPOST localhost:9200/test_1/item/5 -d '{ "text": "one two|"}' 
curl -XPOST localhost:9200/test_1/item/6 -d '{ "text": "|one two"}' 
curl -XPOST localhost:9200/test_1/item/7 -d '{ "text": "|one two|"}' 
curl -XPOST localhost:9200/test_1/item/8 -d '{ "text": "one|two"}' 
curl -XPOST localhost:9200/test_1/item/9 -d '{ "text": "one| two"}' 
curl -XPOST localhost:9200/test_1/item/10 -d '{ "text": "one |two"}' 

とワイルドカードクエリを使用すると、返すことができ、必要な行:

curl localhost:9200/test_1/_search -d ' 
{ 
"query": { 
    "match" : { 
     "test" : "one two" 
    } 
} 
}' 

そして、このクエリの復帰7行:

[email protected]:/data/databases/elasticsearch-5.2.2/bin$ curl localhost:9200/test_1/_search?filter_path=hits.total -d ' 
{ 
"query": { 
    "wildcard" : { 
     "text" : "*one two*" 
    } 
} 
}' 
{"hits":{"total":7}} 

?filter_path - いくつかのフィールドを表示させます。この場合、存在する行の合計が表示されます。

関連する問題