全文检索-Fulltext query
目录
本章节所用数据:
PUT /product/_doc/1
{
"name" : "xiaomi phone",
"desc" : "shouji zhong de zhandouji",
"price" : 3999,
"lv":"qijianji",
"type":"phone",
"createtime":"2020-10-01T08:00:00Z",
"tags": [ "xingjiabi", "fashao","buka" ]
}
PUT /product/_doc/2
{
"name" : "xiaomi nfc phone",
"desc" : "zhichi quangongneng nfc,shouji zhong de jianjiji",
"price" : 4999,
"lv":"qijianji",
"type":"phone",
"createtime":"2020-05-21T08:00:00Z",
"tags": [ "xingjiabi", "fashao","gongjiaoka" ]
}
PUT /product/_doc/3
{
"name" : "nfc phone",
"desc" : "shouji zhong de hongzhaji ",
"price" : 2999,
"lv":"gaoduanji",
"type":"phone",
"createtime":"2020-06-20",
"tags": [ "xingjiabi", "fashao", "menjinka" ]
}
PUT /product/_doc/4
{
"name" : "xiaomi erji",
"desc" : "erji zhong de huangmenji",
"price" : 999,
"lv":"baiyuanji",
"type":"erji",
"createtime":"2020-06-23",
"tags": [ "low", "bufangshui","yinzhicha" ]
}
PUT /product/_doc/5
{
"name" : "hongmi erji",
"desc" : "erji zhong de kendeji nfc",
"price" : 399,
"type":"erji",
"lv":"baiyuanji",
"createtime":"2020-07-20",
"tags": [ "lowbee","xuhangduan", "zhiliangx" ]
}
PUT /product/_doc/6
{
"name" : "xiaomi phone 10",
"desc" : "chongdian zeikuai diaodian gengkuai,chaoji wudi wangyuanjing,gaoshua dianjingping",
"price" : "5999",
"lv":"qijianji",
"type":"phone",
"createtime":"2020-07-27",
"tags": [ "120Hz", "120W", "120bianjiao" ]
}
PUT /product/_doc/7
{
"name" : "aipao SE2",
"desc" : "chule CPU,nothing",
"price" : "3299",
"lv":"qijianji",
"type":"phone",
"createtime":"2020-07-21",
"tags": [ "gejiucai", "gejiujiucai", "gexinjiucai" ]
}
PUT /product/_doc/8
{
"name" : "XS Max",
"desc" : "tingshuo yaochu 12 le zhongyu keyi huandiao shouli de 4S le",
"price" : 4399,
"lv":"qijianji",
"type":"shouji",
"createtime":"2020-08-19",
"tags": [ "5V1A", "4Gquanwangtong", "big" ]
}
PUT /product/_doc/9
{
"name" : "xiaomi TV",
"desc" : "Sydney's KTV",
"price" : 2998,
"lv":"gaoduanji",
"type":"erji",
"createtime":"2020-08-16",
"tags": [ "jumo", "jiatingyingyuan", "games" ]
}
PUT /product/_doc/10
{
"name" : "hongmi TV",
"desc" : "wo bishangmian nage genghuasuan,woye 2998,woye 70 cun,danshi wo genghaokan",
"price" : 2999,
"type":"TV",
"lv":"gaoduanji",
"createtime":"2020-08-28",
"tags": [ "dapian", "languang8K", "chaobo" ]
}
一. 常用语法
1. match
GET /product/_search
{
"query": {
"match": {
"name": "xiaomi nfc phone"
}
}
}
解析:这个请求的意思是 查询product索引中name字段包含“xiaomi nfc phone”的文档
相当于mysql中的这个语句:
select * from product where name like '%xiaomi nfc phone%'
有mysql使用经验的同学都知道, 这条sql语句会导致全表扫描, 消耗巨大资源且效率极低
而ES是怎么做的呢? ES会将"xiaomi nfc phone" 分为三个此项: "xiaomi", "nfc", "phone", 再将三个词项在目标索引的文档中匹配, 将匹配到的文档全部返回.
查询结果
仅截图三条相关数据, 方便大家查看
从查询结果中我们可以看到:
- 文档1中name字段包含了xiaomi nfc phone三个词项
- 文档2中name字段包含了 nfc phone 两个词项
- 文档3中name字段包含了 xiaomi phone 两个词项
- 三个文档的评分值是降序排列
不难发现, 在mtach查询语句中, 只要任意一个词项被命中, 这条数据就可以被查询到.
2. match_all
顾名思义, 查询所有结果.
值得注意的是 当我们不加任何查询条件时, 与match_all是一致的, 如下代码
GET /product/_search
{
"query": {
"match_all": {}
}
}
GET /product/_search
3. multi_match
多字段查询, 可以在多个字段中匹配查询条件, 只要任意字段符合任意词项就将命中结果
GET /product/_search
{
"query": {
"multi_match": {
"query": "erji zhandouji",
"fields": ["name", "desc"]
}
}
}
语句解析: 查询name或desc字段中包含有erji zhandouji 任意一个词项的数据
也就是说, name或者desc两个字段只要包含了erji或者zhandouji中任意一个,那么这条数据将被查询出来.
查询结果
可以发现:
- 文档1中desc字段包含了 zhandouji
- 文档2中name字段包含了erji
- 文档3中name字段包含了erji
4. mtach_phrase
短语查询. 与mtach不同的是, match查询只需要包含查询条件中任意一个词项即可命中数据, 但是短语查询中, 文档必须包含查询条件中所有的词项, 并且词项的位置必须和条件中的位置一致才可以命中
GET /product/_search
{
"query": {
"match_phrase": {
"name": "xiaomi nfc"
}
}
}
我们来对比一下相同条件下两种查询方式的结果:
可以发现match命中了6条数据, match_phrase仅1条
match
match_phrase:
那么我们将xiaomi和nfc位置对换一下查看结果:
发现位置对换无法查询到数据