ElasticSearch 搜索

  1. 精确值查找
    1. term查询数字
  • 可以用它处理数字(numbers)、布尔值(Booleans)、日期(dates)以及文本(text)。
  • 查询价格包含20的
"term" : {
"price" : 20
}
  1. 组合过滤器
    1. bool (布尔)过滤器是复合过滤器(compound filter)
    2. 布尔过滤器包含3部分
      1. { "bool" : { "must" : [], "should" : [], "must_not" : [], } }
    3. must 所有语句都必须匹配 等价于关系型数据库的AND
    4. must_not
      1. 所有的语句都 不能(must not) 匹配,与 NOT 等价。
    5. 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 )
  1. 查找多个精确值
    1. 使用多加个s的terms
      1. { "terms" : { "price" : [20, 30] //这里相当于 price=20 or price=30 } }
    2. 精确相等
      1. 使用tag_count 指定标签数量
      2. GET /my_index/my_type/_search
{
"query": {
"constant_score" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : { "tags" : "search" } },
{ "term" : { "tag_count" : 1 } }
]}
}
}
}
}
  1. 范围
    1. range
      1. "range" : { "price" : { "gte" : 20, "lte" : 40 } }
      2. 大于等于20 小于等于40,range 查询可同时提供包含(inclusive)和不包含(exclusive)这两种范围表达式,可供组合的选项如下:
  • 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" } }
  1. 处理Null值
    1. WHERE tags IS NOT NULL
    2. 翻译为es查询方式
    3. "filter" : { "exists" : { "field" : "tags" } }
    4. WHERE tags IS NULL
    5. "filter": { "missing" : { "field" : "tags" } }
  2. 匹配查询
    1. match 这表示它既能处理全文字段,又能处理精确字段
      1. { "query": { "match": { "title": "QUICK!" } } }
  3. 多词查询
{
"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
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注