I've just started using elasticsearch and I'm unsure how you go about using dynamic search queries. Currently I am using something like this:
$params['body'] = '
{
"query": {
"multi_match": {
"query": "' . $terms . '",
"fields": [
"title^1",
"description"
],
"minimum_should_match": "70%"
}
},
"highlight": {
"pre_tags" : ["<b>"],
"post_tags" : ["</b>"],
"fields": {
"description": {
"fragment_size": 400
},
"title": {
"number_of_fragments": 0
}
}
}
}
}';
This works fine when $terms
is just a string like ball
. What I'm trying to do is be able for a user to search by string ball
but maybe they want a ball from a particular location also and would have an optional location check box,if clicked a $state
could be part of the query now. I looked into Bool Query
but I just wasn't sure if this was the right approach. Any guidance would be great.
Updated code
{
"query": {
"bool": {
"must": [
{
"term": {
"description": {
"value": "' . $terms .'"
}
}
},
{
"term": {
"state_id": {
"value": "' . $location . '"
}
}
}
]
}
}
}';
I'm just using a simple if(isset() in PHP to see if the terms are there and one for the location. If there is no location $_GET variable i'm just setting it as null.