ElasticSearch-常规操作-查询操作
ElasticSearch文档增删查改(CURD)
# 增 PUT wql/_doc/1 { "name":"顾老二", "age":30, "from": "gu", "desc": "皮肤黑、武器长、性格直", "tags": ["黑", "长", "直"] } # 删 DELETE wql/_doc/4 # 改 # 修改1:如果之前没有,就是新增,如果之前有,会使用新的覆盖掉旧的(新的字段少,少的字段会被删除) PUT wql/_doc/1 { "desc":"皮肤很黄,武器很长,性格很直", "tags":["很黄","很长", "很直"] } # 修改2---这个多 POST wql/_doc/2/_update { "doc": { "desc": "皮肤很黄,武器很长,性格很直", "tags": ["很黄","很长", "很直"] } } # 查(复杂) # 根据id查,查单条 GET wql/_doc/1
ElasticSearch查询的两种方式
# 查询方式一: GET wql/_search?q=from:gu GET wql/_search?q=age:30 # 查询方式二:结构化查询,查询要放到query字典中,使用match查询,条件有且只有一个 GET wql/_search { "query": { "match": { "from": "gu" } } }
term与match查询
# term和match有什么区别? -match:进行搜索的时候,会先进行分词,分词完后,再来匹配,全文检索用它 -term:term是代表完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词 -如果查的类型是keyword类型,用term比较好 -如果全文检索,text类型,用match较好 # match_all:查询所有 GET wql/_search { "query": { "match_all": { } } } # 短语查询之match_phrase 中国和世界之间最多有2个字符 GET t1/_search { "query": { "match_phrase": { "title": { "query": "中国世界", "slop": 2 } } } } # 最左前缀match_phrase_prefix -查询一bea开头的单词 GET t3/_search { "query": { "match_phrase_prefix": { "desc": "bea" } } }
ElasticSearch排序查询
GET wql/_search { "query": { "match": { "from": "gu" } }, "sort": [ { "age": { "order": "desc" } } ] } # 不是所有字段都可以排序,只有数字字段可以
ElasticSearch分页查询
GET wql/_search { "query": { "match_all": {} }, "sort": [ { "age": { "order": "desc" } } ], "from": 0, "size": 2 }
ElasticSearch布尔查询
# 多个条件的查询 must(and) should(or) must_not(not) filter # and条件 GET wql/_search { "query": { "bool": { "must": [ { "match": { "from": "gu" } }, { "match": { "age": "28" } } ] } } } ## or条件 GET wql/_search { "query": { "bool": { "should": [ { "match": { "from": "gu" } }, { "match": { "tags": "闭月" } } ] } } } # 取反 GET wql/_search { "query": { "bool": { "must_not": [ { "match": { "from": "gu" } }, { "match": { "tags": "可爱" } }, { "match": { "age": 18 } } ] } } } # filter GET wql/_search { "query": { "bool": { "must": [ { "match_all": { } } ], "filter": { "range": { "age": { "lte": 25 } } } } } } # 小结 must:与关系,相当于关系型数据库中的and。 should:或关系,相当于关系型数据库中的or。 must_not:非关系,相当于关系型数据库中的not。 filter:过滤条件。 range:条件筛选范围。 gt:大于,相当于关系型数据库中的>。 gte:大于等于,相当于关系型数据库中的>=。 lt:小于,相当于关系型数据库中的<。 lte:小于等于,相当于关系型数据库中的<=。
ElasticSearch查询结果过滤
GET wql/_search { "query": { "match": { "name": "顾老二" } }, "_source": ["name", "age"] }
ElasticSearch高亮查询
# 基本使用 GET wql/_search { "query": { "match": { "name": "石头" } }, "highlight": { "fields": { "name": {} } } } # 自定义高亮显示 GET wql/_search { "query": { "match": { "name": "石头" } }, "highlight": { "pre_tags": "<b class='key' style='color:red'>", "post_tags": "</b>", "fields": { "name": {} } } } <b class='key' style='color:red'>石</b><b class='key' style='color:red'>头</b>
ElasticSearch聚合函数
# 聚合函数 avg max min sum GET wql/_search { "query": { "match_all": { } }, "aggs": { "my_avg": { "sum": { "field": "age" } } } } # 分组 GET wql/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "age_group": { "range": { "field": "age", "ranges": [ { "from": 15, "to": 20 }, { "from": 20, "to": 25 }, { "from": 25, "to": 30 } ] } } } }
ElasticSearch for Python之连接
# import requests # # res=requests.get('http://localhost:9200/lqz/_doc/2').json() # print(res['_source']) # es官方提供的操作的包,es的客户端 from elasticsearch import Elasticsearch # es是7版本,模块也要用7版本 obj = Elasticsearch(hosts=['http://127.0.0.1:9200', ]) # 创建索引(Index) # result = obj.indices.create(index='user',ignore=[400, 404]) # result = obj.indices.delete(index='user', ignore=[400, 404]) # print(result) # 插入数据 # data = {'userid': '1', 'username': 'lqz','password':'123'} # result = obj.create(index='user1',id=2, document=data) # print(result) # 更新数据 # data = {'password': '888', 'test': '88'} # result = obj.update(index='user1', doc=data, id=2) # print(result) # 删除数据 # result = obj.delete(index='user1',id=1) # 查询 # 查找所有文档 # query = {'query': {'match_all': {}}} # 查找名字叫做jack的所有文档 query = {'query': {'term': {'from': 'gu'}}} # 查找年龄大于11的所有文档 # query = {'query': {'range': {'age': {'gt': 11}}}} # allDoc = obj.search(index='lqz', body=query) # print(allDoc) print(allDoc['hits']['hits'][0]['_source'])
ElasticSearch高级之 集群搭建,数据分片
...