常用的Elasticseaerch检索技巧汇总
本篇博客是对前期工作中遇到ES坑的一些小结,顺手记录下,方便日后查阅。
0、前言
为了讲解不同类型ES检索,我们将要对包含以下类型的文档集合进行检索:
. title 标题;
. authors 作者;
. summary 摘要;
. release data 发布日期;
. number of reviews 评论数。
首先,让我们借助 bulk API批量创建新的索引并提交数据。
PUT /bookdb_index
{ "settings": { "number_of_shards": }} POST /bookdb_index/book/_bulk
{ "index": { "_id": }}
{ "title": "Elasticsearch: The Definitive Guide", "authors": ["clinton gormley", "zachary tong"], "summary" : "A distibuted real-time search and analytics engine", "publish_date" : "2015-02-07", "num_reviews": , "publisher": "oreilly" }
{ "index": { "_id": }}
{ "title": "Taming Text: How to Find, Organize, and Manipulate It", "authors": ["grant ingersoll", "thomas morton", "drew farris"], "summary" : "organize text using approaches such as full-text search, proper name recognition, clustering, tagging, information extraction, and summarization", "publish_date" : "2013-01-24", "num_reviews": , "publisher": "manning" }
{ "index": { "_id": }}
{ "title": "Elasticsearch in Action", "authors": ["radu gheorge", "matthew lee hinman", "roy russo"], "summary" : "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms", "publish_date" : "2015-12-03", "num_reviews": , "publisher": "manning" }
{ "index": { "_id": }}
1、基本匹配检索( Basic Match Query)
1.1 全文检索
有两种方式可以执行全文检索:
1)使用包含参数的检索API,参数作为URL的一部分。
举例:以下对”guide”执行全文检索。
GET /bookdb_index/book/_search?q=guide
[Results]
"hits": [
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.28168046,
"_source": {
"title": "Elasticsearch: The Definitive Guide",
"authors": [
"clinton gormley",
"zachary tong"
],
"summary": "A distibuted real-time search and analytics engine",
"publish_date": "2015-02-07",
"num_reviews": ,
"publisher": "manning"
}
},
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.24144039,
"_source": {
"title": "Solr in Action",
"authors": [
"trey grainger",
"timothy potter"
],
"summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
"publish_date": "2014-04-05",
"num_reviews": ,
"publisher": "manning"
}
}
]
2)使用完整的ES DSL,其中Json body作为请求体。
其执行结果如方式1)结果一致。
{
"query": {
"multi_match" : {
"query" : "guide",
"fields" : ["_all"]
}
}
}
解读:使用multi_match关键字代替match关键字,作为对多个字段运行相同查询的方便的简写方式。 fields属性指定要查询的字段,在这种情况下,我们要对文档中的所有字段进行查询。
1.2 指定特定字段检索
这两个API也允许您指定要搜索的字段。 例如,要在标题字段中搜索带有“in action”字样的图书,
1)URL检索方式
如下所示:
GET /bookdb_index/book/_search?q=title:in action [Results]
"hits": [
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.6259885,
"_source": {
"title": "Solr in Action",
"authors": [
"trey grainger",
"timothy potter"
],
"summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
"publish_date": "2014-04-05",
"num_reviews": ,
"publisher": "manning"
}
},
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.5975345,
"_source": {
"title": "Elasticsearch in Action",
"authors": [
"radu gheorge",
"matthew lee hinman",
"roy russo"
],
"summary": "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
"publish_date": "2015-12-03",
"num_reviews": ,
"publisher": "manning"
}
}
]
2)DSL检索方式
然而,full body的DSL为您提供了创建更复杂查询的更多灵活性(我们将在后面看到)以及指定您希望的返回结果。 在下面的示例中,我们指定要返回的结果数、偏移量(对分页有用)、我们要返回的文档字段以及属性的高亮显示。
结果数的表示方式:size;
偏移值的表示方式:from;
指定返回字段 的表示方式 :_source;
高亮显示 的表示方式 :highliaght。
POST /bookdb_index/book/_search
{
"query": {
"match" : {
"title" : "in action"
}
},
"size": ,
"from": ,
"_source": [ "title", "summary", "publish_date" ],
"highlight": {
"fields" : {
"title" : {}
}
}
} [Results]
"hits": {
"total": ,
"max_score": 0.9105287,
"hits": [
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.9105287,
"_source": {
"summary": "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
"title": "Elasticsearch in Action",
"publish_date": "2015-12-03"
},
"highlight": {
"title": [
"Elasticsearch <em>in</em> <em>Action</em>"
]
}
},
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.9105287,
"_source": {
"summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
"title": "Solr in Action",
"publish_date": "2014-04-05"
},
"highlight": {
"title": [
"Solr <em>in</em> <em>Action</em>"
]
}
}
]
}
注意:对于 multi-word 检索,匹配查询允许您指定是否使用‘and’运算符,
而不是使用默认’or’运算符。
您还可以指定minimum_should_match选项来调整返回结果的相关性。
详细信息可以在Elasticsearch指南中查询Elasticsearch guide. 获取。
2、多字段检索 (Multi-field Search)
如我们已经看到的,要在搜索中查询多个文档字段(例如在标题和摘要中搜索相同的查询字符串),请使用multi_match查询。
POST /bookdb_index/book/_search
{
"query": {
"multi_match" : {
"query" : "elasticsearch guide",
"fields": ["title", "summary"]
}
}
} [Results]
"hits": {
"total": ,
"max_score": 0.9448582,
"hits": [
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.9448582,
"_source": {
"title": "Elasticsearch: The Definitive Guide",
"authors": [
"clinton gormley",
"zachary tong"
],
"summary": "A distibuted real-time search and analytics engine",
"publish_date": "2015-02-07",
"num_reviews": ,
"publisher": "manning"
}
},
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.17312013,
"_source": {
"title": "Elasticsearch in Action",
"authors": [
"radu gheorge",
"matthew lee hinman",
"roy russo"
],
"summary": "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
"publish_date": "2015-12-03",
"num_reviews": ,
"publisher": "manning"
}
},
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.14965448,
"_source": {
"title": "Solr in Action",
"authors": [
"trey grainger",
"timothy potter"
],
"summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
"publish_date": "2014-04-05",
"num_reviews": ,
"publisher": "manning"
}
}
]
}
注意:以上结果3匹配的原因是guide在summary存在。
3、 Boosting提升某字段得分的检索( Boosting)
由于我们正在多个字段进行搜索,我们可能希望提高某一字段的得分。 在下面的例子中,我们将“摘要”字段的得分提高了3倍,以增加“摘要”字段的重要性,从而提高文档 4 的相关性。
POST /bookdb_index/book/_search
{
"query": {
"multi_match" : {
"query" : "elasticsearch guide",
"fields": ["title", "summary^3"]
}
},
"_source": ["title", "summary", "publish_date"]
} [Results]
"hits": [
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.31495273,
"_source": {
"summary": "A distibuted real-time search and analytics engine",
"title": "Elasticsearch: The Definitive Guide",
"publish_date": "2015-02-07"
}
},
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.14965448,
"_source": {
"summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
"title": "Solr in Action",
"publish_date": "2014-04-05"
}
},
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.13094766,
"_source": {
"summary": "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
"title": "Elasticsearch in Action",
"publish_date": "2015-12-03"
}
}
]
注意:Boosting不仅意味着计算得分乘法以增加因子。 实际的提升得分值是通过归一化和一些内部优化。参考 Elasticsearch guide.查看更多。
4、Bool检索( Bool Query)
可以使用AND / OR / NOT运算符来微调我们的搜索查询,以提供更相关或指定的搜索结果。
在搜索API中是通过bool查询来实现的。
bool查询接受”must”参数(等效于AND),一个must_not参数(相当于NOT)或者一个should参数(等同于OR)。
例如,如果我想在标题中搜索一本名为“Elasticsearch”或“Solr”的书,AND由“clinton gormley”创作,但NOT由“radu gheorge”创作:
POST /bookdb_index/book/_search
{
"query": {
"bool": {
"must": {
"bool" : { "should": [
{ "match": { "title": "Elasticsearch" }},
{ "match": { "title": "Solr" }} ] }
},
"must": { "match": { "authors": "clinton gormely" }},
"must_not": { "match": {"authors": "radu gheorge" }}
}
}
} [Results]
"hits": [
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.3672021,
"_source": {
"title": "Elasticsearch: The Definitive Guide",
"authors": [
"clinton gormley",
"zachary tong"
],
"summary": "A distibuted real-time search and analytics engine",
"publish_date": "2015-02-07",
"num_reviews": ,
"publisher": "oreilly"
}
}
]
注意:您可以看到,bool查询可以包含任何其他查询类型,包括其他布尔查询,以创建任意复杂或深度嵌套的查询。
5、 Fuzzy 模糊检索( Fuzzy Queries)
在 Match检索 和多匹配检索中可以启用模糊匹配来捕捉拼写错误。 基于与原始词的Levenshtein距离来指定模糊度。
POST /bookdb_index/book/_search
{
"query": {
"multi_match" : {
"query" : "comprihensiv guide",
"fields": ["title", "summary"],
"fuzziness": "AUTO"
}
},
"_source": ["title", "summary", "publish_date"],
"size":
} [Results]
"hits": [
{
"_index": "bookdb_index",
"_type": "book",
"_id": "",
"_score": 0.5961596,
"_source": {
"summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
"title": "Solr in Action",
"publish_date": "2014-04-05"
}
}
]
“AUTO”的模糊值相当于当字段长度大于5时指定值2。但是,设置80%的拼写错误的编辑距离为1,将模糊度设置为1可能会提高整体搜索性能。 有关更多信息, Typos and Misspellingsch 。
https://blog.csdn.net/laoyang360/article/details/76769208 从6开始
常用的Elasticseaerch检索技巧汇总的更多相关文章
- es6常用数组操作及技巧汇总
定义数组 const array = [1, 2, 3]; // 或者 const array = new Array(); array[0] = '1'; 检测数组 Array.isArray([] ...
- WebStorm 常用功能的使用技巧分享
WebStorm 是 JetBrain 公司开发的一款 JavaScript IDE,使用非常方便,可以使编写代码过程更加流畅. 本文在这里分享一些常用功能的使用技巧,希望能帮助大家更好的使用这款强大 ...
- CSS 技巧汇总
CSS 选择符优先级 !important 声明>内联样式(style)>id 选择符(#id)>类选择符(.class)=伪类选择符(:hover )=属性选择符([attr] ) ...
- UWP开发必备:常用数据列表控件汇总比较
今天是想通过实例将UWP开发常用的数据列表做汇总比较,作为以后项目开发参考.UWP开发必备知识点总结请参照[UWP开发必备以及常用知识点总结]. 本次主要讨论以下控件: GridView:用于显示数据 ...
- Oracle手边常用70则脚本知识汇总
Oracle手边常用70则脚本知识汇总 作者:白宁超 时间:2016年3月4日13:58:36 摘要: 日常使用oracle数据库过程中,常用脚本命令莫不是用户和密码.表空间.多表联合.执行语句等常规 ...
- C/C++常用头文件及函数汇总
转自: C/C++常用头文件及函数汇总 C/C++头文件一览 C #include <assert.h> //设定插入点#include <ctype.h> //字符处理#in ...
- ASP.NET(C#)常用数据加密和解密方法汇总
一. 数据加密的概念 1. 基本概念 2. 基本功能 3. 加密形式 二. 数据加密的项目应用和学习 1. 媒体加密:DRM 2. 文件加密:文本 ...
- 移动平台3G手机网站前端开发布局技巧汇总
移动平台3G手机网站前端开发布局技巧汇总 作者:前端开发-武方博 发布:2011-05-10 09:11 分类:移动开发 阅读:120,618 views 7条评论 您或许正在 ...
- 移动平台WEB前端开发技巧汇总(转)
最近我很关注移动前端的知识,但做为一个UI设计师和web前端工作人员没有这个工作环境接触,做为门外汉,网上系统的知识也了了,一直有种雾里看花的感觉,见到本文,我自己是奉为经典.所以我分享之后又专门打笔 ...
随机推荐
- 使用@Scheduled注解编写spring定时任务
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframewor ...
- ES中DSL查询相关
elasticsearch中的API:http://www.cnblogs.com/yjf512/p/4862992.html elasticsearch查询系列:http://blog.csdn.n ...
- C#根据Type获取默认值
简单的获取某变量类型的默认值 在c#中为我们提供了default(),但是default的参数是具体的类名, 如何根据变量类型的Type获取默认值Code如下: 1 public static obj ...
- 九度OJ 1027:欧拉回路 (欧拉回路)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2989 解决:1501 题目描述: 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是 ...
- Cocos2d-js官方完整项目教程翻译:六、添加Chipmunk物理引擎在我们的游戏世界里
添加Chipmunk物理引擎在我们的游戏世界里 一.简介 cocos2d JS能给我们力量来创造令人印象深刻的游戏世界.但缺乏某种现实. ...
- 我的Android进阶之旅------>对Java中注释/**@hide*/的初步认识
今天写一个调节系统背光亮度的时候,参考了Android中的Setting源码,在源码中有这么一段代码: private static final int MAXIMUM_BACKLIGHT = and ...
- java参数传递------真心是值传递
未完待续 不同意的请尽管去深入看一下. 对象是引用传递没错,参数传递是值传递.
- RaspBerry Pi3 ~ 内核编译
RaspBerryPi3-内核编译 转载注明出处:http://www.cnblogs.com/einstein-2014731/p/5985128.html 在有道云笔记的同步分享:http://n ...
- UML类图几种关系的总结 ---(转载)
在UML类图中,常见的有以下几种关系:泛化(Generalization), 实现(Realization),关联(Association),聚合(Aggregation),组合(Compositi ...
- CentOS 7 设置自定义开机启动,添加自定义系统服务
详细文档,http://www.linuxidc.com/Linux/2015-04/115937.htm 摘自: http://www.centoscn.com/CentOS/config/2015 ...