- 精确值查找
- term查询数字
- 可以用它处理数字(numbers)、布尔值(Booleans)、日期(dates)以及文本(text)。
- 查询价格包含20的
"term" : {
"price" : 20
}
- 组合过滤器
- bool (布尔)过滤器是复合过滤器(compound filter)
- 布尔过滤器包含3部分
- { "bool" : { "must" : [], "should" : [], "must_not" : [], } }
- must 所有语句都必须匹配 等价于关系型数据库的AND
- must_not
- 所有的语句都 不能(must not) 匹配,与 NOT 等价。
- should 至少有一个语句要匹配,与 OR 等价。
{
"query" : {
"filtered" : {
"filter" : {
"bool" : {
"should" : [
{ "term" : {"price" : 20}},
{ "term" : {"productID" : "XHDK-A-1293-#fJ3"}}
],
"must_not" : {
"term" : {"price" : 30}
}
}
}
}
}
}
这里相当于 (price=20 or productID=XHDK-A-1293-#fJ3) and price != 30
嵌套布尔过滤器
{
"query" : {
"filtered" : {
"filter" : {
"bool" : {
"should" : [
{ "term" : {"productID" : "KDKE-B-9947-#kL5"}},
{ "bool" : {
"must" : [
{ "term" : {"productID" : "JODL-X-1937-#pV7"}},
{ "term" : {"price" : 30}}
]
}
}
]
}
}
}
这里相当于 productID=KDKE-B-9947-#kL5 or (productID=JODL-X-1937-#pV7 and price=30 )
- 查找多个精确值
- 使用多加个s的terms
- { "terms" : { "price" : [20, 30] //这里相当于 price=20 or price=30 } }
- 精确相等
- 使用tag_count 指定标签数量
- GET /my_index/my_type/_search
- 使用多加个s的terms
{
"query": {
"constant_score" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : { "tags" : "search" } },
{ "term" : { "tag_count" : 1 } }
]}
}
}
}
}
- 范围
- range
- "range" : { "price" : { "gte" : 20, "lte" : 40 } }
- 大于等于20 小于等于40,range 查询可同时提供包含(inclusive)和不包含(exclusive)这两种范围表达式,可供组合的选项如下:
- range
- gt: > 大于(greater than)
- lt: < 小于(less than)
- gte: >= 大于或等于(greater than or equal to)
- lte: <= 小于或等于(less than or equal to)
- 日期范围
- "range" : { "timestamp" : { "gt" : "2014-01-01 00:00:00", "lt" : "2014-01-07 00:00:00" } }
- 处理Null值
- WHERE tags IS NOT NULL
- 翻译为es查询方式
- "filter" : { "exists" : { "field" : "tags" } }
- WHERE tags IS NULL
- "filter": { "missing" : { "field" : "tags" } }
- 匹配查询
- match 这表示它既能处理全文字段,又能处理精确字段
- { "query": { "match": { "title": "QUICK!" } } }
- match 这表示它既能处理全文字段,又能处理精确字段
- 多词查询
{
"query": {
"match": {
"title": "BROWN DOG!"
}
}
}
提高精度 :通过match的 operator 操作符作为输入参数
控制精度:match 查询支持 minimum_should_match 最小匹配参数,这让我们可以指定必须匹配的词项数用来表示一个文档是否相关。我们可以将其设置为某个具体数字,更常用的做法是将其设置为一个百分数,因为我们无法控制用户搜索时输入的单词数量:
{
"query": {
"match": {
"title": {
"query": "quick brown dog",
"minimum_should_match": "75%"
}
}
}
}
}
8. 组合查询
{
"query": {
"bool": {
"must": { "match": { "title": "quick" }},
"must_not": { "match": { "title": "lazy" }},
"should": [
{ "match": { "title": "brown" }},
{ "match": { "title": "dog" }}
]}
}
}
9 语句提升权重、
使用boost
"content": {
"query": "Elasticsearch",
"boost": 3
}