# How to add criteria dynamically while searching against elasticsearch

**URL:** https://discuss.elastic.co/t/how-to-add-criteria-dynamically-while-searching-against-elasticsearch/248333
**Category:** Elasticsearch
**Created:** [September 11, 2020, 1:39pm UTC](https://discuss.elastic.co/t/how-to-add-criteria-dynamically-while-searching-against-elasticsearch/248333 "2020-09-11T13:39:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ocmvin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ocmvin/32/99006_2.png) [@ocmvin](https://discuss.elastic.co/u/ocmvin)
#### Post date: [September 11, 2020, 1:39pm UTC](https://discuss.elastic.co/t/how-to-add-criteria-dynamically-while-searching-against-elasticsearch/248333/1 "2020-09-11T13:39:35Z")

</div>

I have a big document with a requirement to dynamically search  
E.g. Student

> {  
> id,  
> name,  
> Address,  
> Associated\_Subjects  
> phone,  
> home\_phone,  
> mobile,  
> year,  
> dob,  
> geolocation:(lat/long)  
> height  
> .  
> .  
> .  
> etc  
> }

However I have certain fields to be searched with static criteria e.g. year as 2020,geoLocation within 20 miles etc  
and some criteria to be added on various conditions e.g. year \<2020 and height \>4 feet (this is just an example)

Not sure how do I cater this requirement ie. Either use a SQL template as below and have place holder to inject (append) additional criterias in it through  
java code or use RestHighLevelClient (Could not find such a realistic example through like relational ORM criteria api like Specification)

Here is how my template will look like and will be formed on the fly as per request

```auto
    "bool": {
        "must": [
           
            {
                "match": {
                    "Associated_Subjects": $SUBJECT$
                }
            }
        ],
        "filter": [
			$DYNAMIC_PLACEHOLDERS$
            {
                "geo_distance": {
                    "distance": "$DISTANCE_RADIUS$",
                    "location": {
                        "lat": $LATITUDE$,
                        "lon": $LONGITUDE$
                    }
                }
            },
            {
                "range" : {
                    "dob": {
                        "gte": "$DOB$"
                    }
                }
            }
        ]
    }
}

```

e.g. $DYNAMIC\_PLACEHOLDERS$ will be added as below on the fly  
{  
"range" : {  
"age": {  
"gte": "$DOB$"  
}  
}  
},  
My Question : Is having such template and modifying as per request is good idea or is there any better way to deal with it and what about the performance is there any best practices in Elasticsearch ?

---

<div class="post-metadata">

### Author: ![Peter\_Steenbergen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_steenbergen/32/22888_2.png) [@Peter\_Steenbergen](https://discuss.elastic.co/u/Peter_Steenbergen)
#### Post date: [September 11, 2020, 1:43pm UTC](https://discuss.elastic.co/t/how-to-add-criteria-dynamically-while-searching-against-elasticsearch/248333/2 "2020-09-11T13:43:35Z")

</div>

Elasticsearch does not have if/else in their templating engine. Only if a field is set you could add a block to the query.

If it is a dynamic query with dynamic fields and conditions I would recommend using it in code somewhere and build up the query there. But maybe I am getting the question wrong..

---

<div class="post-metadata">

### Author: ![ocmvin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ocmvin/32/99006_2.png) [@ocmvin](https://discuss.elastic.co/u/ocmvin)
#### Post date: [September 14, 2020, 10:33am UTC](https://discuss.elastic.co/t/how-to-add-criteria-dynamically-while-searching-against-elasticsearch/248333/3 "2020-09-14T10:33:41Z")

</div>

For the Elastic search document definition below  
{  
id,  
name,  
Address,  
Associated\_Subjects  
phone,  
home\_phone,  
mobile,  
year,  
dob,  
geolocation:(lat/long)  
**height**  
**gender**  
.  
.  
.  
etc  
}

e.g. in Above document I need to search on the fly  
e.g. If gender = **Male** then **height \>=5** feet  
& if gender = **FeMale** then **height \>=4** feet

However rest of the Query fields remain same & vary based on gender as below

I would have QUERY\_TEMPLATE as below  
E.g.  
"bool": {  
"must": [

```
            {
                "match": {
                    "Associated_Subjects": $SUBJECT$
                }
            }
        ],
        "filter": [
			$DYNAMIC_PLACEHOLDERS$
            {
                "geo_distance": {
                    "distance": "$DISTANCE_RADIUS$",
                    "location": {
                        "lat": $LATITUDE$,
                        "lon": $LONGITUDE$
                    }
                }
            },
            {
                "range" : {
                    "dob": {
                        "gte": "$DOB$"
                    }
                }
            }
        ]
    }
}

```

Here based on gender  
e.g. if Male **DYNAMIC\_PLACEHOLDERS** will be replaced with

```
{
				"range": {
					"LATITUDE": {
						"gte": 5
					}
				}
},

```

AND  
e.g. if **FeMale** DYNAMIC\_PLACEHOLDERS will be replaced with

```
{
				"range": {
					"LATITUDE": {
						"gte": 4
					}
				}
},

```

Here replacing DYNAMIC\_PLACEHOLDERS with above range constraint I am planning to handle in java code.Will this be a good idea and efficient way or is there  
any better way to do it as I have many such dynamic conditions will require to be added based on request parameters ?

---

<div class="post-metadata">

### Author: ![Peter\_Steenbergen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_steenbergen/32/22888_2.png) [@Peter\_Steenbergen](https://discuss.elastic.co/u/Peter_Steenbergen)
#### Post date: [September 23, 2020, 10:50am UTC](https://discuss.elastic.co/t/how-to-add-criteria-dynamically-while-searching-against-elasticsearch/248333/4 "2020-09-23T10:50:32Z")

</div>

Are these values always present? What you could do is creating a search template and use parameters to change those values. Your application triggers the search. So in that case you can use the template and set the right values to the params.

A link to the documentation about that:

> **[Search Template | Elasticsearch Reference \[7.9\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html)**

It comes to this workflow:

1. Create the template

```auto
POST _scripts/<templateid>
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "title": "{{query_string}}"
        }
      }
    }
  }
}

```

The name between {{ and }} will be the paramaters.

1. Fire the search with the template id

```auto
GET _search/template
{
  "id": "<templateid>", 
  "params": {
    "query_string": "search for these words"
  }
}

```

So the latitude value can be a parameter same like the origin point and the radius, next to the subjects.

---

<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: [October 21, 2020, 10:50am UTC](https://discuss.elastic.co/t/how-to-add-criteria-dynamically-while-searching-against-elasticsearch/248333/5 "2020-10-21T10:50:37Z")

</div>

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