python 使用 elasticsearch 經常使用方法(檢索)

#記錄es查詢等方法

#清楚數據
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 foreach(doc):
doc = res['hits']['hits']

if len(doc):
for item in doc:
print(item['_source'])

es = Elasticsearch(['xx.xx.xx.xx:9200'])

#查詢全部數據
#方法1
#res = es.search(index='test6', size=20)
#方法2
res = es.search(index='test6', size=20, body = {
"query": {
"match_all": {}
}
})

#foreach(res)


#等於查詢 term與terms, 查詢 name='tom cat' 這個值不會分詞必須徹底包含
res = es.search(index='test6', size=20, body= {
"query": {
"term": {
"name": "tom cat"
}
}
})
#foreach(res)

#等於查詢 term與terms, 查詢 name='tom' 或 name='lili'
res = es.search(index= 'test6', size= 20, body= {
"query": {
"terms": {
"name": ["tom","lili"]
}
}
})
#foreach(res)

#包含查詢,match與multi_match
# match: 匹配name包含"tom cat"關鍵字的數據, 會進行分詞包含tom或者cat的
res = es.search(index='test6', size=20, body={
"query": {
"match": {
"name": "tom cat"
}
}
})
#foreach(res)

#multi_match: 在name或info裏匹配包含little的關鍵字的數據
res = es.search(index='test6', size=20, body={
"query": {
"multi_match": {
"query": "little",
"fields": ["name", "info"]
}
}
})
#foreach(res)


#ids , 查詢id 1, 2的數據 至關於mysql的 in
res = es.search(index='test6', size=20, body={
"query": {
"ids": {
"values": ["1", "2"]
}
}
})
#foreach(res)


#複合查詢bool , bool有3類查詢關係,must(都知足),should(其中一個知足),must_not(都不知足)
#name包含"tom" and term包含 "18"
res = es.search(index='test6', size=20, 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, #從第二條數據開始
"size": 4, # 獲取4條數據
})
#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)


#排序
res = es.search(index='test6', size=20, body={
"query": {
"wildcard": {
"name": "*i"
}
},
"sort": {
"age": {
"order": "desc" #降序
}
}
})
#foreach(res)


# count, 執行查詢並獲取該查詢的匹配數
c = es.count(index='test6')
print(c)

# 短語匹配 match_phrase (搜索is a little的短語,不進行切分)res = es.search(index='test6', size=20, body={    "query": {        "match_phrase": {            "name": "is a little"        }    }})foreach(res)
相關文章
相關標籤/搜索