18

私のelasticsearchインデックスに、句読点で区切られた単語の文字列を含む「ドット」というフィールドがあるとしましょう(例:「first.second.third」)。

たとえば、「first.second」を検索してから、「ドット」フィールドに正確に「first.second」または「first.second.」で始まる文字列が含まれるすべてのエントリを取得する必要があります。

テキストのクエリがどのように機能するかを理解するのに問題があります。少なくとも、その仕事をするクエリを作成できませんでした。

4

5 に答える 5

23

Elasticsearch には、まさにそのようなユース ケースのために作成されたPath Hierarchy Tokenizerがあります。インデックスに設定する方法の例を次に示します。

# Create a new index with custom path_hierarchy analyzer 
# See http://www.elasticsearch.org/guide/reference/index-modules/analysis/pathhierarchy-tokenizer.html
curl -XPUT "localhost:9200/prefix-test" -d '{
    "settings": {
        "analysis": {
            "analyzer": {
                "prefix-test-analyzer": {
                    "type": "custom",
                    "tokenizer": "prefix-test-tokenizer"
                }
            },
            "tokenizer": {
                "prefix-test-tokenizer": {
                    "type": "path_hierarchy",
                    "delimiter": "."
                }
            }
        }
    },
    "mappings": {
        "doc": {
            "properties": {
                "dots": {
                    "type": "string",
                    "analyzer": "prefix-test-analyzer",
                    //"index_analyzer": "prefix-test-analyzer", //deprecated
                    "search_analyzer": "keyword"
                }
            }
        }
    }
}'
echo
# Put some test data
curl -XPUT "localhost:9200/prefix-test/doc/1" -d '{"dots": "first.second.third"}'
curl -XPUT "localhost:9200/prefix-test/doc/2" -d '{"dots": "first.second.foo-bar"}'
curl -XPUT "localhost:9200/prefix-test/doc/3" -d '{"dots": "first.baz.something"}'
curl -XPOST "localhost:9200/prefix-test/_refresh"
echo
# Test searches. 
curl -XPOST "localhost:9200/prefix-test/doc/_search?pretty=true" -d '{
    "query": {
        "term": {
            "dots": "first"
        }
    }
}'
echo
curl -XPOST "localhost:9200/prefix-test/doc/_search?pretty=true" -d '{
    "query": {
        "term": {
            "dots": "first.second"
        }
    }
}'
echo
curl -XPOST "localhost:9200/prefix-test/doc/_search?pretty=true" -d '{
    "query": {
        "term": {
            "dots": "first.second.foo-bar"
        }
    }
}'
echo
curl -XPOST "localhost:9200/prefix-test/doc/_search?pretty=true&q=dots:first.second"
echo
于 2012-08-26T01:46:40.203 に答える
2

プレフィックス クエリをご覧ください。

$ curl -XGET 'http://localhost:9200/index/type/_search' -d '{
    "query" : {
        "prefix" : { "dots" : "first.second" }
    }
}'
于 2012-08-24T23:04:06.283 に答える
1

同様の解決策を探していましたが、プレフィックスのみに一致します。私は@imtovの答えを見つけて、ほぼそこにたどり着きましたが、1つの変更点として、アナライザーを切り替えました。

"mappings": {
    "doc": {
        "properties": {
            "dots": {
                "type": "string",
                "analyzer": "keyword",
                "search_analyzer": "prefix-test-analyzer"
            }
        }
    }
}

それ以外の

"mappings": {
    "doc": {
        "properties": {
            "dots": {
                "type": "string",
                "index_analyzer": "prefix-test-analyzer",
                "search_analyzer": "keyword"
            }
        }
    }
}

このように追加します:

'{"dots": "first.second"}'
'{"dots": "first.third"}'

firstsecond、トークンを保存せずに、これらの完全なトークンのみを追加しthirdます。

まだどちらかを探している

first.second.anyotherstring
first.second

最初のエントリのみを正しく返します。

'{"dots": "first.second"}'

あなたが求めたものとは正確には異なりますが、何らかの形で関連しているので、誰かを助けることができると思いました.

于 2016-02-15T16:34:48.053 に答える
1

次のようなクエリを作成するには、commodin chars を使用する必要があります。

$ curl -XGET http://localhost:9200/myapp/index -d '{
    "dots": "first.second*"
}'

構文に関するその他の例: http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html

于 2012-08-24T22:23:26.403 に答える