当前位置:   article > 正文

ElasticSearch 复合搜索与过滤API_es api过滤索引

es api过滤索引

本文基于Elasticsearch7.x

全文搜索在搜索时, 会对输入的搜索文本进行分词, 然后去倒排索引中进行匹配, 只要能匹配上任意一个关键词(词项), 就可以作为结果返回.

在学习本篇博客前先了解下Elasticsearch全文搜索之基础语法API

在这里插入图片描述

Rest API

添加搜索实例数据

POST /blogs/_bulk
{"index": {}}
{"post_date": "2020-01-01", "title": "Quick brown rabbits", "content": "Brown rabbits are commonly seen.", "author_id": 11401}
{"index": {}}
{"post_date": "2020-01-02", "title": "Keeping pets healthy", "content": "My quick brown fox eats rabbits on a regular basis.", "author_id": 11402}
{"index": {}}
{"post_date": "2020-01-03", "title": "My dog barks", "content": "I see a lot of barking dogs on the road.", "author_id": 11403}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

bool

基础匹配API的实例都是对一个搜索文本进行匹配, 即单条件搜索. 下面我们来学习下bool多条件搜索, 即由多个搜索文本构成的复合搜索.

(1) bool语法

  • must
    必须匹配, 贡献算分.
  • must_not
    必须不能匹配, 贡献算分.
  • should
    选择性匹配, 贡献算分.
  • filter
    必须匹配, 不贡献算分.

must, must_not, should这三个条件是会用于相关度分数计算的, 而filter不会, 从而filter的性能会更好. 由以上四种搜索子句合并为一条复合搜索语句, 这就是bool搜索.

基础匹配API中讲述的match, match_phrase, dis_max, multi_match, term是基础的搜索语法, bool搜索是基于它们来实现的.

(2) 实例

a. 基础使用

GET /blogs/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "author_id": {
              "value": "11403"
            }
          }
        }
      ],
      "must_not": [
        {
          "range": {
            "post_date": {
              "lte": "2020-01-02"
            }
          }
        }
      ],
      "should": [
        {
          "term": {
            "title.keyword": {
              "value": "My dog barks"
            }
          }
        },
        {
          "term": {
            "content
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号