ElasticSearch学习6

复杂查询

test3索引中的内容

①查询匹配
  • match:匹配(会使用分词器解析(先分析文档,然后进行查询))
  • _source:过滤字段(就是查询哪些字段)
  • sort:排序
  • formsize 分页(form表示从第几页开始,size表示每页展示多少条数据)
// 查询匹配 
GET / blog / user / _search {
	"query": {
		"match": {
			"name": "流"
		}
	},
	"_source": ["name", "desc"],
	"sort": [{
		"age": {
			"order": "asc"
		}
	}],
	"from": 0,
	"size": 1
}

②多条件查询(bool)

  • must 相当于 and
  • should 相当于 or
  • must_not 相当于 not (... and ...)
  • filter 过滤
/// bool 多条件查询
 must <==> and
 should <==> or
 must_not <==> not (... and ...)
 filter数据过滤
 boost
 minimum_should_match
GET / blog / user / _search {
	"query": {
		"bool": {
			"must": [{
				"match": {
					"age": 3
				}
			}, {
				"match": {
					"name": "流"
				}
			}],
			"filter": {
				"range": {
					"age": {
						"gte": 1,
						"lte": 3
					}
				}
			}
		}
	}
}
③匹配数组(满足数组中的一个值就会被查询出来)
  • 貌似不能与其它字段一起使用
  • 可以多关键字查(空格隔开)— 匹配字段也是符合的
  • match 会使用分词器解析(先分析文档,然后进行查询)
  • 搜词
// 匹配数组 貌似不能与其它字段一起使用
// 可以多关键字查(空格隔开)
// match 会使用分词器解析(先分析文档,然后进行查询)
GET / blog / user / _search {
	"query": {
		"match": {
			"desc": "年龄 牛 大"
		}
	}
}
④精确查询
  • term 直接通过 倒排索引 指定词条查询
  • 适合查询 number、date、keyword ,不适合text
// 精确查询(必须全部都有,而且不可分,即按一个完整的词查询)
// term 直接通过 倒排索引 指定的词条 进行精确查找的
GET / blog / user / _search {
	"query": {
		"term": {
			"desc": "年 "
		}
	}
}
⑤text和keyword
  • text:
    • 支持分词全文检索、支持模糊、精确查询,不支持聚合,排序操作;
    • text类型的最大支持的字符长度无限制,适合大字段存储;
  • keyword:
    • 不进行分词直接索引、支持模糊、支持精确匹配,支持聚合、排序操作。
    • keyword类型的最大支持的长度为——32766个UTF-8类型的字符,可以通过设置ignore_above指定自持字符长度,超过给定长度后的数据将不被索引,无法通过term精确匹配检索返回结果
// 测试keyword和text是否支持分词
// 设置索引类型
PUT / test {
	"mappings": {
		"properties": {
			"text": {
				"type": "text"
			},
			"keyword": {
				"type": "keyword"
			}
		}
	}
}
// 设置字段数据
PUT / test / _doc / 1 {
	"text": "测试keyword和text是否支持分词",
	"keyword": "测试keyword和text是否支持分词"
}
// text 支持分词
// keyword 不支持分词
GET / test / _doc / _search {
	"query": {
		"match": {
			"text": "测试"
		}
	}
} 
// 查的到
GET / test / _doc / _search {
	"query": {
		"match": {
			"keyword": "测试"
		}
	}
} 
// 查不到,必须是 "测试keyword和text是否支持分词" 才能查到
GET _analyze {
	"analyzer": "keyword",
	"text": ["测试liu"]
}
// 不会分词,即 测试liu
GET _analyze {
	"analyzer": "standard",
	"text": ["测试liu"]
}
// 分为 测 试 liu
GET _analyze {
	"analyzer": "ik_max_word",
	"text": ["测试liu"]
} // 分为 测试 liu
⑥高亮查询
/// 高亮查询
GET blog / user / _search {
	"query": {
		"match": {
			"name": "流"
		}
	},
	"highlight": {
		"fields": {
			"name": {}
		}
	}
}
// 自定义前缀和后缀
GET blog / user / _search {
	"query": {
		"match": {
			"name": "流"
		}
	},
	"highlight": {
		"pre_tags": "<p class='key' style='color:red'>",
		"post_tags": "</p>",
		"fields": {
			"name": {}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值