私はElasticSearch(6.2)プロジェクトに取り組んでいます。このプロジェクトでは、index
多くのkeyword
フィールドがあり、lowercase
大文字と小文字を区別しない検索を実行するためにフィルターで正規化されています。検索はうまく機能し、正規化されたフィールドの実際の値(小文字ではない)を返します。ただし、集計はフィールドの実際の値を返さない(小文字を返す)。
次の例は、ElasticSearchドキュメントから抜粋したものです。
https://www.elastic.co/guide/en/elasticsearch/reference/master/normalizer.html
インデックスの作成:
PUTインデックス { "設定":{ "分析":{ "ノーマライザー":{ "my_normalizer":{ "タイプ": "カスタム"、 "char_filter":[]、 "フィルター":["小文字"、 "asciifolding" ] }} } } }、 "mappings":{ "_ doc":{ "properties":{ "foo":{ "type": "keyword"、 "normalizer": "my_normalizer" } } } } }}
ドキュメントの挿入:
PUT index / _doc / 1 { "foo": "Bar" } PUT index / _doc / 2 { "foo": "Baz" }
集計による検索:
GET index / _search { "size":0、 " aggs":{ " foo_terms":{ "terms":{ "field": "foo" } } } }
結果:
{ "took":43、 "timed_out":false、 "_shards":{ "total":1、 "successful":1、 "skipped":0、 "failed":0 }、 "hits":{ "total ":3、 " max_score ":0.0、 " hits ":{ " total ":2、 " max_score ":0.47000363、 " hits ":[ { " _index ":" index "、 " _type ":" _doc "、 " _id ":" 1 "、 " _ score ":0.47000363、 " _ source ":{ "foo": "Bar" } }、 { "_ index": "index"、 "_type": "_doc"、 "_id": "2"、 "_score":0.47000363、 "_source":{ "foo": "Baz" } } ] } }、 "aggregations":{ "foo_terms":{ "doc_count_error_upper_bound":0、 "sum_other_doc_count ":0、 "バケット ":[ { "キー ":"バー "、 " doc_count ":1 }、 { "キー ":" baz "、 " doc_count ":1 } ] } } }}
集計を確認すると、小文字の値が返されていることがわかります。例:"key": "bar"
。
実際の値を返すように集計を変更する方法はありますか?
例えば "key": "Bar"
大文字と小文字を区別しない検索を実行しながら、集計で正確な値を返したい場合は、ノーマライザーは必要ありません。サブtext
フィールドを持つフィールド(トークンを小文字にし、デフォルトで大文字と小文字を区別しない検索を可能にする)を持つことができkeyword
ます。前者を検索に使用し、後者を集計に使用します。こんなふうになります:
PUT index
{
"mappings": {
"_doc": {
"properties": {
"foo": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}
2つのドキュメントにインデックスを付けた後、次の場所でterms
集計を発行できますfoo.keyword
。
GET index/_search
{
"size": 2,
"aggs": {
"foo_terms": {
"terms": {
"field": "foo.keyword"
}
}
}
}
そして、結果は次のようになります。
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "index",
"_type": "_doc",
"_id": "2",
"_score": 1,
"_source": {
"foo": "Baz"
}
},
{
"_index": "index",
"_type": "_doc",
"_id": "1",
"_score": 1,
"_source": {
"foo": "Bar"
}
}
]
},
"aggregations": {
"foo_terms": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Bar",
"doc_count": 1
},
{
"key": "Baz",
"doc_count": 1
}
]
}
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加