目录
参考文章:Elasticsearch中 match、match_phrase、query_string和term的区别
1.概念
1.1.term与match
term是精确查询
match是模糊查询
1.2.text与keyword
ES更新到5版本后,取消了 string 数据类型,代替它的是 keyword 和 text 数据类型。
Text
Text 数据类型被用来索引长文本,比如说电子邮件的主体部分或者一款产品的介绍。这些文本会被分析,在建立索引前会将这些文本进行分词,转化为词的组合,建立索引。允许 ES来检索这些词语。text 数据类型不能用来排序和聚合。
Keyword
Keyword 数据类型用来建立电子邮箱地址、姓名、邮政编码和标签等数据,不需要进行分词。可以被用来检索过滤、排序和聚合。keyword 类型字段只能用本身来进行检索。
注意:如果不像以上通过mapping 配置索引时,遇到字符串类型时候的字端,系统会默认为“text”类型。检索的时候对字符串进行分析。所以要想只通过字段本身来进行检索,还是需要按照上面把该字段改为“keyword”类型。
以下参考:Elasticsearch中 match、match_phrase、query_string和term的区别
2.Term + Text/Keyword
(1)term查询text字段
因为text字段会分词,而term不分词,所以term查询的条件必须是text字段分词后的某一个。
(2)term查询keyword字段
term不会分词。而keyword字段也不分词。需要完全匹配才可。
3.Match + Text/Keyword
(1)match查询text字段
match分词,text也分词,只要match的分词结果和text的分词结果有相同的就匹配。
Match查询会先对搜索词进行分词,分词完毕后再逐个对分词结果进行匹配,因此相比于term的精确搜索,match是分词匹配搜索,match搜索还有两个相似功能的变种,一个是match_phrase,一个是multi_match。
(2)match查询keyword字段
match会被分词,而keyword不会被分词,match的需要跟keyword的完全匹配可以。
4.Match - Match_phrase
主要知识点:
- match_phrase的使用场景
- match_phrase的用法
- match_phrase的原理
一、什么是近似匹配
match_phrase的使用场景
现假设有两个句子
1、java is my favourite programming language, and I also think spark is a very good big data system.
2、java spark are very related, because scala is spark's programming language and scala is also based on jvm like java.
进行match query,query语法如下:
{
"query":{
"match": {
"content": "java spark"
}
}
}
match query进行搜索,只能搜索到包含java或spark的document,包含java和spark的doc都会被返回回来。现在假如说我们要实现以下三个需求:
1、java spark,就靠在一起,中间不能插入任何其他字符,就要搜索出来这种doc
2、java spark,但是要求,java和spark两个单词靠的越近,doc的分数越高,排名越靠前
3、我们搜索时,文档中必须包含java spark这两个文档,且他们之间的距离不能超过5,
要实现上述三个需求,用match做全文检索,是搞不定的,必须得用proximity match(近似匹配),proximity match分两种,短语匹配(phrase match)和近似匹配(proximity match)。这一讲,要学习的是phrase match,就是仅仅搜索出java和spark靠在一起的那些doc,比如有个doc,是java use'd spark,这就不是结果。
二、match_phrase的用法
phrase match,就是要去将多个term作为一个短语,一起去搜索,只有包含这个短语的doc才会作为结果返回。match是只在包含其中任何一个分词就返回。
1、match语法:
GET /forum/article/_search
{
"query": {
"match": {
"content": "java spark"
}
}
}
单单包含java的doc也返回了,不是我们想要的结果
2、改一个数据,将一个doc的content设置为恰巧包含java spark这个短语,以方便搜索
POST /forum/article/5/_update
{
"doc": {
"content": "spark is best big data solution based on scala ,an programming language similar to java spark"
}
}
3、match_phrase语法
GET /forum/article/_search
{
"query": {
"match_phrase": {
"content": "java spark"
}
}
}
结果只返回了最后我们修改的那个doc,只包含java或spark的doc不会返回
三、match_phrase的原理
原理不想了解的童鞋可以忽略。
1、理解term position
es分词器在分词做倒排索引时,会记录下每个分词在对应的doc中的位置(position)
比如有下面两个doc>
doc1:hello world, java spark
doc2:hi, spark java
做倒排索引时:
hello doc1(0)
wolrd doc1(1)
java doc1(2) doc2(2)
spark doc1(3) doc2(1)
()中表示位置。可以通过以下语句进行查看。
GET _analyze
{
"text": "hello world, java spark",
"analyzer": "standard"
}
2、match_phrase的基本原理
match_phrase执行过程:
1.如match搜索一样进行分词,
2.对分词后的单词到field中去进行搜索。这一步返回每个单词对应的doc,并返回这些单词在对应的doc中的位置,
3.对返回的doc进行第一步的筛选,找到每个单词都在同一个field的doc。
4.对第3步进行筛选后的doc进行再一次的筛选,选回位置符合要求的doc。比如,对于match_phrase,就是找到后一个单词的位置比前一个单词的位置大1。
5.proximity match原理一样,只是第四位对位置进行筛选时的方法不同。
math_phrase 集合slop
GET /my_index/my_type/_search
{
"query": {
"match_phrase": {
"title": {
"query": "quick fox",
"slop": 1
}
}
}
}
slop参数告诉match_phrase查询词条能够相隔多远时仍然将文档视为匹配。相隔多远的意思是,你需要移动一个词条多少次来让查询和文档匹配。
5.Match - Multi_match
参考:https://www.elastic.co/guide/cn/elasticsearch/guide/current/multi-match-query.html
6.Query_String
和match_phrase区别的是,query_string查询text类型字段,不需要连续,顺序还可以调换。
7、实际案例参考
GET /sw_ws_gd/_search
{
"from": 0,
"size": 500,
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"multi_match": {
"query": "ä¸ä»‹",
"fields": [
"bgbc_content^2.0",
"bycm_content^2.0",
"case_code^2.0",
"case_title^2.0",
"dsrxx_content^2.0",
"flyj^2.0",
"jbxx_content^2.0",
"sljg_content^2.0",
"wsnr^2.0",
"ygsc_content^2.0"
],
"type": "phrase",
"operator": "OR",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"minimum_should_match": "40%",
"tie_breaker": 0.3,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 1
}
},
{
"multi_match": {
"query": "å±…é—´",
"fields": [
"bgbc_content^1.0",
"bycm_content^1.0",
"case_code^1.0",
"case_title^1.0",
"dsrxx_content^1.0",
"flyj^1.0",
"jbxx_content^1.0",
"sljg_content^1.0",
"wsnr^1.0",
"ygsc_content^1.0"
],
"type": "phrase",
"operator": "OR",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"minimum_should_match": "40%",
"tie_breaker": 0.3,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
{
"bool": {
"adjust_pure_negative": true,
"boost": 1
}
}
],
"filter": [
{
"bool": {
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"_source": {
"includes": [
"case_code",
"case_title"
],
"excludes": []
},
"track_total_hits": 2147483647
}