I'm trying to register a percolator query with the elastic4s library. After much research I've found I need to use something like:
val myQuery = """ { "query" : { "match" : { "foo" : "bar" } } } """
esClient.execute( register id 12345 into "baz" query myQuery )
As if I'm doing this:
curl -XPUT 'localhost:9200/baz/.percolator/12345' -d '{
"query" : {
"match" : {
"foo" : "bar"
}
}
}'
But whenever I try running esClient.execute I end up getting a few parsing errors, but it works fine through the 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] ...
So I've got the feeling that I need to format the query differently, but it's not in the docs and I can't seem to find any examples out there. I've tried a few other formats like so:
val myQuery2 = """ "query" : { "match" : { "foo" : "bar" } } """
val myQuery3 = """ { "match" : { "foo" : "bar" } } """
val myQuery4 = """ "match" : { "foo" : "bar" } """
But nothing seems to be able to work, the only thing I've gotten to work was just having it like:
val myQuery5 = "query"
But with this I cannot specify what I actually want to match on. Anyone have any experience with this, or any ideas?
The query
method accepts a string, but the string is a query string and not a json fragment as you have tried. If the query string format is not suitable, then you need to create the query using the elastic4s query dsl, eg
termQuery("field", "value")
or
matchQuery("field", "value")
etc. There are many query types, and they can be nested with a bool query.