Create home authored by Fruth, Leon's avatar Fruth, Leon
Findings with the Elastic Search REST API
===========================================
Different search query methods
--------------------------------
**(Multi-)match** vs. **(simple_)query_string**
- **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)
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!
Let's start with a simple example (GET: https://localhost:9200/_search):
```json
{
"query": {
"simple_query_string" : {
"query": "Python programming",
"fields": ["_content.Page.Content.Title.~"]
}
}
}
```
Searches for the two words *Python* and *programming* in the field *title*.
The returned results contain 5 documents:
1. "Programming for linguistics" (Score: 4.294303)
2. "Programming for linguistics" (Score: 4.294303)
3. "Python basics" (Score: 4.268799)
4. "Stanford NER from Python" (Score: 3.556681)
5. "Stanford PoS Tagger: tagging from Python" (Score: 3.0481849)
The problem is that this input either searches for the string *python* OR the string *programming* in the title.
To search for results containing both words the *default_operator* parameter (default: "or") needs to be set to "and":
```json
{
"query": {
"simple_query_string" : {
"query": "Python programming",
"fields": ["_content.Page.Content.Title.~"],
"default_operator": "and"
}
}
}
```
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:
```json
"fields": ["_content.Page.Content.Title.~",
"_content.Page.Content.Body.~"]
```
Now the returned search results contain 11 Documents.