elasticsearch中如何手动控制全文检索结果的精准度
1、为帖子数据增加标题字段
POST /forum/article/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"title" : "this is java and elasticsearch blog"} }
{ "update": { "_id": "2"} }
{ "doc" : {"title" : "this is java blog"} }
{ "update": { "_id": "3"} }
{ "doc" : {"title" : "this is elasticsearch blog"} }
{ "update": { "_id": "4"} }
{ "doc" : {"title" : "this is java, elasticsearch, hadoop blog"} }
{ "update": { "_id": "5"} }
{ "doc" : {"title" : "this is spark blog"} }
2、搜索标题中包含java或elasticsearch的blog
这个,就跟之前的那个term query,不一样了。不是搜索exact value,是进行full text全文检索。
match query,是负责进行全文检索的。当然,如果要检索的field,是not_analyzed类型的,那么match query也相当于term query。
GET /forum/article/_search
{
"query": {
"match": {
"title": "java elasticsearch"
}
}
}
3、搜索标题中包含java和elasticsearch的blog
搜索结果精准控制的第一步:灵活使用and关键字,如果你是希望所有的搜索关键字都要匹配的,那么就用and,可以实现单纯match query无法实现的效果
GET /forum/article/_search
{
"query":{
"match":{
"title":{
"query":"java elasticsearch",
"operator":"and"
}
}
}
}
4、搜索包含java,elasticsearch,spark,hadoop,4个关键字中,至少3个的blog
控制搜索结果的精准度的第二步:指定一些关键字中,必须至少匹配其中的多少个关键字,才能作为结果返回
GET /forum/article/_search
{
"query":{
"match":{
"title":{
"query":"java elasticsearch spark hadoop",
"minimum_should_match":"75%"
}
}
}
}
5、用bool组合多个搜索条件,来搜索title
GET /forum/article/_search
{
"query":{
"bool":{
"must":{
"match":{
"title":"java"
}
},
"must_not":{
"match":{
"title":"spark"
}
},
"should":[
{
"match":{
"title":"hadoop"
}
},
{
"match":{
"title":"elasticsearch"
}
}
]
}
}
}
6、bool组合多个搜索条件,如何计算relevance score
must和should搜索对应的分数,加起来,除以must和should的总数
排名第一:java,同时包含should中所有的关键字,hadoop,elasticsearch
排名第二:java,同时包含should中的elasticsearch
排名第三:java,不包含should中的任何关键字
should是可以影响相关度分数的
must是确保说,谁必须有这个关键字,同时会根据这个must的条件去计算出document对这个搜索条件的relevance score
在满足must的基础之上,should中的条件,不匹配也可以,但是如果匹配的更多,那么document的relevance score就会更高
{
"took":6,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"failed":0
},
"hits":{
"total":3,
"max_score":1.3375794,
"hits":[
{
"_index":"forum",
"_type":"article",
"_id":"4",
"_score":1.3375794,
"_source":{
"articleID":"QQPX-R-3956-#aD8",
"userID":2,
"hidden":true,
"postDate":"2017-01-02",
"tag":[
"java",
"elasticsearch"
],
"tag_cnt":2,
"view_cnt":80,
"title":"this is java, elasticsearch, hadoop blog"
}
},
{
"_index":"forum",
"_type":"article",
"_id":"1",
"_score":0.53484553,
"_source":{
"articleID":"XHDK-A-1293-#fJ3",
"userID":1,
"hidden":false,
"postDate":"2017-01-01",
"tag":[
"java",
"hadoop"
],
"tag_cnt":2,
"view_cnt":30,
"title":"this is java and elasticsearch blog"
}
},
{
"_index":"forum",
"_type":"article",
"_id":"2",
"_score":0.19856805,
"_source":{
"articleID":"KDKE-B-9947-#kL5",
"userID":1,
"hidden":false,
"postDate":"2017-01-02",
"tag":[
"java"
],
"tag_cnt":1,
"view_cnt":50,
"title":"this is java blog"
}
}
]
}
}
7、搜索java,hadoop,spark,elasticsearch,至少包含其中3个关键字
默认情况下,should是可以不匹配任何一个的,比如上面的搜索中,this is java blog,就不匹配任何一个should条件
但是有个例外的情况,如果没有must的话,那么should中必须至少匹配一个才可以
比如下面的搜索,should中有4个条件,默认情况下,只要满足其中一个条件,就可以匹配作为结果返回
但是可以精准控制,should的4个条件中,至少匹配几个才能作为结果返回
GET /forum/article/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"title":"java"
}
},
{
"match":{
"title":"elasticsearch"
}
},
{
"match":{
"title":"hadoop"
}
},
{
"match":{
"title":"spark"
}
}
],
"minimum_should_match":3
}
}
}
elasticsearch中如何手动控制全文检索结果的精准度的更多相关文章
- Elasticsearch学习之深入搜索一 --- 提高查询的精准度
1. 为帖子增加标题字段 POST /forum/article/_bulk { "} } { "doc" : {"title" : "th ...
- 在Elasticsearch中查询Term Vectors词条向量信息
这篇文章有点深度,可能需要一些Lucene或者全文检索的背景.由于我也很久没有看过Lucene了,有些地方理解的不对还请多多指正. 更多内容还请参考整理的ELK教程 关于Term Vectors 额, ...
- laravel Scout包在elasticsearch中的应用
laravel Scout包在elasticsearch中的应用 laravel的Scout包是针对自身的Eloquent模型开发的基于驱动的全文检索引擎.意思就是我们可以像使用ORM一样使用检索功能 ...
- Elasticsearch 6.x版本全文检索学习之分布式特性介绍
1.Elasticsearch 6.x版本全文检索学习之分布式特性介绍. 1).Elasticsearch支持集群默认,是一个分布式系统,其好处主要有两个. a.增大系统容量,如内存.磁盘.使得es集 ...
- Elasticsearch 6.x版本全文检索学习之Search API
Elasticsearch 6.x版本全文检索学习之Search API. 1).Search API,实现对es中存储的数据进行查询分析,endpoind为_search,如下所示. 方式一.GET ...
- elasticsearch中TermQuery查不到数据问题
在java rest client中调用elasticsearch中的数据,精准匹配的termQuery查不到数据,这个问题是java rest client客户端自带的bug,换用matchPhra ...
- Elasticsearch 中为什么选择倒排索引而不选择 B 树索引
目录 前言 为什么全文索引不使用 B+ 树进行存储 全文检索 正排索引 倒排索引 倒排索引如何存储数据 FOR 压缩 RBM 压缩 倒排索引如何存储 字典树(Tria Tree) FST FSM 构建 ...
- 如何在Elasticsearch中安装中文分词器(IK+pinyin)
如果直接使用Elasticsearch的朋友在处理中文内容的搜索时,肯定会遇到很尴尬的问题--中文词语被分成了一个一个的汉字,当用Kibana作图的时候,按照term来分组,结果一个汉字被分成了一组. ...
- elasticsearch中常用的API
elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...
随机推荐
- 43、android:screenOrientation
android:screenOrientationThe orientation of the activity's display on the device. The value can be a ...
- LeetCode Problem 35:Search Insert Position
描述:Given a sorted array and a target value, return the index if the target is found. If not, return ...
- N - Broken Keyboard (a.k.a. Beiju Text)(DFS,链表)
N - Broken Keyboard (a.k.a. Beiju Text) Time Limit:1000MS Memory Limit:0KB 64bit IO Format:% ...
- Sping Cloud 微服务框架学习
Spring Cloud官方中文站 https://springcloud.cc
- 创建String字符串的方式与区别
Java中创建一个字符串的方式有很多种,常见如: String s = new String("riqi"); String s = "riqi"; 但两者有什 ...
- <2013 08 09> __attribute__是GCC编译器特有语法
1. __attribute__ ((packed)) 的作用就是告诉编译器取消结构在编译过程中的优化对齐,按照实际占用字节数进行对齐,是GCC特有的语法.这个功能是跟操作系统没关系,跟编译器有关,g ...
- mysql 5.7.18版本 sql_mode 问题
only_full_group_by 模式的,但开启这个模式后,原先的 group by 语句就报错,然后又把它移除了. only_full_group_by ,感觉,group by 将变成和 di ...
- 【转】PowerVM 的主要组成部分及概念
PowerVM 是在基于 IBM POWER 处理器的硬件平台上提供的具有行业领先水平的虚拟化技术家族.它是 IBM Power System 虚拟化技术全新和统一的品牌(逻辑分区,微分区,Hyper ...
- SQLServer中行列转换Pivot UnPivot
PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P ...
- 我的Android进阶之旅------>Android关于dp(dip)、sp转px的工具类
下面是一个工具类,提供了dp.sp.px之间相互转化的方法. import android.content.Context; /** * dp.sp 转换为 px 的工具类<br> * & ...