ES的常用查询

1 数据准备

1.1 创建索引和新增数据

先新增一条数据,新增数据时会自动创建索引 test_standard_analyzer。

bash 复制代码
PUT /test_standard_analyzer/_doc/1
{
  "remark": "This is a test doc"
}

然后查询一下。

bash 复制代码
GET test_standard_analyzer/_search
{
  "query": {
    "match_all": {}
  }
}

查询结果如下所示。

bash 复制代码
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_standard_analyzer",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "remark" : "This is a test doc"
        }
      }
    ]
  }
}

1.2 测试分词

没指定es分词器时,es会使用默认分词器-standard。测试下分词效果。

bash 复制代码
 POST test_standard_analyzer/_analyze
{
  "field": "remark",
  "text": "This is a test doc"
}

# 或

 POST test_standard_analyzer/_analyze
{
  "analyzer": "standard",
  "text": "This is a test doc"
}

分词结果如下所示。

bash 复制代码
{
  "tokens" : [
    {
      "token" : "this",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "is",
      "start_offset" : 5,
      "end_offset" : 7,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "a",
      "start_offset" : 8,
      "end_offset" : 9,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "test",
      "start_offset" : 10,
      "end_offset" : 14,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "doc",
      "start_offset" : 15,
      "end_offset" : 18,
      "type" : "<ALPHANUM>",
      "position" : 4
    }
  ]
}

2 ES查询

2.1 match

match查询会将查询条件进行分词。命中数据的条件:匹配到查询条件的其中一个分词即可。

以下查询将命中数据,查询条件被分词为"b"和"doc"。

bash 复制代码
GET test_standard_analyzer/_search
{
  "query": {
    "match": {
      "remark":"b doc"
    }
  }
}

2.2 match_phrase

match查询也会将查询条件进行分词。

命中数据的条件:(1)查询条件的所有分词都需要匹配到,(2)相对顺序还要一致,(3)默认(slop=0或者未设置该值)查询条件的分词在es数据中是连续的。

2.2.1 查询条件的分词在es数据中需要是连续的

(1)命中数据

bash 复制代码
GET test_standard_analyzer/_search
{
  "query": {
    "match_phrase": {
      "remark":"a test doc"
    }
  }
}

(2)未命中数据

以下查询未命中数据,因为查询条件的分词在es数据中不连续,中间还间隔一个"test"。

bash 复制代码
GET test_standard_analyzer/_search
{
  "query": {
    "match_phrase": {
      "remark":"a doc"
    }
  }
}

2.2.2 查询条件的分词在es数据中不需要是连续的

slop 参数用于指定中间可省略几个词语。slop > 0时,查询条件的分词在es数据中可以不连续。

因此以下查询将命中数据。

bash 复制代码
GET test_standard_analyzer/_search
{
  "query": {
    "match_phrase": {
      "remark":{
        "query": "a doc",
        "slop": 1
      }
    }
  }
}

3 参考文献

(1)【ES知识】ES基础查询语法一览

相关推荐
夏天是冰红茶12 分钟前
Sphinx和ReadtheDocs构建一个文档网站
搜索引擎·全文检索·sphinx
YFJ_mily18 分钟前
2025第二届机电一体化、机器人与控制系统国际会议(MRCS2025)即将来袭
大数据·人工智能·机器人·机电一体化
倔强的石头1061 小时前
Bright Data MCP+Trae :快速构建电商导购助手垂直智能体
大数据·人工智能
莫彩9 小时前
Mapreduce 工业界批式计算经验汇总(下)
大数据·mapreduce
爱吃面的猫13 小时前
大数据Hadoop之——Flink1.17.0安装与使用(非常详细)
大数据·hadoop·分布式
Fireworkitte14 小时前
安装 Elasticsearch IK 分词器
大数据·elasticsearch
ywyy679815 小时前
短剧系统开发定制全流程解析:从需求分析到上线的专业指南
大数据·需求分析·短剧·推客系统·推客小程序·短剧系统开发·海外短剧系统开发
暗影八度17 小时前
Spark流水线数据质量检查组件
大数据·分布式·spark
白鲸开源17 小时前
Linux 基金会报告解读:开源 AI 重塑经济格局,有人失业,有人涨薪!
大数据
海豚调度17 小时前
Linux 基金会报告解读:开源 AI 重塑经济格局,有人失业,有人涨薪!
大数据·人工智能·ai·开源