基于Term和基于全文的搜索
基于Term的查询
示例
POST /products/_search
{
"query": {
"term": {
"desc": {
"value": "iPhone" //不能查询到结果,因为term查询的时候不会做分词处理,但index数据的时候做了分词
"value":"iphone" //能查询到结果,
}
}
}
}
要做完全匹配用keyword
复合查询 - ConstantScore 转为 Filter
基于全文的查询
特点
示例
Match Query查询过程
结构化搜索
结构化数据
ES中的结构化搜索
#结构化搜索,精确匹配
DELETE products
POST /products/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10,"avaliable":true,"date":"2018-01-01", "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20,"avaliable":true,"date":"2019-01-01", "productID" : "KDKE-B-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30,"avaliable":true, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30,"avaliable":false, "productID" : "QQPX-R-3956-#aD8" }
GET products/_mapping
#对布尔值 match 查询,有算分
POST products/_search
{
"profile": "true",
"explain": true,
"query": {
"term": {
"avaliable": true
}
}
}
#对布尔值,通过constant score 转成 filtering,没有算分
POST products/_search
{
"profile": "true",
"explain": true,
"query": {
"constant_score": {
"filter": {
"term": {
"avaliable": true
}
}
}
}
}
#数字类型 Term
POST products/_search
{
"profile": "true",
"explain": true,
"query": {
"term": {
"price": 30
}
}
}
#数字类型 terms
POST products/_search
{
"query": {
"constant_score": {
"filter": {
"terms": {
"price": [
"20",
"30"
]
}
}
}
}
}
#数字 Range 查询
GET products/_search
{
"query" : {
"constant_score" : {
"filter" : {
"range" : {
"price" : {
"gte" : 20,
"lte" : 30
}
}
}
}
}
}
# 日期 range
POST products/_search
{
"query" : {
"constant_score" : {
"filter" : {
"range" : {
"date" : {
"gte" : "now-1y"
}
}
}
}
}
}
搜索的相关性算分
相关性和相关性算分
词频 TF
逆文档频率 IDF
TF-IDF的概念
BM 25
Query&Filtering与多字符串多字段查询
Query Context & Filter Context
条件组合
bool查询
bool查询语法
Filter Context - 不影响算分
Query Context - 影响算分
控制字段的Boosting
单字符串多字段查询:Dis-Max-Query
单字符串多字段查询:Multi-Match