Currently I'm working with a ElasticsearchRepository and Springboot Data. I can write a simple method with a simple Query annotation:
@Query("{\"bool\":{\"must\":[{\"match\":{\"_resource\":\"?0\"}}]}}")
List<Activity> findByResource(String resource);
However, the Activity index might have different, multiple fields (varying from line to line in logstash). So, I might need to add multiple match clauses. I was testing adding a second parameter that would contain the rest of the params (if any) like this:
@Query("{\"bool\":{\"must\":[{\"match\":{\"_resource\":\"?0\"}}?1]}}")
List<Activity> findByResource(String resource, String additionalClauses);
the second parameter could look like this:
String additional = ",{\"match\":{\"_method\":\"GET\"}}
However, I get this exception when calling the endpoint with the service:
Elasticsearch exception [type=x_content_parse_exception, reason=[1:60] [bool] failed to parse field [must]]; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=x_content_parse_exception, reason=[1:60] [bool] failed to parse field [must]]]; nested: ElasticsearchException[Elasticsearch exception [type=json_parse_exception, reason=Unexpected character ('\\' (code 92)): was expecting double-quote to start field name\n at [Source: (ByteArrayInputStream); line: 1, column: 62]]];
I understand that what I was trying to do isn't not the ideal way to use the @Query annotation (I don't think it's even allowed to do what I'm trying), so:
Cheers.
Passing additional query components as parameter in this way is not possible. If a parameter is a String it will be quoted and escaped before being inserted into the value of the @Query
annotation. This created String is then used to build a StringQuery
.
You will need to create one of the Query
variants (see https://docs.spring.io/spring-data/elasticsearch/docs/4.3.0/reference/html/#elasticsearch.operations.queries for details), probably a StringQuery
and pass that to the ElasticsearchOperations
.