首页 > 编程知识 正文

es 精确查询,es查询方法

时间:2023-05-05 02:52:45 阅读:178610 作者:3463

文章目录1.dsl检索数据准备1. mapping准备2 .数据查询1.dsl检索1 .用URL构建(QueryString方式)用post构建) dsl方式)3.查询4 .查询关键词term、 terms )无分词)全部4 .查询关键词match_phrase4.查询关键词operator4.基于查询关键词minimum_should_match5.id基于ids6.单词

一. dsl检索数据准备1 .映射准备

{ ' properties ' : { ' id ' : ' long ' }、' age ' : { ' type ' : ' integer ' }、'、' username ' analyzer': 'ik_max_word' },' money ' : ' ' type ' : ' ' analyzer ' 3360 ' ik _ max _ word ' },'。 ' face':{'type':'texyte},' face ' 3360 { ' type ' 3360 ' tex yte } ' age ' :' username': ',' ' birrer t=158106664 ()、id(:1001,age ) :18、username ) :giteeamazing,nickname ) 3360、开源中国) ' birthday': '1992-12-24 ', 学习了' face ' : ' https://gitee.com/static/images/logo -的t=158106664 ()、id(:1002、age ) :19、username money 'sex': 1、' birthday': '1993-01-24 '、 这是' face ' : ' https://gitee.com/static/images/logo-black .t=158106664 (),id(:1003,age ) :20,ususstid monename新玛蒂与欧洲,' sex': 1,' birthday': '1996-01-14 ',' face ' : ' https://gitee.com/static/IIC age ) :22、username ) :flyfish、NickName ) 3360、money后来去慕课网上课了'、' sex': 0、' birthday ' 3: ' face ' : ' https://gitee.com/static/images/loog es t=158106664 (),id(:1005,age': 25,username653: ) 非常开心。 很好。 ',' sex': 1,' birthday': '1989-03-14 ',' fa

ce": "https://gitee.com/static/images/logo-black.svg?t=158106664"}{ "id": 1006, "age": 19, "username": "missgitee", "nickname": "我叫跳跃的微笑", "money": 156.8, "desc": "我叫辛勤的楼房,今年20岁,是一名律师,我在琦星球做演讲", "sex": 1, "birthday": "1993-04-14", "face": "https://gitee.com/static/images/logo-black.svg?t=158106664"}{ "id": 1007, "age": 19, "username": "msgame", "nickname": "gamexbox", "money": 1056.8, "desc": "明天去进货,最近微软处理很多游戏机,还要买xbox游戏卡带", "sex": 1, "birthday": "1985-05-14", "face": "https://gitee.com/static/images/logo-black.svg?t=158106664"}{ "id": 1008, "age": 19, "username": "kaiyuan", "nickname": "开源学习", "money": 1056.8, "desc": "大学毕业后,可以到gitee.com进修", "sex": 1, "birthday": "1995-06-14", "face": "https://gitee.com/static/images/logo-black.svg?t=158106664"}{ "id": 1009, "age": 22, "username": "shaonian", "nickname": "骚年轮", "money": 96.8, "desc": "骚年在大学毕业后,考研究生去了", "sex": 1, "birthday": "1998-07-14", "face": "https://gitee.com/static/images/logo-black.svg?t=158106664"}{ "id": 1010, "age": 30, "username": "tata", "nickname": "bqdfd", "money": 100.8, "desc": "隔壁老外去国外出差,带给我很多好吃的", "sex": 1, "birthday": "1988-07-14", "face": "https://gitee.com/static/images/logo-black.svg?t=158106664"}{ "id": 1011, "age": 31, "username": "sparder", "nickname": "皮特帕克", "money": 180.8, "desc": "它是一个超级英雄", "sex": 1, "birthday": "1989-08-14", "face": "https://gitee.com/static/images/logo-black.svg?t=158106664"}{ "id": 1012, "age": 31, "username": "super hero", "nickname": "super hero", "money": 188.8, "desc": "BatMan,GreenArrow,SpiderMan,IronMan... are all Super Hero", "sex": 1, "birthday": "1980-08-14", "face": "https://gitee.com/static/images/logo-black.svg?t=158106664"}

二、dsl搜索 1. 通过url构建(QueryString方式)

http://47.107.41.60:9200/shop/_search?q=desc:慕课网&q=age:20

1. 通过post构建(DSL方式)

路径: http://47.107.41.60:9200/shop/_doc/_search

参数

{ "query":{ "match":{ "desc":"慕课网" } }}

查询结果:

{ "took": 9, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 1.4819028, "hits": [ { "_index": "shop", "_type": "_doc", "_id": "1004", "_score": 1.4819028, "_source": { "id": 1004, "age": 22, "username": "flyfish", "nickname": "水中鱼", "money": 55.8, "desc": "昨天在学校的池塘里,看到有很多鱼在游泳,然后就去慕课网上课了", "sex": 0, "birthday": "1988-02-14", "face": "https://gitee.com/static/images/logo-black.svg?t=158106664" } }, { "_index": "shop", "_type": "_doc", "_id": "1002", "_score": 0.83427787, "_source": { "id": 1003, "age": 20, "username": "bigFace", "nickname": "飞翔的巨鹰", "money": 66.8, "desc": "慕课网团队和导游坐飞机去海外旅游,去了新马泰和欧洲", "sex": 1, "birthday": "1996-01-14", "face": "https://gitee.com/static/images/logo-black.svg?t=158106664" } }, { "_index": "shop", "_type": "_doc", "_id": "1003", "_score": 0.83427787, "_source": { "id": 1003, "age": 20, "username": "bigFace", "nickname": "飞翔的巨鹰", "money": 66.8, "desc": "慕课网团队和导游坐飞机去海外旅游,去了新马泰和欧洲", "sex": 1, "birthday": "1996-01-14", "face": "https://gitee.com/static/images/logo-black.svg?t=158106664" } } ] }}

查找某个字段是否存在

3. 查询所有 http://47.107.41.60:9200/shop/_doc/_search(方式1)match_all(方式2)
查询某个字段
分页
from:0 // 从第0条开始
size:10 // 每页大小
4. 查询关键字term、terms(不分词)


terms允许数组的形式

4. 查询关键字match_phrase match_phrase还是分词后去搜的目标文档需要包含分词后的所有词目标文档还要保持这些词的相对顺序和文档中的一致


slop为允许跳过词的数量

4. 查询关键字operator

operator里可以指明是and还是or,and是分词里的词要都匹配,or是匹配一个就行

4. 查询关键字minimum_should_match

多于百分之60将会被检测出来


多于2个将会被匹配出来

5. 根据id就行查询ids

6. 根据词语搜索多个字段里是否包含(multi_match)


提高某个字段的权重boost
eg: 提高nickname字段的权重,可以在字段的后面加上^10


7. 布尔查询

可以组合多重查询

must:查询必须匹配搜索条件,譬如 andshould:查询匹配满足1个以上条件,譬如 ormust_not:不匹配搜索条件,一个都不要满足
操作


还可以使用boost进行加权

8. 过滤器(post_filter)


代表的是检索完成后进行的过滤,效率高,对结果进行过滤,也不会进行分值的计算
range代表范围
gt代表大于
lt代表小于
gte: 大于等于
lte: 小于等于

9. 排序 es的排序同sql,可以desc也可以asc。也支持组合排序。

对text进行排序,会报错,通常我们可以为这个字段增加额外的一个附属属性,类型为keyword,用于做排序。

主属性.keyword

10.高亮显示

添加highlight,同时可以指定前缀后缀

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。