0

I have this interface defined in Spring for querying Elascticsearch. I added @Query annotation to get some filtering done.

public interface ObjectElasticSearch extends ElasticsearchRepository<ElasticObject, String> {

@Query("{\"query\" : {\"filtered\" : {\"filter\" : { \"and\" : [ { \"term\" : { \"firstName\" : \":firstName\" }}, { \"term\" : { \"lastName\" : \"Baggins\" }} ] }}}}")
List<ElasticObject> findByDocFirstNameAndDocLastName(@Param("firstName") String firstName,
        @Param("lastName") String lastName);
};

The @Query annotation gets ignored completely. As you can see I tried hardcoding last name, and it has no effect on the outcome of the query. If I delete a curly brace in the query string, I don't get any errors. Query still works, the filtering is ignored, and it returns all matches.

Can someone please help me figure out what am I doing wrong here.

1 Answers1

0

Query should return a bool that in a must section can cover all your searches.

Here is my example that works for me, I have a records with startTimestamp and endTimestamp, and my method will find all of them that overlap specified timeframe + match fields by query

    @Query("{\"bool\": {\"must\":["
        + "            {\"range\": {\"endTimestamp\": {\"gte\": ?0}}},"
        + "            {\"range\": {\"startTimestamp\": {\"lte\": ?1}}}"
        + "        ], \"should\": ["
        + "             {\"match\": {\"_all\": {\"query\": \"?2\", \"zero_terms_query\": \"all\"}}}"
        + "        ],"
        + "        \"minimum_should_match\" : 1"
        + "    }"
        + "}")