Article 6: Composite data types-arrays, objects, and nesting,

Source: Internet
Author: User
Tags eventbase

Article 6: Composite data types-arrays, objects, and nesting,

ElasticSearch uses a JSON structure to store data. A Key/Value pair is a JSON field, and a Value can be a basic data type or an array, documents (also called objects), or document arrays. Therefore, each JSON document has a hierarchical structure. Composite data types are exponential groups, object types, and nesting types. Each type has the following characteristics:

  • An array field refers to a field with multiple values. Each value is an element of the array field. The element type can be basic or document type;
  • The object type indicates that the field value is a JSON document;
  • Nested fields are a special version of the object type. The ElasticSearch engine indexes nested fields into a single document. If an object array is defined in a nested field, each element (document) in the object array is indexed into a single document, and each document can be queried independently.

1. Object Type

A json document has a hierarchical structure. A document may contain other documents. If a document contains other documents, the document value is of the object type, and its data type is of the object type, elasticSearch sets the document attribute type to object by default, that is, "type": "object ".

For example, when creating an index ing, define the name field as the object type without explicitly defining the type attribute value. The default value is object:

"manager":{     "properties":{        "age":{ "type":"integer"},      "name":{           "properties":{              "first":{"type":"string"},            "last":{ "type":"string"}         }      }   }}

By default, the above document type is indexed as a data structure named after the dot. After the hierarchical structure is expanded, the data structure is composed of Flat key/value pairs:

{  "manager.age":        30,  "manager.name.first": "John",  "manager.name.last":  "Smith"}

2. Out-of-the-box array type

ElasticSearch does not have a special Array data type. However, by default, any field can contain 0 or more values, this means that each field is of the array type by default, except that the data types of element values of the array type must be the same. In ElasticSearch, arrays are out-of-the-box and can be directly used without any configuration.

1. array type

In the same array, the Data Types of array elements are the same. ElasticSearch does not support multiple data types: [10, "some string"]. The commonly used array types are:

  • Character array: ["one", "two"]
  • Integer array: productid: [1, 2]
  • Object (document) array: "user": [{"name": "Mary", "age": 12 },{ "name": "John", "age ": 10}], ElasticSearch expands the object array to {"user. name ": [" Mary "," John "]," user. age ": [12, 10]}

For the document array, each element is a document with the same structure, and documents are not independent. In the document array, a single document cannot be queried independently of other documents. This is because, the association between the internal fields of a document is removed, and each document forms an object array.

Query integer arrays. For example, query a document with productid 1 and 2 by using the multi-term (terms) query type:

{     "query":{        "terms":{           "productid":[ 1, 2 ]      }   }}

2. Object Array

Use the PUT verb to automatically create indexes and document types, and create an object array in the document:

PUT my_index/my_type/1{  "group" : "fans",  "user" : [     {      "first" : "John",      "last" :  "Smith"    },    {      "first" : "Alice",      "last" :  "White"    }  ]}

The ElasticSearch engine expands the object array into a flat data structure and expands the document-type data structure of the preceding example. The document data is similar:

{  "group" :        "fans",  "user.first" : [ "alice", "john" ],  "user.last" :  [ "smith", "white" ]}

Field user. first and user. last is expanded as an array field. However, after expansion, the association between fields in a single document will be lost. In this example, the association between the first and last fields of the expanded document data loss, for example,liceAndwhiteIs lost.

3. nested Data Types

The nested data type is a special version of the object data type. It allows each object in the object array to be indexed. Each object in the array remains independent and can be separately queried for each document, this means that the nested data type retains the internal association between documents. The ElasticSearch engine uses different methods to process nested data types and object arrays. For nested data types, elasticSearch indexes each Nested Document in the array as a single Document. These documents are Hidden and mutually independent. However, keep the association between internal fields of a document and use Nested Query to Query a single document independently of other documents. When creating a field of the nested data type, you must set the type attribute of the field to nested.

1. Create nested fields in index ing

Set the user field to the nested data type. Because each field can be an array by default, the nested field can also be an object array.

"mappings":{     "my_type":{        "properties":{           "group":{ "type":"string"},         "user":{              "type":"nested",            "properties":{                 "first":{ "type":"string"},               "second":{  "type":"string"}            }         }      }   }}

2. assign values to nested Fields

If multiple values are assigned to a nested field, ElasticSearch automatically converts the field value to an array type.

PUT my_index/my_type/1{  "group" : "fans",  "user" : [     { "first" : "John", "last" :  "Smith"},    { "first" : "Alice", "last" :  "White"}  ]}

In ElasticSearch, Nested Documents are indexed as multiple independent hidden documents. These hidden Documents can only be accessed through Nested queries. Each nested document is an element of a nested field (document array. The association between the internal fields of the nested documents is reserved by the ElasticSearch engine, while the nested documents are independent of each other. In this example, ElasticSearch causes the association between Alice and White to be retained, but there is no association between John and White.

By default, up to 50 nested documents can be created for each index. You can set index. mapping. nested_fields.limit to modify the default limit.

Indexing a document with 100 nested fields actually indexes 101 documents as each nested document is indexed as a separate document.

Iv. nested Query

The nested query is used to query nested objects. The execution condition of the nested query is that the nested object is indexed as a single document, and the query function is on the Root document (Root Parent. The nested query is specified by the keyword "nested:

"nested" : {        "path" : "obj1",        "query" : {...}

1. parameters that must be assigned values:

  • Path parameter: Specify the document path of the nested field. The root path is the document at the top level. You can specify the path of the nested document by clicking;
  • Query parameter: query is performed on the nested document of the matching path (parameter path). The query parameter specifies the query condition for executing the nested document.

2. Use nested queries to access nested documents

GET my_index/_search{  "query": {    "nested": {      "path": "user",      "query": {        "bool": {          "must": [            { "match": { "user.first": "Alice" }},            { "match": { "user.last":  "White" }}           ]        }      }    }  }}

5. Use the C # index array type

1. Create an index ing for ElasticSearch

View Code

The topics field is of the integer type and is assigned an integer of [1, 2, 3]. The field can store arrays.

"topics":{      "type":"integer",    "store":true,    "index":"analyzed"}

2. Create a Data Model)

The array field is defined as the List type, and the Data Type of each List item is int.

public class EventBase{    public long eventid { get; set; }}public class EbrieEvents:EventBase{    public string eventname { get; set; }    public List<int> topics { get; set; }}

3. assign values to Fields

Assign values to the List field topics and call NEST to index the document.

EbrieEvents pb = new EbrieEvents();//Topics ListList<string> strTopics = TableRow["Topics"].ToString().TrimEnd(',').Split(',').ToList();List<int> topics = new List<int>();foreach(string str in strTopics){    topics.Add(int.Parse(str));}pb.topics = topics;  

4. query array Fields

{     "query":{        "terms":{           "topics":[1001,487]      }   }}

 

Reference:

Elasticsearch Reference [2.4]» Mapping» Field datatypes

Elasticsearch Reference [2.4]» Query DSL» Joining queries» Nested Query

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.