Reputation: 51
I've been working with simple query string to query information inside of elastic but I ran into a problem.
When I create a query like:
"simple_query_string":{
"query":"\"this is a phrase\" | KEYWORD_1 | KEYWORD_2",
"fields":[
"field_1",
"field_2",
"field_3",
],
}
It return 0 results, meanwhile if the query is structured with parenthesis:
"simple_query_string":{
"query":"(\"this is a phrase\" | KEYWORD_1) | KEYWORD_2",
"fields":[
"field_1",
"field_2",
"field_3",
],
}
It works smoothly.
I was wondering if I'm missing something in elastic's documentation.
Upvotes: 2
Views: 494
Reputation: 51
As IanGabes said in a comment:
According to elasticsearch's documentation:
foo bar +baz -qux
The familiar boolean operators AND, OR and NOT (also written &&, || and !) are also supported but beware that they do not honor the usual precedence rules, so parentheses should be used whenever multiple operators are used together. For instance the previous query could be rewritten as:
((foo AND baz) OR (bar AND baz) OR baz) AND NOT qux
Or in simple query lenguaje:
((foo + baz) | (bar + baz) | baz) + -qux
Regarding my example:
"this is a phrase" | KEYWORD_1 | KEYWORD_2
Could be extended to
("this is a phrase" | KEYWORD_1) | ("this is a phrase" | KEYWORD_2) | (KEYWORD_1 | KEYWORD_2)
But is, in a simpler way:
("this is a phrase" | KEYWORD_1) | KEYWORD_2
Upvotes: 2
Reputation: 16172
Adding a working example with index data, search query, and search result. To know more about Simple Query string parameters refer to this official documentation on simple query string syntax
Index Data
{
"name":"multi",
"title":"Science",
"shape":"triangle"
}
{
"name":"multi grain bread",
"title":"Maths",
"shape":"square"
}
{
"name":"multi",
"title":"Science",
"shape":"square"
}
Search Query
{
"query": {
"simple_query_string": {
"query": "\"multi grain bread\" | Maths | square",
"fields": [
"title",
"name",
"shape"
]
}
}
}
Search Result
"hits": [
{
"_index": "my-index",
"_type": "_doc",
"_id": "1",
"_score": 2.6891878,
"_source": {
"name": "multi grain bread",
"title": "Maths",
"shape": "square"
}
},
{
"_index": "my-index",
"_type": "_doc",
"_id": "2",
"_score": 0.47000363,
"_source": {
"name": "multi",
"title": "Science",
"shape": "square"
}
}
]
Upvotes: 0