使用Elastic4s向渗滤器注册查询



我正在尝试向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"

但有了这个,我无法具体说明我真正想要匹配的内容。有人对此有任何经验或想法吗?

query方法接受一个字符串,但该字符串是一个查询字符串,而不是您尝试过的json片段。如果查询字符串格式不合适,则需要使用elastic4s查询dsl(例如)创建查询

termQuery("field", "value")

matchQuery("field", "value")

等等。有许多查询类型,它们可以嵌套在bool查询中。

相关内容

  • 没有找到相关文章

最新更新