es原理
一: 一个请求到达es集群,选中一个coordinate节点以后,会通过请求路由到指定primary shard中,如果分发策略选择为round-robin,如果来4个请求,则2个打到primary shard中2个打到replic shard中。
二: es在多个shard进行分片但数据倾斜严重的时候有可能会发生搜索score不准的情况,因为IDF分值的计算方法实在shard本地完成的;如shard1中数据较多,在计算某一词搜索时的分值时会导致分值整体下降,而这时shard2中出现的词频较少会整体分值偏高,这样容易导致原本不太相关的内容却变得分值高了起来,从而使排序不准;解决方法就是让多个shard在生产环境中尽量做到数据均衡分布,这样就不会因为score的本地计算而整体受影响。
三: es计算分值时有两种策略:
1)most-field->默认策略是全文检索的所有关键词,在document的每一个field中可匹配的次数越多则分值越高;规则:(每个match中field匹配分值的和) *(实际document匹配到了字段个数)/(query中match的个数) ,如下代码:
GET /index3/type3/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title":"spark"//title中可匹配成功
}
},
{
"match": {
"content":"java"//content中也可匹配成功
}
}
]
}
}
}
2)beast-field->如果使用dis_max,document的分值则会根据match中field匹配分值最高的决定,也就是说和其他属性无关
GET /index3/type3/_search
{
"query": {
"dis_max": {
"queries": [
{
"match": {
"title": "spark"
}
},
{
"match": {
"content": "java"
}
}
]
}
}
3)es中除了most_fields和beast_fields以外,使用cross_fields的情况还是比较多的,使用es系统中默认的cross_fields策略实质是将 "fields": ["name","content"]两个字段的内容放到一起后建立索引,这样就能通过一个fullField字段进行fullText,使结果更加准确
搜索参数:
GET /index2/type2/_search
{
"query": {
"multi_match": {
"query": "happening like",
//query中的搜索词条去content和name两个字段中来匹配,不过会由于两个字段mapping定义不同导致得分不同,排序结果可能有差异
"fields": ["name","content"],
//best_fields策略是每个document的得分等于得分最高的match field的值;而匹配出最佳以后,其它document得分未必准确;most_fields根据每个field的评分计算出ducoment的综合评分
"type":"cross_fields",
"operator":"and"
}
}
}
结果:
{
"took": 36,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.84968257,
"hits": [
{
"_index": "index2",
"_type": "type2",
"_id": "2",
"_score": 0.84968257,
"_source": {
"num": 10,
"title": "他的名字",
"name": "yes happening like write",
"content": "happening like"
}
},
{
"_index": "index2",
"_type": "type2",
"_id": "4",
"_score": 0.8164005,
"_source": {
"num": 1000,
"title": "我的名字",
"name": "happening like write",
"content": "happening hello like yeas and he happening like had read a lot about happening hello like"
}
},
{
"_index": "index2",
"_type": "type2",
"_id": "3",
"_score": 0.5063205,
"_source": {
"num": 105,
"title": "这是谁的名字",
"name": "happening like write",
"content": " national treasure because of its rare number and cute appearance. Many foreign people are so crazy about pandas and they can’t watching these lovely creatures all the time. Though some action"
}
}
]
}
}
四:提升全文检索效果的两种方法
1) 使用boost提升检索分值
GET index3/type3/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"content": {
"query": "from",
"boost":5//使用boost将term检索评分提升5倍
}
}
},{
"match": {
"content": {
"query": "foot"//如果不使用boost则搜索foot则会得分较高
}
}
}
]
}
}
}
结果:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.3150566,
"hits": [
{
"_index": "index3",
"_type": "type3",
"_id": "1",
"_score": 1.3150566,
"_source": {
"date": "2019-01-02",
"name": "the little",
"content": "Half the hello book ideas in his talk were plagiarized from an article I wrote last month.",
"no": "123"
}
},
{
"_index": "index3",
"_type": "type3",
"_id": "5",
"_score": 1.3114156,
"_source": {
"date": "2019-05-01",
"name": "http litty",
"content": "There are hello moments in life when you miss book someone so much that you just want to pick them from your dreams",
"no": "564",
"description": "描述"
}
},
{
"_index": "index3",
"_type": "type3",
"_id": "3",
"_score": 0.28582606,
"_source": {
"date": "2019-07-01",
"name": "very tag",
"content": "Some of our hello comrades love book to write long articles with no substance, very much like the foot bindings of a slattern, long as well as smelly",
"no": "123"
}
}
]
}
}
2)使用boosting的positive和negative进行反向筛选,通过设置 (negative_boost:0.5) 降低分值
GET index3/type3/_search
{
"query": {
"boosting": {
//正常匹配的
"positive": {
"match": {
"content": "from"
}
},
//降低分值去匹配的,以下字段的分值乘以negative_boost值
"negative": {
"match": {
"content": {
"query": "Half"
}
}
},
"negative_boost": 0.1
}
}
}
结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.26228312,
"hits": [
{
"_index": "index3",
"_type": "type3",
"_id": "5",
"_score": 0.26228312,
"_source": {
"date": "2019-05-01",
"name": "http litty",
"content": "There are hello moments in life when you miss book someone so much that you just want to pick them from your dreams",
"no": "564",
"description": "描述"
}
},
{
"_index": "index3",
"_type": "type3",
"_id": "1",
"_score": 0.026301134,
"_source": {
"date": "2019-01-02",
"name": "the little",
"content": "Half the hello book ideas in his talk were plagiarized from an article I wrote last month.",
"no": "123"
}
}
]
}
}
es原理的更多相关文章
- ES原理(转载)
该博客属于转载,是很经典的一篇关于ES的介绍: Elasticsearch 是一个兼有搜索引擎和NoSQL数据库功能的开源系统,基于Java/Lucene构建,可以用于全文搜索,结构化搜索以及近实时分 ...
- 【漫画】ES原理 必知必会的倒排索引和分词
倒排索引的初衷 倒排索引,它也是索引.索引,初衷都是为了快速检索到你要的数据. 我相信你一定知道mysql的索引,如果对某一个字段加了索引,一般来说查询该字段速度是可以有显著的提升. 每种数据库都有自 ...
- 【Elasticsearch 技术分享】—— 十张图带大家看懂 ES 原理 !明白为什么说:ES 是准实时的!
前言 说到 Elasticsearch ,其中最明显的一个特点就是 near real-time 准实时 -- 当文档存储在Elasticsearch中时,将在1秒内以几乎实时的方式对其进行索引和完全 ...
- 全文检索原理以及es
最近要做个文章搜索,对全文检索原理以及es原理进行了一些调研, 1. es索引文件为多个文本文件描述,索引文件中的内容构成可见 http://elasticsearch.cn/article/86 ...
- es集群数据库~原理细节
ES原理一 基本定义 index(索引) 相当于mysql中的数据库 type(类型) 相当于mysql中的一张表 document(文档) 相当于mysql中的一行(一条记录) fie ...
- Elasticsearch(二)--集群原理及优化
一.ES原理 1.索引结构ES是面向文档的 各种文本内容以文档的形式存储到ES中,文档可以是一封邮件.一条日志,或者一个网页的内容.一般使用 JSON 作为文档的序列化格式,文档可以有很多字段,在创建 ...
- 《ElasticSearch6.x实战教程》之父-子关系文档
第七章-父-子关系文档 打虎亲兄弟,上阵父子兵. 本章作为复杂搜索的铺垫,介绍父子文档是为了更好的介绍复杂场景下的ES操作. 在非关系型数据库数据库中,我们常常会有表与表的关联查询.例如学生表和成绩表 ...
- python操作elasticsearch增、删、改、查
最近接触了个新东西--es数据库 这东西虽然被用的很多,但我是前些天刚刚接触的,发现其资料不多,学起来极其痛苦,写个文章记录下 导入库from elasticsearch import Elastic ...
- ELK集群之elasticsearch(3)
Elasticsearch-基础介绍及索引原理分析 介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引 ...
随机推荐
- UOJ#397. 【NOI2018】情报中心 线段树合并 虚树
原文链接www.cnblogs.com/zhouzhendong/p/UOJ397.com 前言 这真可做吗?只能贺题解啊-- 题解 我们称一条路径的 LCA 为这条路径两端点的 LCA. 我们将相交 ...
- ssh 连接云主机错误
ssh链接云主机: ssh root@123.59.xx.xx 报错:THE AUTHENTICITY OF HOST XX CAN’T BE ESTABLISHED 解决办法: ssh -o Str ...
- Python在windows平台的多版本配置
Python在windows平台的多版本配置 快速阅读: python在windows平台的环境变量以及多版本配置 ,以及pycharm如何安装包,以及安装包出错时如何排查. 1.python环境 ...
- Git的使用(2) —— 本地版本库的操作
1. 向本地版本库中添加文件 注意:.git文件夹是本地版本库,包含.git文件夹的目录叫工作目录,要往本地版本库中添加文件,就必须将文件放在工作目录中. (1) 把文件添加到工作目录中. (2) 右 ...
- Communications link failure due to underlying exception: ** BEGIN NESTED EXC
一是将 wait_timeout=31536000 interactive_timeout=31536000 将过期时间修改为1年. 二是在连接URL上添加参数:&autoReconnect= ...
- 拒绝让Eclipse帮倒忙,解决其复制粘贴时把反斜杠变成双反斜杠的问题
比如,你粘贴到字符串的文本是“C:\Users\horn1\Desktop”,结果变成了“C:\\Users\\horn1\\Desktop\\”,这还好,不会带来麻烦. 但是,比如你输入的是正则表达 ...
- Spring Boot Actuator:健康检查、审计、统计和监控(转)
Spring Boot Actuator可以帮助你监控和管理Spring Boot应用,比如健康检查.审计.统计和HTTP追踪等.所有的这些特性可以通过JMX或者HTTP endpoints来获得. ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍
笔记 1.SpringBoot2.x持久化数据方式介绍 简介:介绍近几年常用的访问数据库的方式和优缺点 1.原始java访问数据库 开发流程麻烦 ...
- git commit之后,撤销 commit
写完代码后,我们一般这样 git add . //添加所有文件 git commit -m "本功能全部完成" 执行完commit后,想撤回commit,怎么办? 可以执行如下命令 ...
- 16个python常用魔法函数
==,is的使用 ·is是比较两个引用是否指向了同一个对象(引用比较). ·==是比较两个对象是否相等 1.__ init__(): 所有类的超类object,有一个默认包含pass的__ init ...