es-从搜索中检索选定的字段

本文详细介绍了在Elasticsearch中如何检索选定的字段,包括使用fields选项、_source选项、docvalue_fields、stored_fields和script_fields。重点讨论了fields的优势,如返回标准化值、支持多字段及格式化数据类型,以及对nested和未映射字段的处理。此外,还解释了docvalues的作用,如支持排序和聚合,以及与_source、stored_fields的区别。
摘要由CSDN通过智能技术生成

从搜索中检索选定的字段

Retrieve selected fields from a search(文档学习)

https://www.elastic.co/guide/en/elasticsearch/reference/7.13/search-fields.html#source-filtering

默认情况下,搜索响应中的每个命中都包括document的 _source部分,这是在索引文档时提供的整个JSON对象。
在这里插入图片描述

建议使用两种方法从搜索查询中检索选定字段:
(1)使用 fields 选项提取索引映射中存在的字段的值
(2)如果需要访问在索引时传递的原始数据,请使用_source选项
我们可以同时使用这两种方法,但首选fields选项,因为它同时参考文档数据和索引映射。在某些情况下,我们可能希望使用其他方法检索数据。
其他的话还有三种,补充使用
(3)docvalue_fields
使用 docvalue_fields 参数获取选定字段的值。当返回一个数量比较少的doc value(例如keyword和date),这可能是一个不错的选择。
(4)stored_fields
使用 stored_fields 参数获取特定存储字段(使用store映射选项的字段)的值。
(5)script_fields
使用 script_fields 参数来检索每个命中的脚本评估,提取/输出新字段。

The fields option

要检索搜索响应中的特定字段,请使用fields参数。因为它参考索引映射,所以fields参数提供了几个优于直接引用_source的优点。具体而言,字段参数:

  • 以与映射类型匹配的标准化方式返回每个值
  • 接受multi-fileds和field aliases
  • 格式化date和spatial(空间)数据类型
  • 检索runtime field values
  • 返回脚本在索引时计算的字段

还考虑了其他映射选项,包括上面的ignore_above、ignore_malformed和null_value。

  • ignore_above
    字符串元素的长度超过ignore_above不会被索引或存储

  • ignore_malformed
    允许的映射类型中忽略格式错误的内容

  • null_value

    fields选项返回值的方式与Elasticsearch索引值的方式相匹配。对于标准字段,这意味着fields选项在_source中查找值,然后使用映射解析并格式化它们。

(1)Search for specific fields

搜索指定的字段。比如text,keyword,objec,nested,date,spatial等字段都可以。
使用对象表示法,可以传递format参数以自定义返回日期或地理空间值的格式。

  • date and date_nanos 字段 使用date from
  • Spatial fields 使用 GeoJSON/wkt

以下搜索请求使用fields参数检索user.id字段、以http.response.开头的所有字段和@timestamp字段的值。

数据源
##数据源
DELETE my-index-000001

##spatial 要定类型,必须先创建mapping
PUT my-index-000001
{
   
  "mappings": {
   
    "properties": {
   
      "location.one": {
   "type": "geo_point"},
      "location.two": {
   "type": "geo_point"},        
      "location.three": {
   "type": "geo_point"},        
      "location.four": {
   "type": "geo_point"},        
      "location.five": {
   "type": "geo_point"},
      "username": {
   
        "type": "nested",
        "properties": {
   
          "first" : {
    "type" : "keyword" },
          "last" : {
    "type" : "keyword" }
        }
      },
      "timestamp":{
   
           "type": "date",
           "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
           "ignore_malformed": false,
           "null_value": null
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
   
  "user": {
   
    "id": "kimchy",
    "name": "kimchy wang",
    "age": 12
  },
  "http": {
   
    "response": {
   
      "status_code": 200,
      "bytes": "1070000"
    }
  },
  "@timestamp": "2018-10-18T12:20:51.603Z",
  "timestamp": 1539865251603,
  "ip_addr": "192.168.1.1",
  "location": {
   
    "one": {
   "lat": 41.12,"lon": -71.34},
    "two": "41.12,-71.34",
    "three": "drm3btev3e86",
    "four": [41.12, -71.34],
    "five" : "POINT (41.12 -71.34)"
  },
  "username":{
   
    "first": "John",
    "last": "Smith"
  }
}

##查看mapping结构
GET /my-index-000001/_mapping
fields指定字段 请求
##1.object字段
##2.前缀模糊匹配
##3.date 用户epoch_millis输出
##4.Spatial 用wkt输出
##"_source": false 不返回_source
POST my-index-000001/_search
{
   
  "query": {
   
    "match": {
   
      "user.id": "kimchy"
    }
  },
  "fields": [
    "user.id",
    "http.response.*",         
    {
   
      "field": "@timestamp",
      "format": "epoch_millis" 
    },
    {
   
      "field": "location.*",
      "format": "geojson"
    },
     {
   
      "field": "location.*",
      "format": "wkt"
    }
  ],
  "_source": false
}

fields指定字段返回 响应结果
##1.object字段
##2.前缀模糊匹配
##3.date 用户epoch_millis输出
##4.Spatial 用wkt输出
##"_source": false 不返回_source
{
   
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
   
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
   
    "total" : {
   
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
   
        "_index" : "my-index-000001",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "fields" : {
   
          "@timestamp" : [
            "1539865251603"
          ],
          "location.three" : [
            "POINT (-71.34000029414892 41.119999922811985)"
          ],
          "user.id" : [
            "kimchy"
          ],
          "http.response.bytes.keyword" : [
            "1070000"
          ],
          "http.response.bytes" : [
            "1070000"
          ],
          "location.two" : [
            "POINT (-71.34 41.12)"
          ],
          "location.one" : [
            "POINT (-71.34 41.12)"
          ],
          "location.five" : [
            "POINT (41.12 -71.34)"
          ],
          "http.response.status_code" : [
            200
          ],
          "location.four" : [
            "POINT (41.12 -71.34)"
          ]
        }
      }
    ]
  }
}

(2)Response always returns an array

  • 响应总是返回一个数组。
  • fields响应始终为每个字段返回一个值数组,即使_source中只有一个值。这是因为Elasticsearch没有专用的数组类型,任何字段都可能包含多个值。fields参数也不能保证数组值按特定顺序返回。
  • 响应将值作为一个平面列表包含在每个命中的字段部分中。因为fields参数不获取整个对象,所以只返回leaf字段。
  • 这里对比nested的结果看,就知道平铺列表形式了。
    在这里插入图片描述

(3)fields response for nested 检索nested字段

  • nested字段的字段响应与常规对象字段的字段响应略有不同。常规对象字段中的叶值以平面列表的形式返回,而嵌套字段中的值则分组以保持原始嵌套数组中每个对象的独立性。对于嵌套字段数组中的每个条目,值将再次以平面列表的形式返回,除非父嵌套对象中存在其他嵌套字段,在这种情况下,对更深的嵌套字段再次重复相同的过程。
  • 给定以下映射,其中user是嵌套字段,检索用户字段下的所有字段:
数据源
DELETE my-index-000002

PUT my-index-000002
{
   
  "mappings": {
   
    "properties": {
   
      "group" : {
    "type" : "keyword" },
      "user": {
   
        "type": "nested",
        "properties": {
   
          "first" : {
    "type" : "keyword" },
          "last" : {
    "type" : "keyword" }
        }
      },
      "addr":{
   
        "properties": {
   
          "country": {
    "type" : "keyword" },
          "province": {
    "type" : "keyword" },
          "city": {
    "type" : "keyword" },
          "description": {
    "type" : "keyword" } 
        }
      }
    }
  }
}
##user-nested tel-object(非定义) add-object(定义) 
PUT my-index-000002/_doc/1?refresh=true
{
   
  "group": "fans",
  "user": [
    {
   
      "first": "John",
      "last": "Smith"
    },
    {
   
      "first": "Alice",
      "last": "White"
    }
  ],
  "tel": {
   
    "number": "13334567890",
    "area_code": "86"
  },
  "addr": {
   
    "country": "CN",
    "province": "YN",
    "city": "KM",
    "description": "地址信息"
  }
}

GET /my-index-000002/_mapping

fields指定nested 请求
  • 1.tel-object(非定义) 区分了text,keyword
  • 2.add-object(定义) keyword
  • 3.user-nested
POST my-index-000002
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值