ES检索实例

ES查询操作

示例

GET /test/_doc/_search?q=label:森林2

match

会使用分词器解析,所以是部分匹配

  • 搜索所有带字的精确匹配
GET /test/_doc/_search
{
  "query": {
    "match": {
      "label": "森"
    }
  }
}
  • 过滤结果字段

在示例1的结果中会默认查询出文档类的所有字段,
可以通过添加_source来过滤结果集. 下面的示例中指定了结果仅显示label字段

GET /test/_doc/_search
{
  "query": {
    "match": {
      "label": "林"
    }
  },
  "_source": ["label"]
}
  • 结果排序

按结果排序会导致_score字段为空。
这里由于一开始插值时没指定age字段类型,默认为text,且后期无法修改。text类型的字段无法作为排序条件Fielddata is disabled on text fields by default.

GET /test/_doc/_search
{
  "query": {
    "match": {
      "label": "林"
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}
  • 分页

查询中指定from, size 默认从0开始

GET /test/_doc/_search
{
  "query": {
    "match": {
      "label": "林"
    }
  },
  "from": 0,
  "size": 1
}
  • 匹配分数过滤

仅返回匹配分数>0.3的

GET /test/_doc/_search
{
  "query": {
    "match": {
      "label": "林"
    }
  },
  "min_score":0.3
}
  • 布尔查询

多条件精确查询

must同时满足

GET /test/_doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "label": "林"
          }
        },
        {
          "match": {
            "age": 3
          }
        }
      ]
    }
  }
}

should 相当于or

GET /test/_doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "label": "林"
          }
        },
        {
          "match": {
            "age": 1
          }
        }
      ]
    }
  }
}

must_not 相当于not

GET /test/_doc/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "label": "林"
          }
        }
      ]
    }
  }
}

一个混合示例, 查询label中有age不等于1

GET /test/_doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "label": "林"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "age": "1"
          }
        }
      ]
    }
  }
}

另一个复合查询示例,查询包含,且时间范围在2017-01-01 ~ 2019-12-31的数据

GET test/_doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "label": "林"
          }
        },
        {
          "range" : {
            "postdate" : {
                "gte" : "2017-01-01",
                "lte" :  "2019-12-31",
                "format": "yyyy-MM-dd"
            }
          }
        }
      ]
    }
  }
}
  • 区间查询
    lt: 小于
    lte: 小于等于
    gt: 大于
    gte: 大于等于
GET test/_doc/_search
{
    "query": {
        "range" : {
            "postdate" : {
                "gte" : "2017-01-01",
                "lte" :  "2019-12-31",
                "format": "yyyy-MM-dd"
            }
        }
    }
}
  • 过滤器
GET /test/_doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "label": "林"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 2,
            "lte": 5
          }
        }
      }
    }
  }
}
  • 包含某个字段的匹配

查询有tags字段的文档,或者说tags字段不为空的文档

GET test/_doc/_search
{
  "query": {
    "exists": {
      "field": "tags"
    }
  }
}
  • 多关键字匹配

直接用空格隔开即可,注意这里是or关系

GET /test/_doc/_search
{
  "query": {
    "match": {
      "tags": "区 田"
    }
  }
}

term

与match的区别,
对于输入的查询query,match会相对query进行分词,然后与匹配字段的分词结果进行匹配,也就是查询林田,会被分词为林,和田,然后看匹配字段的分词结果是否包含林或田。
但term中,输入林田,不会对林田进行分词,直接与匹配字段的分词结果进行匹配,注意,这里是和匹配字段的分词结果进行匹配,通过ik分词测试发现,林田的分词结果是林和田,而不包含林田,因此查不到结果。

https://www.cnblogs.com/yjf512/p/4897294.html

测试林田的ik分词结果

GET _analyze
{
  "analyzer": "standard",
  "text": "林田"
}

term查询

GET /test/_doc/_search
{
  "query": {
    "term": {
      "label": "林田"
    }
  }
}

高亮查询

添加highlight字段,会自动标注匹配成功的内容。
默认高亮内容被<em>标签包裹。
可以添加pre_tagspost_tags自定义高亮css。

GET /test/_doc/_search
{
  "query": {
    "match": {
      "label": "林"
    }
  },
  "highlight": {
    "pre_tags": "<div class='key' style='color: red;'>",
    "post_tags": "</div>",
    "fields": {
      "label": {}
    }
  }
}

第二种方式 就是标签的位置换了下,
也可以指定多字段高亮(注意添加了"require_field_match":false),如下面对tags也进行了高亮.

GET /test/_doc/_search
{
  "query": {
    "match": {
      "label": "林"
    }
  },
  "highlight": {
    "require_field_match":false,
    "fields": {
      "label": {
        "pre_tags": "<div class='key' style='color: red;'>",
        "post_tags": "</div>"
      },
      "tags": {}
    }
  }
}

显然直接在highlight里面指定的pre_tagspost_tags是高亮默认的,对多字段高亮的情况下,可以单独在指定特定的高亮方式.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值