# How to Implement a Flexible Search Method in Java with Low Level Client to Filter, Sort, and Limit Fields?

**URL:** https://discuss.elastic.co/t/how-to-implement-a-flexible-search-method-in-java-with-low-level-client-to-filter-sort-and-limit-fields/346253
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [November 2, 2023, 12:41am UTC](https://discuss.elastic.co/t/how-to-implement-a-flexible-search-method-in-java-with-low-level-client-to-filter-sort-and-limit-fields/346253 "2023-11-02T00:41:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Roman\_Kagan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_kagan/32/63832_2.png) [@Roman\_Kagan](https://discuss.elastic.co/u/Roman_Kagan)
#### Post date: [November 2, 2023, 12:41am UTC](https://discuss.elastic.co/t/how-to-implement-a-flexible-search-method-in-java-with-low-level-client-to-filter-sort-and-limit-fields/346253/1 "2023-11-02T00:41:36Z")

</div>

Hello,

I'm working on implementing a search method in Java that needs to support several variations of search criteria. Specifically, I need the method to be able to:

1. Search by a term within certain fields, conditionally filter the data by a date range, limit the output to only include specified fields, and sort the results by one or more fields.
2. Perform the same operations as above but use a `filterMap` for filtering instead of a search term.
3. Combine both term-based and `filterMap`-based filtering.

Here's the signature of the method I've come up with: `private List<Map<String, Object>> search( final Set<String> requestedFields, final List<SortBy> sortByList, final List<String> termSearchableFields, final String searchTerm, final Map<String, Object> filterMap, final DateRangeTerm dateRangeTerm )...`

---

<div class="post-metadata">

### Author: ![Roman\_Kagan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_kagan/32/63832_2.png) [@Roman\_Kagan](https://discuss.elastic.co/u/Roman_Kagan)
#### Post date: [November 7, 2023, 11:20pm UTC](https://discuss.elastic.co/t/how-to-implement-a-flexible-search-method-in-java-with-low-level-client-to-filter-sort-and-limit-fields/346253/2 "2023-11-07T23:20:44Z")

</div>

This is what it boils down to: \> GET patdocument-1382/\_search

> {  
> "query": {  
> "multi\_match": {  
> "query": "Patient Letter-Lara",  
> "fields":["DocumentName","AuthorName"]  
> }  
> },  
> "fields": [  
> "DocumentName",  
> "AuthorName",  
> "CreatedDate"  
> ],  
> "post\_filter": {"range": { "CreatedDate.keyword": {"gte":"2022-10-04 23:44:37.000000","lte":"2023-06-24 05:46:49.000000"}}},  
> "sort" : [  
> { "CreatedDate.keyword": {"order": "desc"}}],
> 
> "\_source": false  
> }

---

<div class="post-metadata">

### Author: ![Roman\_Kagan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_kagan/32/63832_2.png) [@Roman\_Kagan](https://discuss.elastic.co/u/Roman_Kagan)
#### Post date: [November 14, 2023, 12:02am UTC](https://discuss.elastic.co/t/how-to-implement-a-flexible-search-method-in-java-with-low-level-client-to-filter-sort-and-limit-fields/346253/3 "2023-11-14T00:02:21Z")

</div>

> [@Roman\_Kagan](#):
>
> {  
> "query": {  
> "multi\_match": {  
> "query": "Patient Letter-Lara",  
> "fields":["DocumentName","AuthorName"]  
> }  
> },  
> "fields": [  
> "DocumentName",  
> "AuthorName",  
> "CreatedDate"  
> ],  
> "post\_filter": {"range": { "CreatedDate.keyword": {"gte":"2022-10-04 23:44:37.000000","lte":"2023-06-24 05:46:49.000000"}}},  
> "sort" : [  
> { "CreatedDate.keyword": {"order": "desc"}}],
> 
> "\_source": false  
> }

This is the solution:

```
    String searchText = "xx";
    List fields = new ArrayList<>();
    fields.add("DocumentName");
    fields.add("AuthorName");

    List fieldsToReturn = new ArrayList<FieldAndFormat>();
   // list.add() ....
    double maxPricemaxPrice = 200.0;
    List sortOptions = new ArrayList<SortOptions>();
    RangeQuery range = RangeQuery.of(r -> r
            .field("price")
            .gte(JsonData.of(maxPricemaxPrice)) // <3>
    )._toQuery().range();
            try {
                SearchResponse<PatDocument2> response = client.search(s -> s
                                .index("patdocument-3482")
                                .query(q -> q
                                        .multiMatch(t -> t
                                                .fields(fields)
                                                .query(searchText)
                                        )
                                ).fields(fieldsToReturn)
                                .postFilter(r->r.range(range))
                                .sort(sortOptions),
                        PatDocument2.class);

```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [November 14, 2023, 9:54am UTC](https://discuss.elastic.co/t/how-to-implement-a-flexible-search-method-in-java-with-low-level-client-to-filter-sort-and-limit-fields/346253/4 "2023-11-14T09:54:19Z")

</div>

It's not with the Low Level client then. May I suggest that you edit the title of your post?

---

<div class="post-metadata">

### Author: ![Roman\_Kagan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roman_kagan/32/63832_2.png) [@Roman\_Kagan](https://discuss.elastic.co/u/Roman_Kagan)
#### Post date: [December 6, 2023, 2:29am UTC](https://discuss.elastic.co/t/how-to-implement-a-flexible-search-method-in-java-with-low-level-client-to-filter-sort-and-limit-fields/346253/5 "2023-12-06T02:29:18Z")

</div>

I am using the low level Java client. What do you mean "it's not with the Low Level client"?

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [January 3, 2024, 2:30am UTC](https://discuss.elastic.co/t/how-to-implement-a-flexible-search-method-in-java-with-low-level-client-to-filter-sort-and-limit-fields/346253/6 "2024-01-03T02:30:13Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
