Search code examples
elasticsearchspring-data-elasticsearch

How to design a generic search method for searching through any attribute in an index


How to design a generic search method for searching through any attribute in an index. I would like to take in a generic Map as my input where it allows you to describe the attribute and the corresponding value which you intend to map it to and I would prefer the output is available in a paginated fashion.

For example it could be something like, search all User's which has
    gender = 'm' AND first_name starts contains "ack" AND birth_date between 1970 and 1980

I am looking to build it using an interface which allows me to build the query based on the inputs provided in a dynamic fashion. Does spring-data-elasticsearch provide any higher level constructs to build such queries and return results in a paginated fashion. How should I approach this?

@PostMapping("/search")
public Iterable<User> search(@RequestParam(required = false) Map<String, String> inputs, 
    Pageable pageable) {
    return userService.search(inputs, pageable);
}

Solution

  • Please check the documentation, Section 7.6, "Queries"

    The link to this section seems to be broken, so click on 7.5 and scroll down a bit.

    There the 3 possibilities that Spring Data Elasticsearch offers (CriteriaQuery, StringQuery and NatveSearchQuery) are described.