Elasticsearch查询上下文和_source

查询上下文

json 复制代码
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "user",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "admin",
          "age": 18,
          "firends": [
            "小明",
            "小白",
            "小吕"
          ],
          "owner": {
            "name": "desc"
          }
        }
      }
    ]
  }
}
  1. took:查询时间,毫秒
  2. time_out:是否超时
  3. _shards:当前请求的分片
  4. total:分片数量
  5. successful:成功数量
  6. skipped:跳过数量
  7. failed:失败数量
  8. hits:查询结果
  9. total:
    1. value:查询结果条数
    2. relation:查询方式 如"eq"等于
    3. max_score:相关度评分
    4. hits:当前返回的结果
      1. _index:数据属于的索引
      2. _type:ES7.0后为_doc
      3. _id:当前数据id
      4. _score:当前数据相关度评分
      5. _source:元数据

相关度评分

在查询时候,如果不指定排序字段,默认使用相关度评分排序,评分度越高排名越靠前。

元数据

类似于mysql中的查询字段,*表示查询所有数据,相当于:

json 复制代码
get user/_search
{
  "query":{
    "match_all":{}
  }
}

禁用元数据

  1. 查询时候禁用元数据:
json 复制代码
get user/_search
{
  "_source":false,
  "query":{
    "match_all":{}
  }
}

结果:hits中只包含id

json 复制代码
{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "user",
        "_id": "1",
        "_score": 1
      }
    ]
  }
}
  1. 使用Mapping禁用元数据:通过includes指定要查询的字段,excludes指定不查询的字段,excludes优先级高于includes,不推荐使用,因为mapping创建后不能修改。
json 复制代码
put user2
{
  "mappings": {
    "_source": {
      "includes": [
        "name",
        "age"
      ],
      "excludes": [
        "firends",
        "owner"
      ]
    }
  }
}
put user2/_doc/1
{
  "name":"admin",
  "age":18,
  "firends":[
      "小明",
      "小白",
      "小吕"
    ],
  "owner":{
    "name":"desc"
  }
}
get user2/_search

结果:

json 复制代码
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "user2",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "admin",
          "age": 18
        }
      }
    ]
  }
}

查询时候_source的使用

和Mysql中的select指定查询的字段作用一样

  1. 可使用通配符
json 复制代码
get user/_search
{
  "_source": "owner.*",
  "query":{
    "match_all": {}
  }
}

结果:

json 复制代码
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "user",
        "_id": "1",
        "_score": 1,
        "_source": {
          "owner": {
            "name": "desc",
            "other": "other"
          }
        }
      }
    ]
  }
}
  1. 使用includes和excludes
json 复制代码
get user/_search
{
  "_source":
  {
    "includes":
    [
      "name",
      "owener.name"
    ],
    "excludes":
    [
      "age"
    ]
 
  },
  "query":{
    "match_all": {}
  }
}

结果:

json 复制代码
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "user",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "admin"
        }
      }
    ]
  }
}
  1. 禁用_source
json 复制代码
get user/_search
{
  "_source":false,
  "query":{
    "match_all":{}
  }
}

query_string查询

  1. 查询所有

get /user/_search

  1. 带参查询

get /user/_search?p=name:admin

  1. 分页查询

get /user/_search?from=0&size=2&sort=age:asc

  1. 精准匹配

get /user/_search?q=date=2024-06-29

  1. _all查询,在所有的有索引的字段中匹配

get /user/_search?q=2024-06-29

注意

  1. 第4种查询是精准匹配,只会匹配date字段
  2. 第5种查询是全文匹配,会匹配所有有索引的字段
  3. 如果执行 get /user/_searct?q=2024,则date类型的字段不会被检索,因为date类型只支持精准匹配
相关推荐
武子康3 分钟前
大数据-202 sklearn 决策树实战:criterion、Graphviz 可视化与剪枝防过拟合
大数据·后端·机器学习
hk112414 分钟前
【Algo/Forensics】2026年度无损压缩算法与高阶网络取证基准索引 (Benchmark Index)
大数据·算法·网络安全·系统架构·数据集
Elastic 中国社区官方博客19 分钟前
Elasticsearch:在 Streams 中使用 ML 自动化 log 解析
大数据·运维·elk·elasticsearch·搜索引擎·自动化·全文检索
chenshi178133 分钟前
匠厂和普通SEO工具有什么区别?深度评测GEO自动化效率
大数据·人工智能
kong79069281 小时前
Hadoop介绍HDFS介绍
大数据·hadoop·分布式
小北方城市网1 小时前
GEO 新生态:跨界融合 + 场景渗透,重构 AI 时代本地商业增长版图
大数据·网络·人工智能·python·状态模式
oMcLin1 小时前
如何在Debian 11服务器上部署并优化高性能Elasticsearch集群,处理PB级数据?
服务器·elasticsearch·debian
天码-行空1 小时前
【大数据环境安装指南】Flink的Standalone Cluster(独立集群)部署教程
大数据·linux·运维·flink
linweidong1 小时前
Spark Shuffle的优化
大数据·分布式·spark
Nightmare0042 小时前
Mac 安装brew
大数据·macos