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)


转载于:https://www.cnblogs.com/loveyouyou616/p/11573080.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值