Create an es index library, which is equivalent to creating a table
from elasticsearch import Elasticsearch
from elasticsearch. helpers import bulk
es = Elasticsearch( hosts= 'localhost:9200' )
es. indices. create( index= 'my' , ignore= 400 )
Insert a piece of data into the es index library
data = {
"name" : "小明" , "age" : "8" , "gender" : "男" }
res = es. index( index= 'my' , doc_type= 'doc' , body= data)
Batch insert data into the index library
from elasticsearch. helpers import bulk
data_lis = [ {
"name" : "小红" , "age" : "8" , "gender" : "女" } , {
"name" : "小王" , "age" : "8" , "gender" : "男" } ]
res2 = bulk( es, data_lis, index= 'my' )
Query all data
data = es. search( index= 'my' )
body = {
'query' : {
'match_all' : {
}
}
}
data = es. search( index= 'my' , body= body)
term: query data based on the value of a field
body2 = {
"query" : {
"term" : {
"age" : 8 }
}
}
data_list = es. search( index= 'my' , body= body2)
terms: query data based on multiple values of a field
body3 = {
"query" : {
"terms" : {
"age:[ 8 , 10 ] } } }
age_list = es. search( index= 'my' , body= body3)
match: query based on a field containing a certain character
body4 = {
"query" : {
"match" : {
"name" : "红" }
}
}
red_list = es. search( index= 'my' , body= body4)
multi_match: query based on multiple fields containing a certain character
body = {
"query" : {
"multi_match" : {
"query" : "红" ,
"fields" : [ "name" , "addr" ]
}
}
}
es. search( index= "my" , body= body)
match_phrase: matching tiltle contains quick fox, the two fields must be adjacent
body = {
"query" : {
"match_phrase" : {
"title" : {
"query" : "quick fox" ,
"slop" : 1
}
}
}
}
es. search( index= "my" , body= body)
Query based on id
body = {
"query" : {
"ids" : {
"type" : "doc" ,
"values" : [ "vxGrGGwBv1w0KXkAjsEd" ,
"wBG_GGwBv1w0KXkAcsEn"
]
}
} }
h = es. search( index= "my" , body= body)
Compound query
{
"query" : {
"bool" : {
"must" : [ {
"term" : {
"name" : "小红"
}
} , {
"term" : {
"age" : 18
}
} ]
}
}
}
es. search( index= 'my' , body= body)
range: range query
body = {
"query" : {
"range" : {
"age" : {
"gte" : 18 ,
"lte" : 30
}
}
}
}
es. search( index= "my_index" , doc_type= "test_type" , body= body)
Slice query
body = {
"query" : {
"match_all" : {
}
} ,
"from" : 2 ,
"size" : 4
}
es. search( index= "my" , body= body)
Wildcard query
body = {
"query" :
{
"wildcard" :
{
"name" : "*红"
}
}
}
es. search( index= "my" , body= body)
Prefix query
body = {
"query" : {
"prefix" : {
"name" : "李"
}
}
}
es. search( index= 'my' , body= body)
exists: query whether a field is not null data
body = {
"query" : {
"bool" : {
"must" : {
"exists" : {
"field" : "name"
}
}
}
}
}
es. search( index= 'my' , body= body)
Set the weight of the query field
body = {
"query" : {
"multi_match" : {
"query" : "明 红" ,
"field" : [ "name^2" , 'last_name^1.5' , "first_name^0.9" ]
}
}
}
es. search( index= 'my' , body= body)
Sort
body = {
"query" : {
"match_all" : {
}
} ,
"sort" : {
"age" : {
"order" : "asc"
}
}
}
body = {
"query" : {
"match_all" : {
}
} ,
"sort" : [ {
"age" : {
"order" : "asc"
}
} , {
"name" : {
"order" : "asc"
}
} ] ,
}
es. search( index= 'my' , body= body)
count execute the query and get the number of query matches
body = {
"query" : {
"prefix" : {
"name" : "李"
}
}
}
es. count( index= 'my' , body= body)
aggs: aggregate query to get the oldest age
body = {
"query" : {
"match_all" : {
}
} ,
"aggs" : {
"max_age" : {
"max" : {
"field" : "age"
}
}
}
}
es. search( index= 'my' , body= body)
aggs: aggregate query to get the smallest age
body = {
"query" : {
"match_all" : {
}
} ,
"aggs" : {
"min_age" : {
"min" : {
"field" : "age"
}
}
}
}
es. search( index= 'my' , body= body)
aggs: Aggregate query to get the age of the sum
body = {
"query" : {
"match_all" : {
}
} ,
"aggs" : {
"sum_age" : {
"sum" : {
"field" : "age"
}
}
}
}
es. search( index= 'my' , body= body)
aggs: aggregate query to get the average age
body = {
"query" : {
"match_all" : {
}
} ,
"aggs" : {
"avg_age" : {
"avg" : {
"field" : "age"
}
}
}
}
es. search( index= 'my' , body= body)