zoukankan      html  css  js  c++  java
  • mapping 详解3(Meta-Fields)

    文档标识相关元数据字段

    _index

    • 当执行多索引查询时,可能需要添加特定的一些与文档有关联的索引的子句。
    • _index 字段可以用在 term、terms 查询,聚合(aggregations)操作,脚本(script)操作以及用来排序(sort)。
    GET index_1,index_2/_search
    {
      "query": {
        "terms": {
          "_index": ["index_1", "index_2"] 
        }
      },
      "aggs": {
        "indices": {
          "terms": {
            "field": "_index", 
            "size": 10
          }
        }
      },
      "sort": [
        {
          "_index": { 
            "order": "asc"
          }
        }
      ],
      "script_fields": {
        "index_name": {
          "script": "doc['_index']" 
        }
      }
    }
    _index field

    _type

    • _type 可以用来让针对具体 type 的搜索更加快。
    • _type 字段可以用在 querys、aggregations、scripts 以及 sorting。
    GET my_index/_search/type_*
    {
      "query": {
        "terms": {
          "_type": [ "type_1", "type_2" ] 
        }
      },
      "aggs": {
        "types": {
          "terms": {
            "field": "_type", 
            "size": 10
          }
        }
      },
      "sort": [
        {
          "_type": { 
            "order": "desc"
          }
        }
      ],
      "script_fields": {
        "type": {
          "script": "doc['_type']" 
        }
      }
    }
    _type field

    原始信息相关元数据字段

    _source

    字段说明

    • _source 字段存放的是文档的原始 JSON 信息
    • _source 字段不被 indexed ,不过被 stored ,所以可以通过 get 或 search 取得该字段的值。

    禁用_source字段

    • _source 字段可以在 mapping 设置中禁用
    • 如果禁用 _source 字段将会有一些其它影响,比如:update API 将无法使用等等。
    PUT tweets
    {
      "mappings": {
        "tweet": {
          "_source": {
            "enabled": false
          }
        }
      }
    }
    enable _source

    _source排除特定字段

    • 在 _source 的 mapping 设置中可以通过 includes 和 excludes 参数来包含或排除特定字段
    • 包含或排除的字段,需要以 plain 格式的 field 名称,名称支持通配符。
    PUT logs
    {
      "mappings": {
        "event": {
          "_source": {
            "includes": [
              "*.count",
              "meta.*"
            ],
            "excludes": [
              "meta.description",
              "meta.other.*"
            ]
          }
        }
      }
    }
    include _source

    索引操作相关元数据字段

    _all

    字段说明

    • _all 字段把其他所有字段的内容存储到一个大的字符串中,不管其它字段是什么数据类型,在 _all 中都被当作字符串处理。
    • 每个 index 只有一个 _all 字段。
    • 该字符串会被 analyzed 和 indexed,但不会 store(存储)。可以被搜索,但无法用来恢复。
    • _all 字段也和普通字符串字段一样可以接收:analyzer、term_vectors、index_options 和 store 等参数。
    • 生成 _all 字段是有资源消耗的,会消耗 CPU 和 disk 存储。
    GET my_index/_search
    {
      "query": {
        "match": {
          "_all": "john smith 1970"
        }
      }
    }
    _all query

    _all字段查询

    • query_string 和 simple_query_string 查询操作,默认就是查询 _all 字段,除非自己明确指定。
    GET _search
    {
      "query": {
        "query_string": {
          "query": "john smith 1970"
        }
      }
    }
    _all query_string

    禁用_all字段

    • _all 字段可以在 mapping 设置中完全禁用,如果禁用,query_string 和 simple_query_string 查询操作需要指定默认字段才可用。
    PUT my_index
    {
      "mappings": {
        "my_type": {
          "_all": {
            "enabled": false 
          },
          "properties": {
            "content": {
              "type": "string"
            }
          }
        }
      },
      "settings": {
        "index.query.default_field": "content" 
      },
    }
    enable _all

    _all排除特定字段

    • 字段通过 mapping 设置可以通过 include_in_all 参数控制该字段否包含在 _all 字段。
    PUT my_index
    {
      "mappings": {
        "my_type": {
          "properties": {
            "date": { 
              "type": "date",
              "include_in_all": false
            }
          }
        }
      }
    }
    include_in_all

    _all字段存储

    • _all 字段可以通过参数 store 来设置其是否存储。
    PUT myindex
    {
      "mappings": {
        "mytype": {
          "_all": {
            "store": true
          }
        }
      }
    }
    store _all

    _field_names

    字段说明

    • _field_names 字段是用来存储文档中所有非 null 字段的字段名称的。
    • 该字段供 existsmissing 查询使用,来查询某个文档中是否包含或不包含某个字段。
    GET my_index/_search
    {
      "query": {
        "terms": {
          "_field_names": [ "title" ] 
        }
      },
      "aggs": {
        "Field names": {
          "terms": {
            "field": "_field_names", 
            "size": 10
          }
        }
      },
      "script_fields": {
        "Field names": {
          "script": "doc['_field_names']" 
        }
      }
    }
    _field_names

    路由相关元数据字段

    _parent

    字段说明

    • 在同一个 index 中,可以通过设置 type 的父子关系来建立文档之间的父子关系。
    • 父子 type 必须是不同的 type。
    • 指定的 parent type 必须要是还不存在的,已存在的 type 不能作为其它 type 的 parent type。
    • 父子关系的 doc 必须被索引到相同的 shard 上,子文档通过参数 parent 参数来作为其 routing 来保证索引到相同分片。 
    PUT my_index
    {
      "mappings": {
        "my_parent": {},
        "my_child": {
          "_parent": {
            "type": "my_parent" 
          }
        }
      }
    }
    _parent

    _routing

    • _routing 字段用来确定文档索引的分片:shared_num = hash(routing) % num_primary_shards
    • 默认的 _routing 是文档的 _id 或 _parent 的 ID。
    • 通过 routing 参数可以自定义 _routing 的值。
    GET my_index/_search
    {
      "query": {
        "terms": {
          "_routing": [ "user1" ] 
        }
      },
      "aggs": {
        "Routing values": {
          "terms": {
            "field": "_routing", 
            "size": 10
          }
        }
      },
      "sort": [
        {
          "_routing": { 
            "order": "desc"
          }
        }
      ],
      "script_fields": {
        "Routing value": {
          "script": "doc['_routing']" 
        }
      }
    }
    _routing
  • 相关阅读:
    [ERR] Node 10.211.55.8:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
    PAT A1137 Final Grading (25 分)——排序
    PAT A1136 A Delayed Palindrome (20 分)——回文,大整数
    PAT A1134 Vertex Cover (25 分)——图遍历
    PAT A1133 Splitting A Linked List (25 分)——链表
    PAT A1132 Cut Integer (20 分)——数学题
    PAT A1130 Infix Expression (25 分)——中序遍历
    PAT A1142 Maximal Clique (25 分)——图
    PAT A1141 PAT Ranking of Institutions (25 分)——排序,结构体初始化
    PAT A1140 Look-and-say Sequence (20 分)——数学题
  • 原文地址:https://www.cnblogs.com/licongyu/p/5492261.html
Copyright © 2011-2022 走看看