Elasticsearch整理之Meta-Fields

本文介绍了Elasticsearch中的Meta字段,包括_index、_id、_source、_field_names、_routing和_meta的详细作用。_index标识文档所属的索引,_id是文档的唯一标识,_source存储原始文档内容,关闭后将影响某些功能。_routing用于计算文档所在的分片,可以自定义。_meta字段用于存储元信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. _index

文档所属的索引。https://www.elastic.co/guide/en/elasticsearch/reference/6.3/mapping-source-field.html

2. _id

每个文档都有一个独一的id标识它,id可用于特定的查询 (termtermsmatchquery_stringsimple_query_string).

PUT my_index/_doc/1
{
  "text": "Document with ID 1"
}

PUT my_index/_doc/2
{
  "text": "Document with ID 2"
}

GET my_index/_search
{
  "query": {
    "terms": {
      "_id": [ "1","2" ] 
    }
  }
}

3. _source

存储了原始的文档。若要关闭,须将enabled属性设为false。但是,如果关闭_source,那么下面的功能将不再支持

      (1)update、update_by_query、reindex

      (2)使用高亮

      (3)改变索引、改变mapping、升级索引

4. _field_names

5. _routing

ES使用下面的公式计算文档被放到哪个分片上

shard_num = hash(_routing) % num_primary_shards

_routing的默认值是文档的_id,可以通过设置_routing来设置自定义路由。下面的代码使用user1作为路由值,在GET数据时也要使用路由值

PUT my_index/_doc/1?routing=user1&refresh=true 
{
  "title": "This is a document"
}

GET my_index/_doc/1?routing=user1 
GET my_index/_search
{
  "query": {
    "terms": {
      "_routing": [ "user1" ] 
    }
  }
}

GET my_index/_search?routing=user1,user2 
{
  "query": {
    "match": {
      "title": "document"
    }
  }
}

当需要使用自定义路由时,为了保险起见,需将_routing设为required,这样CRUD操作都要指定路由否则会报错

PUT my_index2
{
  "mappings": {
    "_doc": {
      "_routing": {
        "required": true 
      }
    }
  }
}

// 抛出路由缺失异常
PUT my_index2/_doc/1
{
  "text": "No routing value provided"
}

6. _meta

https://www.elastic.co/guide/en/elasticsearch/reference/6.3/mapping-meta-field.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值