结构化搜索 term
结构化搜索:不关心文件的相关度或评分;简单的对文档包括和排除处理,要么相等,要么不等。
精确查找
term查询,处理numbers、booleans、dates以及text。
单个精确值查询,不对查询评分计算,只对文档进行包括或排除的计算,使用constant_score查询以非评分模式统一评分。
GET /my_store/products/_search
{
"query" : {
"constant_score" : {
"filter" : {
"term" : {
"price" : 20
}
}
}
}
}
多个精确值查询
{
"terms" : {
"price" : [20, 30]
}
}
组合过滤器
布尔过滤器
{
"bool" : {
"must" : [],
"should" : [],
"must_not" : [],
}
}
must和SQL中的 AND 等价,must_not与 NOT 等价,should和 OR 等价。
将两个term 置入bool过滤器的should语句中。
GET /my_store/products/_search
{
"query" : {
"filtered" : { //需要用filtered包起来
"filter" : {
"bool" : {
"should" : [
{ "term" : {"price" : 20}},
{ "term" : {"productID" : "XHDK-A-1293-#fJ3"}}
],
"must_not" : {
"term" : {"price" : 30}
}
}
}
}
}
}
term和terms是 contains 操作,并非 equals。
返回结果:
"hits" : [
{
"_id" : "1",
"_score" : 1.0,
"_source" : {
"price" : 10,
"productID" : "XHDK-A-1293-#fJ3"
}
},
{
"_id" : "2",
"_score" : 1.0,
"_source" : {
"price" : 20,
"productID" : "KDKE-B-9947-#kL5"
}
}
]
范围
数字范围
"range" : {
"price" : {
"gte" : 20,
"lte" : 40
}
}
日期范围
"range" : {
"timestamp" : {
"gt" : "2014-01-01 00:00:00",
"lt" : "2014-01-07 00:00:00"
}
}
gt:>大于(greater than)lt:<小于(less than)gte:>=大于或等于(greater than or equal to)lte:<=小于或等于(less than or equal to)
处理Null值
存在查询,字段null和实际值同时存在也可以被检索出来。
GET /my_index/posts/_search
{
"query" : {
"constant_score" : {
"filter" : {
"exists" : { "field" : "tags" }
}
}
}
}
缺失查询,返回某个特定null值的文档。
GET /my_index/posts/_search
{
"query" : {
"constant_score" : {
"filter": {
"missing" : { "field" : "tags" }
}
}
}
}