ElasticSearch 5.5 DSL搜索

多个搜索条件 & 短语匹配

(同时包含"广州"和"东")

{
   "bool" : {
      "must":[],
      "should":[],
      "must_not":[],
   }
}

 

DSL:

{
  "query": {
    "bool": {
      "must": [
        [
          { "match_phrase": { "golf_course_name": "广州 " } },
          { "match": { "language_type": "1" } },
          { "range": { "id": { "gte": "11260870" } } }
        ]
      ]
    }
  }
}

 

复杂搜索外层 OR 内部 AND:

SELECT document FROM prices 
WHERE golf_course_name = "广州"
  OR ( golf_course_name = "东" AND price  = 30 )

 

{
  "query": {
    "bool": {
      "should": [
        {
          "match": { "golf_course_name": "广州" }
        },
        {
          "bool": {
            "must": [
              { "term": { "golf_course_name": "东" } },
              { "term": { "price": 30 } }
            ]
          }
        }
      ]
    }
  }
}

 

OR & 条件 & 范围搜索

DSL:

{
  "query": {
    "bool": {
      "should": [
        [
          { "range": { "bdate": { "gte": "1529337600" } } },
          { "match": { "language_type": "1" } }
        ],
        [
          { "match": { "language_type": "2" } }
        ]
      ]
    }
  }
}

 

过滤器(filter) 高效执行结构化搜索

 

exists排除空值

DSL:

{
  "query": {
    "bool": {
      "must": [
        [
          { "match": { "golf_course_name": "东" } }
        ]
      ],
      "filter": [
        {
          "range": { "id": { "gte": "11193704" } }
        },
        {
          "exists": { "field": "golf_course_name" }
        }
      ]
    }
  }
}

 

多重元素的时候需要把它转数组

[range] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

 

da685f7c1d589ac1fd64dc8ee223039e813.jpg
过滤顺序

在 bool 条件中过滤器的顺序对性能有很大的影响。

更详细的过滤条件应该被放置在其他过滤器之前,以便在更早的排除更多的文档。

假如条件 A 匹配 1000 万个文档,而 B 只匹配 100 个文档,那么需要将 B 放在 A 前面。

 match查询可以以一种低级别term查询的方式执行。

 

提高精度搜索:minimum_should_match

{
  "query": {
    "match": {
      "golf_course_name": {
        "query": "广 东",
        "operator": "and",
        "minimum_should_match": "75%"
      }
    }
  }
}

 

控制多少should子句需要被匹配:minimum_should_match

{
  "query": {
    "bool": {
      "should": [
        { "match": { "title": "brown" }},
        { "match": { "title": "fox"   }},
        { "match": { "title": "dog"   }}
      ],
      "minimum_should_match": 2 <1>
    }
  }
}
  

转载于:https://my.oschina.net/u/3359005/blog/1914931

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值