Home > Software design >  Custom query in elasticsearch with spring boot
Custom query in elasticsearch with spring boot

Time:08-02

I am new at elasticsearch and I'm trying to make a custom query with ElasticSearchRepository. Input Data :

List<Long> bookId;
Long authorId;
Timestamp from;
Timestamp to;

How could i find a number of books with List bookId, check that all of them has the same authorId and check that they were published between timestamp from ... to ... ?

For example i think that it could look like

List<Books> findAllBooks (List<Long> booksId, Long authorId, Timestomp from, TimeStamp to); 

I use spring-boot-starter-data-elasticsearch

CodePudding user response:

you must create your query and add the @query annotation. Something like this:

@Query("{"query":{"bool":{"filter":[{"range":{"field_date":{"gte":"from","lte":"to"}}}],"must":[{"term":{"authorId":{"value":"authorId"}}},{"terms":{"bookId":"booksId"}}]}}}")
List<Books> findAllBooks (@Param("booksId") List<Long> booksId, @Param("authorId") Long authorId, @Param("from") Timestomp from, @Param("to") TimeStamp to)

CodePudding user response:

BoolQueryBuilder and NativeSearchQuery Api helps me a lot.

  • Related