基于词项和基于全文的搜索
基于 Term 的查询(一般为了性能可以设置参数使其不打分)
关于 Term查询的例子
数据准备
DELETE products
PUT products
{
"settings": {
"number_of_shards": 1
}
}
POST /products/_bulk
{ "index": { "_id": 1 }}
{ "productID" : "XHDK-A-1293-#fJ3","desc":"iPhone" }
{ "index": { "_id": 2 }}
{ "productID" : "KDKE-B-9947-#kL5","desc":"iPad" }
{ "index": { "_id": 3 }}
{ "productID" : "JODL-X-1937-#pV7","desc":"MBP" }
GET /products
分别打开注释执行
POST /products/_search
{
"query": {
"term": {
"desc": {
//"value": "iPhone"
"value":"iphone"
}
}
}
}
我们输入数据的时候是大写,查询的时候使用大写查是查不出来的,stand 默认转小写
多字段 Mapping 和 Term
使用 keyword 进行精确匹配
POST /products/_search
{
"explain": true,
"query": {
"term": {
"productID.keyword": {
"value": "XHDK-A-1293-#fJ3"
}
}
}
}
可以看到默认有计算打分的过程
复合查询constact score 转换为 Filter
POST /products/_search
{
"explain": true,
"query": {
"constant_score": {
"filter": {
"term": {
"productID.keyword": "XHDK-A-1293-#fJ3"
}
}
}
}
}
基于全文的查询
最新匹配度
短语查询
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 } } }