elasticsearch python using conventional methods (retrieval)

Record # es query method

#清楚数据
curl -XDELETE http://xx.xx.xx.xx:9200/test6

#初始化数据
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/1' -d '{"name": "tom", "age":18, "info": "tom"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/2' -d '{"name": "jack", "age":29, "info": "jack"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/3' -d '{"name": "jetty", "age":18, "info": "jetty"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/4' -d '{"name": "daival", "age":19, "info": "daival"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/5' -d '{"name": "lilei", "age":18, "info": "lilei"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/6' -d '{"name": "lili", "age":29, "info": "lili"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/7' -d '{"name": "tom1", "age":30, "info": "tom1"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/8' -d '{"name": "tom2", "age":31, "info": "tom2"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/9' -d '{"name": "tom3", "age":32, "info": "tom3"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/10' -d '{"name": "tom4", "age":33, "info": "tom4"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/11' -d '{"name": "tom5", "age":34, "info": "tom5"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/12' -d '{"name": "tom6", "age":35, "info": "tom6"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/13' -d '{"name": "tom7", "age":36, "info": "tom7"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/14' -d '{"name": "tom8", "age":37, "info": "tom8"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/15' -d '{"name": "tom9", "age":38, "info": "tom9"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/16' -d '{"name": "john", "age":38, "info": "john"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/17' -d '{"name": "marry", "age":38, "info": "marry and john are friend"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/18' -d '{"name": "john", "age":32, "info": "john"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/19' -d '{"name": "tom is a little boy", "age":7, "info": "tom is a little boy"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/20' -d '{"name": "tom is a student", "age":12, "info": "tom is a student"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/21' -d '{"name": "jack is a little boy", "age":22, "info": "jack"}'
 

from elasticsearch Import elasticsearch

DEF the foreach (DOC):
DOC = RES [ 'Hits'] [' Hits']

IF len (DOC):
for Item in DOC:
Print (Item [ '_ Source'])

ES = elasticsearch ([ 'XX .xx.xx.xx: 9200 '])

# Search data
# methods. 1
#res = es.search (index =' test6 ', size = 20 is)
method # 2
RES = es.search (index =' test6 ', = 20 is size, body = {
"query": {
"MATCH_ALL": {}
}
})

#foreach (RES)


# query term and equal terms, the query name = 'tom cat' this value must not be completely contained word
res = es.search (index = 'test6', size = 20 is, body = {
"Query": {
"Term": {
"name": "tom cat"
}
}
})
#foreach (RES)

# query term and equal terms, the query name = 'tom' = or name 'Lili'
RES = es.search (index = 'test6', size = 20 is, body = {
"Query": {
"Terms ": {
" name ": [" Tom "," Lili "]
}
}
})
#foreach (RES)

# include query, match with multi_match
# match: name matching with" data tom cat "keyword, the word will be tom or a cat comprising
RES = es.search (index = 'test6', size = 20 is, body = {
"Query": {
"match": {
"name": "tom cat"
}
}
})
#foreach (RES )

#multi_match: match the name or keyword data in the info contained little of
res = es.search(index='test6', size=20, body={
"query": {
"multi_match": {
"Query": "Little",
"Fields": [ "name", "info"]
}
}
})
#foreach (RES)


#ids, query id data 1, 2 corresponds to the mysql in
RES = es.search (index = 'test6', size = 20 is, body = {
"query": {
"IDS": {
"values": [ ". 1", "2"]
}
}
})
#foreach (RES)


# compound query bool , bool there are three types of query relations, must (meet), should (one of which meet), must_not (not satisfied)
to the #NAME comprising "tom" and term containing "18 is"
RES = es.search (index = 'test6' , size = 20 is, body = {
"Query": {
"BOOL": {
"MUST":[
{
"term": {
"name": "tom",
},

},
{
"term": {
"age": 18,
},

},
]
}
}
})
#foreach(res)

#name包含"tom" or term包含"19"
res = es.search(index='test6', size=20, body={
"query": {
"bool": {
"should": [
{
"term": {
"name": "tom",
},

},
{
"term": {
"age": 19,
},

},
]
}
}
})

#foreach(res)


#切片式查询
res = es.search(index='test6', size=20, body={
"query": {
"bool": {
"should": [
{
"term": {
"name": "tom",
},

},
{
"term": {
"age": 19,
},

},
]
}
},
"from ": 2, # data from the second start
" size ": 4, # 4 data acquisition
})
#foreach(res)

#范围查询
res = es.search(index='test6', size=20, body={
"query": {
"range": {
"age": {
"gte": 18, #>=18
"lte": 30 #<=30
}
}
}
})
#foreach(res)


#前缀查询
res = es.search(index='test6', size=20, body={
"query": {
"prefix": {
"name": "tom"
}
}
})
#foreach(res)


#通配符查询
res = es.search(index='test6', size=20, body={
"query": {
"wildcard": {
"name": "*i"
}
}
})
#Foreach (RES)


# sort
RES = es.search (index = 'test6', size = 20 is, body = {
"Query": {
"wildcard": {
"name": "I *"
}
},
"Sort": {
"Age": {
"Order": "desc" # descending
}
}
})
#foreach (RES)


# COUNT, executes the query and obtain the number of query matches
c = es.count (index = 'test6 ')
Print (C)

Guess you like

Origin www.cnblogs.com/loveyouyou616/p/11573080.html