Elasticsearch suggested that we try to use Request Body query syntax in this way to support richer.
Common Syntax
First look at a simple example. This example queries two indexes (movies and 404_idx) of all documents. Theoretically, if 404_idx index does not exist, Elasticsearch returns an error. However, due to ignore_unavailable parameters, execution will ignore the index unavailable.
-
# ignore_unavailable=
true,可以忽略尝试访问不存在的索引“
404_idx”导致的报错
-
POST /movies,
404_idx/_search?ignore_unavailable=
true
-
{
-
"profile":
true,
-
"query": {
-
"match_all": {}
-
}
-
}
1 from / size
pagination
We can also use from / size
to implement paging combination.
-
POST /movies/_search
-
{
-
"from":
0,
-
"size":
20,
-
"query": {
-
"match_all": {}
-
}
-
}
2 sort
Sorting
Use sort
to achieve the sort. If you need something like sql statement the same sort on multiple fields, multiple elements can be passed in the sort inside.
-
POST /movies/_search
-
{
-
"sort": [{
"year": {
"order":
"desc"}},
-
{
"id.keyword": {
"order":
"desc"}} ],
-
"from":
0,
-
"size":
20,
-
"query": {
-
"match_all": {}
-
}
-
}
3 _source filter
Too many results Elasticsearch query, it will affect the efficiency of execution. Therefore, we can reduce query fields by filtration _source way.
The following query to display only the title attribute
-
POST /movies/_search
-
{
-
"sort": [{
"year": {
"order":
"desc"}},
-
{
"id.keyword": {
"order":
"desc"}} ],
-
"from":
0,
-
"size":
20,
-
"_source": [
"title"],
-
"query": {
-
"match_all": {}
-
}
-
}
4 Script field
Elasticsearch also supports painless script field, this mechanism can do some simple operations by way of the script, such as: string concatenation. In order scenario, orders have different exchange rates, calculated by the script, we can sort the results for the calculation.
-
POST /movies/_search
-
{
-
"sort": [{
"year": {
"order":
"desc"}},
-
{
"id.keyword": {
"order":
"desc"}} ],
-
"from":
0,
-
"size":
20,
-
"query": {
-
"match_all": {}
-
}
-
,
"script_fields": {
-
"new_title": {
-
"script": {
-
"lang":
"painless",
-
"source":
"doc['year'].value+'hello'"
-
}
-
}
-
}
-
}
5 match expression statement
1) query match, the contents of the query is OR default way. As follows:
-
POST /movies/_search
-
{
-
"query": {
-
"match": {
-
"title":
"last christmas"
-
}
-
}
-
}
2) If you are using AND manner, may be achieved by specifying the operator is and.
-
POST /movies/_search
-
{
-
"query": {
-
"match": {
-
"title": {
-
"query":
"last christmas",
-
"operator":
"and"
-
}
-
-
}
-
}
6 phrase queries
Use the phrase queries. Namely:phrase query
. No gaps among phrases default, but may be used to represent intermediate slop = 1 may be spaced a Term (word)
-
POST /movies/_search
-
{
-
"query": {
-
"match_phrase": {
-
"title":{
-
"query":
"one love",
-
"slop":
1
-
}
-
}
-
}
-
}
二 Query string && Simple query string
1 Elasticsearch also supports query string and simple query string. Let's look query string, in this way supports packet and multi-field.
First, two test data insert
-
PUT /users/_doc/
1
-
{
-
"name":
"Ruan Yiming",
-
"about":
"java, golang, node, swift, elasticsearch"
-
}
-
-
PUT /users/_doc/
2
-
{
-
"name":
"Li Yiming",
-
"about":
"Hadoop"
-
}
test:
-
POST /users/_search
-
{
-
"query": {
-
"query_string": {
-
"default_field":
"name",
-
"query":
"Ruan AND Yiming"
-
}
-
}
-
}
-
-
# 多字段 使用 fields
-
POST /users/_search
-
{
-
"query": {
-
"query_string": {
-
"fields": [
"name",
"about"],
-
"query":
"(Ruan AND Yiming) OR (Java AND Elasticsearch)"
-
}
-
}
-
}
-
simple query string, similar to the query string, but with the following differences:
- Grammar errors are ignored, and only supports parts of the query syntax
- Does not support
AND
, ,OR
,NOT
if the term appears, will be treated as a string - term relationship between the default
OR
, you can specify default_operator - Support part of the logic,
+
instead ofAND
,|
instead ofOR
,-
instead ofNOT
-
POST users/_search
-
{
-
"query": {
-
"simple_query_string": {
-
"query":
"Zhang Fubing",
-
"fields": [
"name"],
-
"default_operator":
"AND"
-
}
-
}
-
}