【ES】查询详解

文章目录

        • 一. dsl搜索数据准备
          • 1. mapping准备
          • 2. 查入数据
        • 二、dsl搜索
          • 1. 通过url构建(QueryString方式)
          • 1. 通过post构建(DSL方式)
          • 3. 查询所有
          • 4. 查询关键字term、terms(不分词)
          • 4. 查询关键字match_phrase
          • 4. 查询关键字operator
          • 4. 查询关键字minimum_should_match
          • 5. 根据id就行查询ids
          • 6. 根据词语搜索多个字段里是否包含(multi_match)
          • 7. 布尔查询
          • 8. 过滤器(post_filter)
          • 9. 排序
          • 10.高亮显示

一. dsl搜索数据准备

1. mapping准备

在这里插入图片描述

{"properties": {"id": {"type": "long"},"age": {"type": "integer"},"username": {"type": "keyword"},"nickname":{"type": "text","analyzer": "ik_max_word"},"money": {"type": "float"},"desc": {"type": "text","analyzer": "ik_max_word"},"sex": {"type": "byte"},"face": {"type": "text","index": false}}
}
2. 查入数据
{"id": ,"age": ,"username": "","nickname": "","money": ,"desc": "","sex": ,"birthday": "","face": "https://gitee.com/static/images/logo-black.svg?t=158106664"
}{"id": 1001,"age": 18,"username": "giteeAmazing","nickname": "开源中国","money": 88.8,"desc": "我的开源中国学习java和前端,学习了很多知识","sex": 0,"birthday": "1992-12-24","face": "https://gitee.com/static/images/logo-black.svg?t=158106664"
}{"id": 1002,"age": 19,"username": "justbuy","nickname": "周杰棍","money": 77.8,"desc": "今天上下班都很堵,车流量很大","sex": 1,"birthday": "1993-01-24","face": "https://gitee.com/static/images/logo-black.svg?t=158106664"
}{"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"
}{"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"
}{"id": 1005,"age": 25,"username": "gotoplay","nickname": "ps游戏机","money": 155.8,"desc": "今年生日,女友送了我一台play station游戏机,非常好玩,非常不错","sex": 1,"birthday": "1989-03-14","face": "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": "隔壁老王","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. 查询所有
  1. http://47.107.41.60:9200/shop/_doc/_search(方式1)
  2. match_all(方式2)
    在这里插入图片描述
  3. 查询某个字段
    在这里插入图片描述
  4. 分页
    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:查询必须匹配搜索条件,譬如 and
  • should:查询匹配满足1个以上条件,譬如 or
  • must_not:不匹配搜索条件,一个都不要满足
    操作

在这里插入图片描述
还可以使用boost进行加权
在这里插入图片描述

8. 过滤器(post_filter)

在这里插入图片描述
在这里插入图片描述

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

9. 排序
  • es的排序同sql,可以desc也可以asc。也支持组合排序。
    在这里插入图片描述
    对text进行排序,会报错,通常我们可以为这个字段增加额外的一个附属属性,类型为keyword,用于做排序。

主属性.keyword
在这里插入图片描述

在这里插入图片描述

10.高亮显示

添加highlight,同时可以指定前缀后缀
在这里插入图片描述

在这里插入图片描述


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部