term query与match query区别

看官方提供的例子

[plain]  view plain  copy
  1. PUT my_index  
  2. {  
  3.   "mappings": {  
  4.     "my_type": {  
  5.       "properties": {  
  6.         "full_text": {  
  7.           "type":  "string"    
  8.         },  
  9.         "exact_value": {  
  10.           "type":  "string",  
  11.           "index": "not_analyzed"    
  12.         }  
  13.       }  
  14.     }  
  15.   }  
  16. }  

1.首先设置索引名称为my_index,类型为my_type的映射,设置该索引的full_text字段类型为String,exact_value字段类型为String, "index": "not_analyzed"表示该字段不分词

不清楚映射与分词的请看这里:https://github.com/looly/elasticsearch-definitive-guide-cn/blob/master/052_Mapping_Analysis/40_Analysis.md


2.添加一条数据

[plain]  view plain  copy
  1. PUT my_index/my_type/1  
  2. {  
  3.   "full_text":   "Quick Foxes!",    
  4.   "exact_value": "Quick Foxes!"     
  5. }  

索引名为my_index,类型为my_type,id为1

full_text字段的值为Quick Foxes!

exact_value字段的值为Quick Foxes!


注:

String字段可以分词,也可以不分词,分词的时候,默认的标准分析器可以将一句话中的单词划分开,然后转为小写,比如Quick Brown Fox!,标准分析器将会将它转换为quick , brown , fox,


由于full_text字段分词,因此在倒排索引中被默认标准分析器分为quick , foxes

exact_value字段不分词,在倒排索引中该字段的值仍为QuickFoxes!



3.执行查询

(1)使用term查询exact_value,搜索内容为Quick Foxes!

[plain]  view plain  copy
  1. GET my_index/my_type/_search  
  2. {  
  3.   "query": {  
  4.     "term": {  
  5.       "exact_value": "Quick Foxes!"    
  6.     }  
  7.   }  
  8. }  

由于exact_value不分词,Quick Foxes!与exact_value的值QuickFoxes!匹配,因此可以匹配


(2)使用term查询full_text,搜索内容为Quick Foxes!

[plain]  view plain  copy
  1. GET my_index/my_type/_search  
  2. {  
  3.   "query": {  
  4.     "term": {  
  5.       "full_text": "Quick Foxes!"    
  6.     }  
  7.   }  
  8. }  
由于full_text字段默认使用标准分析器分词,在倒排索引中被分为quick和foxes,因此使用 Quick Foxes!匹配不到内容

(3)使用term查询full_text,搜索内容为foxes

[plain]  view plain  copy
  1. GET my_index/my_type/_search  
  2. {  
  3.   "query": {  
  4.     "term": {  
  5.       "full_text": "foxes"    
  6.     }  
  7.   }  
  8. }  
由于full_text字段默认使用标准分析器分词,在倒排索引中被分为quick和foxes,因此使用 foxes可以匹配到


(4)使用match查询full_text,查询内容为Quick Foxes!

[plain]  view plain  copy
  1. GET my_index/my_type/_search  
  2. {  
  3.   "query": {  
  4.     "match": {  
  5.       "full_text": "Quick Foxes!"    
  6.     }  
  7.   }  
  8. }  

使用match搜索,先分析搜索字符串Quick Foxes!,对它分词,然后搜索full_text中含有quick或者foxes或者两者都包含的文档,由于full_text字在倒排索引中被分为quick和foxes,因此可以匹配到.


总结:

match query搜索的时候,首先会解析查询字符串,进行分词,然后查询,而term query,输入的查询内容是什么,就会按照什么去查询,并不会解析查询内容,对它分词。


发布了124 篇原创文章 · 获赞 0 · 访问量 6764

猜你喜欢

转载自blog.csdn.net/qq_44813090/article/details/104412676