-**Match**: Returns documents that match a provided text, number, date or boolean value
-**Match**: Returns documents that match a provided text, number, date or boolean value
-**query_string**: Returns documents based on a provided query string, using a parser with a strict syntax. (=> simple_query_string more dynamic than match and easier and more robiust syntax than query_string)
-**query_string**: Returns documents based on a provided query string, using a parser with a strict syntax. (=> simple_query_string more dynamic than match and easier and more robiust syntax than query_string)
Improving the search
## Improving the search
--------------------
Using simple_query_string in the following examples. For quite a few functionalities one can use multi-match too. But simple_query_string is more flexible!
Using simple_query_string in the following examples.
### Basic title search
Let's start with a simple example (GET: https://localhost:9200/_search):
Let's start with a simple example (GET: https://localhost:9200/_search):
```json
```json
...
@@ -34,6 +36,8 @@ The returned results contain 5 documents:
...
@@ -34,6 +36,8 @@ The returned results contain 5 documents:
The problem is that this input either searches for the string *python* OR the string *programming* in the title.
The problem is that this input either searches for the string *python* OR the string *programming* in the title.
### AND & OR operators
To search for results containing both words the *default_operator* parameter (default: "or") needs to be set to "and":
To search for results containing both words the *default_operator* parameter (default: "or") needs to be set to "and":
```json
```json
{
{
...
@@ -47,9 +51,40 @@ To search for results containing both words the *default_operator* parameter (de
...
@@ -47,9 +51,40 @@ To search for results containing both words the *default_operator* parameter (de
}
}
```
```
Now no result is returned, since no title of the documents contains both words. We can search more fields by adding the path to the *fields* parameter:
Now no result is returned, since no title of the documents contains both words.
AND and OR can also be specified in the query string with & and |. Other operations can be used with the [simple query string syntax](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html#simple-query-string-syntax).
### Search multiple fields
We can search more fields by adding the path to the *fields* parameter:
```json
```json
"fields":["_content.Page.Content.Title.~",
"fields":["_content.Page.Content.Title.~",
"_content.Page.Content.Body.~"]
"_content.Page.Content.Body.~"]
```
```
Now the returned search results contain 11 Documents.
Now the returned search results contain 11 Documents.
### Use Fuzziness for typos
Since the searches only look for exact matches of strings and keywords the following search does not return any results:
```json
{
"query":{
"simple_query_string":{
"query":"Pytho",
"fields":[
"_content.Page.Content.Title.~",
"_content.Page.Content.Body.~"
],
"default_operator":"and"
}
}
}
```
To solve this issue we add automatic fuzziness to the query.
```json
"query":"Pytho~"
```
Now we retrieve 28 different documents.
The Edit-Distances for fuzziness can be specified behind the like `"query": "Pytho~2"` but the automatic fuzziness setting is [recommended](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness) as it takes term length into account.