Create a new index
PUT /approval_list_zhc
Copy the code
Insert data
{
"name":"xilianpeng",
"age":27,
"email":"[email protected]",
"sex":"0",
"salary":35000
}
Copy the code
All the query
{"query":{"match_all":{}}}
Copy the code
Query all and sort by salsry in reverse order
{
"query": {
"match_all": {
}
},
"sort":{
"salary":"desc"
}
}
Copy the code
Select * from ‘age’ where ‘salary’ is the same as’ age ‘
{
"query": {
"match_all": {
}
},
"sort": [{
"salary": "desc"
}, {
"age": "asc"
}]
}
Copy the code
Query first and second data, salary descending order, age ascending order
{
"query": {
"match_all": {
}
},
"sort": [{
"salary": "desc"
}, {
"age": "asc"
}],
"from": 0,
"size": 2
}
Copy the code
_source specifies which fields to return
{
"query": {
"match_all": {
}
},
"sort": [{
"salary": "asc"
}, {
"age": "asc"
}],
"from": 1,
"size": 2,
"_source": ["name", "age"]
}
Copy the code
Match specifies which attribute matches which value, such as retrieving information that age is 20
{
"query": {
"match": {
"age": 20
}
}
}
Copy the code
Match a string field is a fuzzy search (maintains an inverted index)
For example, if “wangcai erha “matches an email value of “wangcai erha gouzi” and a “wangcai erha” match that doesn’t match a string, that’s an exact search
{
"query": {
"match": {
"email": "wangcai erha"
}
}
}
Copy the code
Match_phrase Phrase match
If it matches “Wangcai erha gouzi”, then the email field must have the whole phrase “wangcai erha gouzi”, if it’s match, it only has to have one of them, For example, the email field containing “Wangcai erha” will match
{
"query": {
"match_phrase": {
"email": "wangcai erha gouzi"
}
}
}
Copy the code
Only one can be retrieved with match_PHRASE
Two results can be retrieved using match
Multi_match Match of multiple fields
Query as long as the email field contains erha or gouzi data
{
"query": {
"multi_match": {
"query": "erha gouzi",
"fields": "email"
}
}
}
Copy the code
Boolean query
Multiple query criteria can be merged. The sex field must be 1 and the email field must contain erha
{
"query": {
"bool": {
"must": [{
"match": {
"sex": 1
}
},
{
"match": {
"email": "erha"
}
}
]
}
}
}
Copy the code
Must_not must not be met. The following is a multi-condition composite query.
{
"query": {
"bool": {
"must": [{
"match": {
"sex": 1
}
},
{
"match": {
"email": "erha"
}
}
],
"must_not": [{
"match": {
"name": "wangcai"
}
}]
}
}
}
Copy the code
“Should” is the best condition for “should”, the highest score, and it doesn’t matter if you don’t
{
"query": {
"bool": {
"must": [{
"match": {
"sex": 1
}
},
{
"match": {
"email": "erha"
}
}
],
"should": [{
"match": {
"name": "wangcai"
}
}]
}
}
}
Copy the code
Filter will filter out those that do not meet this condition
{ "query": { "bool": { "must": [{ "match": { "sex": 1 } }], "filter": { "range": { "salary": { "gte": 20000, "lte": 30000}}}}}}Copy the code
Term For exact dictionaries, non-text dictionaries use term queries.
Because ES saves the text field, there is a word segmentation problem. It is very difficult to retrieve the entire value with term
{
"query": {
"term": {
"salary": 20000
}
}
}
Copy the code
Keyword corresponds to an exact match
{
"query": {
"match": {
"email.keyword": "wangcai erha"
}
}
}
Copy the code
Aggregations polymerization
You're going to do an aggregation and you're going to give this aggregation a name what kind of aggregation are you going to do the content of the aggregation the metadata in the aggregation based on the last aggregation, sub-aggregationCopy the code
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 10
}
}
}
}
Copy the code
Aggregate by all possible values of age
You can aggregate multiple values, age of all possible values, average wage
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 10
}
},
"salayAvg": {
"avg": {
"field": "salary"
}
}
}
}
Copy the code
Aggregate inside again set aggregate, the average salary of the person below this age group
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 10
},
"aggs": {
"ageAvg": {
"avg": {
"field": "salary"
}
}
}
}
}
}
Copy the code
Results: