Search code examples
spring-bootspring-data-elasticsearch

How to do a terms query on @Query in SpringBoot Elasticsearch Repository


From here, an example of how to query something IN a list, but the example is idealized. It queries id. I want to do a query: name in ["abc","ghi"]

From here, I find an answer, but I find it not work.

I try several different ways below.

@Query("{ \"terms\": {\"name\": [\"?0\"] } }")  //the 1st query. 
Flux<Response> findByNames(String names);


@Query("{\"names\": {\"values\": ?0 }}")  //mimic the ids query in the example.
Flux<Response> findByNames(List<String> names);


@Query("{ \"terms\": {\"name\": ?0 } }")
Flux<Response> findByNames(List<String> names);

The 1st query, I pass in nameList.stream().collect(Collectors.joining(",")), it generates the query json is nearly correct, but a slight error:

{ "terms": {"name": ["abc,ghi"] } }

If it can generate the query json like ( look at the quote mark):

{ "terms": {"name": ["abc","ghi"] } }

then, it works.

So, My question is: How to do a terms query on the @Query in SpringBoot Elasticsearch Repository


Solution

  • There was a bug in the handling of collection parameters in @Query methods that was fixed 2 weeks ago in the main branch (https://github.com/spring-projects/spring-data-elasticsearch/pull/2182), I'll need to port that back to the currently maintained branches as well.

    Addition 16.07.2022: it's backported now and will bei in the next maintenance releases of 4.4 and 4.3.