2

パーコレーター クエリを Elastic4s ライブラリに登録しようとしています。多くの調査の結果、次のようなものを使用する必要があることがわかりました。

val myQuery = """ { "query" : { "match" : { "foo" : "bar" } } } """

esClient.execute( register id 12345 into "baz" query myQuery )

私がこれをやっているかのように:

curl -XPUT 'localhost:9200/baz/.percolator/12345' -d '{
    "query" : {
        "match" : {
            "foo" : "bar"
        }
    }
}'

しかし、esClient.execute を実行しようとすると、いくつかの解析エラーが発生しますが、curl では問題なく動作します。

[info]   org.elasticsearch.index.percolator.PercolatorException: failed to parse query [987654]
[info]   at org.elasticsearch.index.percolator.PercolatorQueriesRegistry.parsePercolatorDocument(PercolatorQueriesRegistry.java:182)
[info]   at org.elasticsearch.index.percolator.PercolatorQueriesRegistry$RealTimePercolatorOperationListener.preIndex(PercolatorQueriesRegistry.java:301)
[info]   at org.elasticsearch.index.indexing.ShardIndexingService.preIndex(ShardIndexingService.java:146)
[info]   at org.elasticsearch.index.shard.IndexShard.index(IndexShard.java:565)
[info]   at org.elasticsearch.index.engine.Engine$Index.execute(Engine.java:836)
[info]   at org.elasticsearch.action.support.replication.TransportReplicationAction.executeIndexRequestOnPrimary(TransportReplicationAction.java:1073)
[info]   at org.elasticsearch.action.index.TransportIndexAction.shardOperationOnPrimary(TransportIndexAction.java:170)
[info]   at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryPhase.performOnPrimary(TransportReplicationAction.java:579)
[info]   at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryPhase$1.doRun(TransportReplicationAction.java:452)
[info]   at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
[info]   ...
[info]   Cause: org.elasticsearch.index.query.QueryParsingException: Failed to parse query [{"query":{"match":{"foo":"bar"}}}]
[info]   at org.elasticsearch.index.query.QueryStringQueryParser.parse(QueryStringQueryParser.java:243)
[info]   at org.elasticsearch.index.query.QueryParseContext.parseInnerQuery(QueryParseContext.java:257)
[info]   at org.elasticsearch.index.query.IndexQueryParserService.parseInnerQuery(IndexQueryParserService.java:244)
[info]   at org.elasticsearch.index.percolator.PercolatorQueriesRegistry.parseQuery(PercolatorQueriesRegistry.java:208)
[info]   at org.elasticsearch.index.percolator.PercolatorQueriesRegistry.parsePercolatorDocument(PercolatorQueriesRegistry.java:179)
[info]   at org.elasticsearch.index.percolator.PercolatorQueriesRegistry$RealTimePercolatorOperationListener.preIndex(PercolatorQueriesRegistry.java:301)
[info]   at org.elasticsearch.index.indexing.ShardIndexingService.preIndex(ShardIndexingService.java:146)
[info]   at org.elasticsearch.index.shard.IndexShard.index(IndexShard.java:565)
[info]   at org.elasticsearch.index.engine.Engine$Index.execute(Engine.java:836)
[info]   at org.elasticsearch.action.support.replication.TransportReplicationAction.executeIndexRequestOnPrimary(TransportReplicationAction.java:1073)
[info]   ...
[info]   Cause: org.apache.lucene.queryparser.classic.ParseException: Cannot parse '{"query":{"match":{"foo":"bar"}}}': Encountered " "}" "} "" at line 1, column 30.
[info] Was expecting one of:
[info]     "TO" ...
[info]     <RANGE_QUOTED> ...
[info]     <RANGE_GOOP> ...
[info]   at org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryParserBase.java:123)
[info]   at org.apache.lucene.queryparser.classic.MapperQueryParser.parse(MapperQueryParser.java:848)
[info]   at org.elasticsearch.index.query.QueryStringQueryParser.parse(QueryStringQueryParser.java:227)
[info]   at org.elasticsearch.index.query.QueryParseContext.parseInnerQuery(QueryParseContext.java:257)
[info]   at org.elasticsearch.index.query.IndexQueryParserService.parseInnerQuery(IndexQueryParserService.java:244)
[info]   at org.elasticsearch.index.percolator.PercolatorQueriesRegistry.parseQuery(PercolatorQueriesRegistry.java:208)
[info]   at org.elasticsearch.index.percolator.PercolatorQueriesRegistry.parsePercolatorDocument(PercolatorQueriesRegistry.java:179)
[info]   at org.elasticsearch.index.percolator.PercolatorQueriesRegistry$RealTimePercolatorOperationListener.preIndex(PercolatorQueriesRegistry.java:301)
[info]   at org.elasticsearch.index.indexing.ShardIndexingService.preIndex(ShardIndexingService.java:146)
[info]   at org.elasticsearch.index.shard.IndexShard.index(IndexShard.java:565)
[info]   ...
[info]   Cause: org.apache.lucene.queryparser.classic.ParseException: Encountered " "}" "} "" at line 1, column 30.
[info] Was expecting one of:
[info]     "TO" ...
[info]     <RANGE_QUOTED> ...
[info]     <RANGE_GOOP> ...
[info]   at org.apache.lucene.queryparser.classic.QueryParser.generateParseException(QueryParser.java:698)
[info]   at org.apache.lucene.queryparser.classic.QueryParser.jj_consume_token(QueryParser.java:580)
[info]   at org.apache.lucene.queryparser.classic.QueryParser.Term(QueryParser.java:394)
[info]   at org.apache.lucene.queryparser.classic.QueryParser.Clause(QueryParser.java:247)
[info]   at org.apache.lucene.queryparser.classic.QueryParser.Query(QueryParser.java:171)
[info]   at org.apache.lucene.queryparser.classic.QueryParser.TopLevelQuery(QueryParser.java:160)
[info]   at org.apache.lucene.queryparser.classic.QueryParserBase.parse(QueryParserBase.java:118)
[info]   at org.apache.lucene.queryparser.classic.MapperQueryParser.parse(MapperQueryParser.java:848)
[info]   at org.elasticsearch.index.query.QueryStringQueryParser.parse(QueryStringQueryParser.java:227)
[info]   at org.elasticsearch.index.query.QueryParseContext.parseInnerQuery(QueryParseContext.java:257)
[info]   ...

そのため、クエリを別の形式にする必要があると感じていますが、ドキュメントには記載されておらず、例が見つからないようです。私は次のような他のいくつかのフォーマットを試しました:

val myQuery2 = """ "query" : { "match" : { "foo" : "bar" } } """
val myQuery3 = """ { "match" : { "foo" : "bar" } } """
val myQuery4 = """ "match" : { "foo" : "bar" } """

しかし、何もうまくいかないようです。私がうまくいったのは、次のようにすることだけでした:

val myQuery5 = "query"

しかし、これでは、実際に一致させたいものを指定できません。誰でもこれに関する経験やアイデアはありますか?

4

1 に答える 1

1

このqueryメソッドは文字列を受け入れますが、文字列はクエリ文字列であり、試したような json フラグメントではありません。クエリ文字列の形式が適切でない場合は、elastic4s query dsl を使用してクエリを作成する必要があります。

termQuery("field", "value")

また

matchQuery("field", "value")

など。多くのクエリ タイプがあり、bool クエリでネストできます。

于 2016-01-03T12:24:10.347 に答える