python+ elasticsearch + lucene使用

elasticsearch,lucene结合使用

案例

from elasticsearch import Elasticsearch
es = Elasticsearch([{'host':'ip地址','port':9200}, timeout=1000]) # 连接es,设置超时时间1000秒 
'''如果需要用户名密码认证,加上  http_auth=('root', '111111') 参数   root用户名 111111 密码'''

查询案例

res=es.search(index='test', q='name:hihi OR name:lucy')['hits']['hits'] # 查询索引为test下,其中name为haha 或者name首字母为a,b,c的所有文档。
''' 如果想要指定文档 加上 doc_type='test_log' 参数  test_log为指定文档的名称'''
##### 查询结果
[{'_index': 'test', '_type': 'test_one', '_id': 'AXfJH3sRISkIKOZVU8d1', '_score': 0.2876821, '_source': {'name': 'lucy', 'sex': 'girl', 'work': 'singer', 'like': 'sing'}}, {'_index': 'test', '_type': 'test_one', '_id': '2', '_score': 0.2876821, '_source': {'name': 'hihi', 'sex': 'boy', 'work': 'sleep', 'like': 'sleep'}}]
es常用参数
index - 索引名
q - 查询指定匹配 使用Lucene查询语法
from_ - 查询起始点  默认0
doc_type - 文档类型
size - 指定查询条数 默认10
field - 指定字段 逗号分隔
sort - 排序  字段:asc/desc
body - 使用Query DSL
scroll - 滚动查询
lucene语法
代码案例( {} [] )
{}尖括号表示不包含最小值和最大值
[]方括号表示包含最小值和最大值
eg:
	{3 TO 6} --------- 4,5
	[3 TO 6] --------- 3,4,5,6
	{3 TO 6] --------- 4,5,6
	[3 TO 6} --------- 3,4,5
res = es.search(index='test', doc_type='test_one')
res1 = es.search(index='test', doc_type='test_one', q="age:[11 TO 15]")
res2 = es.search(index='test', doc_type='test_one', q="age:{11 TO 15}")
res3 = es.search(index='test', doc_type='test_one', q="age:{11 TO 15]")
res4 = es.search(index='test', doc_type='test_one', q="age:[11 TO 15}")
print(res['hits']['hits'], 'res---------------')
print(res1['hits']['hits'], 'age:[11 TO 15]------------')
print(res2['hits']['hits'], 'age:{11 TO 15}----------')
print(res3['hits']['hits'], 'age:{11 TO 15]----------')
print(res4['hits']['hits'], 'age:[11 TO 15}----------')
结果展示
[{'_index': 'test', '_type': 'test_one', '_id': 'AXfJH3sRISkIKOZVU8d1', '_score': 1.0, '_source': {'name': 'lucy', 'sex': 'girl', 'work': 'singer', 'like': 'sing', 'age': 11}}, {'_index': 'test', '_type': 'test_one', '_id': 'AXfMs8e2ISkIKOZVU8eB', '_score': 1.0, '_source': {'name': 'Cindy', 'sex': 'girl', 'work': 'loo', 'like': 'loo', 'age': 13}}, {'_index': 'test', '_type': 'test_one', '_id': 'AXfMtn7WISkIKOZVU8eC', '_score': 1.0, '_source': {'name': 'Cindy', 'sex': 'girl', 'work': 'loo', 'like': 'loo', 'age': 15}}, {'_index': 'test', '_type': 'test_one', '_id': '2', '_score': 1.0, '_source': {'name': 'hihi', 'sex': 'boy', 'work': 'sleep', 'like': 'sleep', 'age': 17}},
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值