1、_source 字段简介

默认地,Elasticsearch 在 _source 字段存储代表文档体的JSON字符串。本文给大家演示一下 _source 字段相关的用法。

第一步:新建索引,并存入两篇文档做测试数据。

curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/myblog' -d '
{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
    }
}
'

curl -X PUT "localhost:9200/myblog/_doc/1?pretty" -H 'Content-Type: application/json' -d'{
    "id":1,
    "title":"郑爽代孕弃养,惨遭广电彻底封杀",
    "content":"郑爽遭广电彻底封杀,一路走来咎由自取,永久退出娱乐圈大快人心!",
    "date":"2021-01-20"
}'

curl -X PUT "localhost:9200/myblog/_doc/2?pretty" -H 'Content-Type: application/json' -d'{
    "id":2,
    "title":"华晨宇承认有一个女儿,却只字未提张碧晨!",
    "content":"在郑爽事件发酵后,有网友爆出华晨宇和张碧晨有一个女儿,并给其上户口。随后华晨宇承认有一个女儿,张碧晨也发长文回应。",
    "date":"2021-01-21"
}'

第二步:获取文档,查看_source

curl -X POST -H 'Content-Type: application/json'  "localhost:9200/myblog/_search?pretty" -d '{
     "query": {"match_all": {}}
 }'

结果如下所示:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "myblog",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "title" : "郑爽代孕弃养,惨遭广电彻底封杀",
          "content" : "郑爽遭广电彻底封杀,一路走来咎由自取,永久退出娱乐圈大快人心!",
          "date" : "2021-01-20"
        }
      },
      {
        "_index" : "myblog",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "id" : 2,
          "title" : "华晨宇承认有一个女儿,却只字未提张碧晨!",
          "content" : "在郑爽事件发酵后,有网友爆出华晨宇和张碧晨有一个女儿,并给其上户口。随后华晨宇承认有一个女儿,张碧晨也发长文回应。",
          "date" : "2021-01-21"
        }
      }
    ]
  }
}

2、定制_source字段里面的内容

curl -X POST -H 'Content-Type: application/json'  "localhost:9200/myblog/_search?pretty" -d '{
     "query": {"match_all": {}},
     "_source": [ "title", "content" ]
 }'

3、禁用_source字段

尽管源字段非常方便,但它确实会导致索引中的存储开销。因此,可以按如下方式禁用:

curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/myblog' -d '
{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
    },
    "mappings": {
    "_source": {
      "enabled": false
    }
  }
}
'

4、定制_source字段

在本例中,_source字段包括title,content,排除了id和date字段。

curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/myblog' -d '
{
    "settings": {
        "number_of_shards": 1, 
        "number_of_replicas": 0
    }, 
    "mappings": {
        "_source": {
            "includes": [
                "title", 
                "content"
            ], 
            "excludes": [
                "id", 
                "date"
            ]
        }
    }
}
'

5、参考

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html

标签: none

添加新评论