A, reference

Elasticsearch Learning Series Directory — Update ing

Nested field type

flattened field type

Elasticsearch: Flatened data type (new in the 7.3 release)

join field type

Elasticsearch Nested type in detail

Elasticsearch – The Trouble with Nested Documents

Second, the nested

2.1 The field value is a list

When the nested field type is not used, the value of a field is a list

As shown in the figure above, the information of users “Zhang San” and “Li Si” are mixed together. At this time, searching for “Zhang Si” can also be obtained

PUT yztest _doc / {1 "group" : "fans", "user" : [{" first ":" zhang ", "last" : "3"}, {" first ":" li ", "last" : "Four"}}] GET yztest / _search {" query ": {" bool" : {" filter ": [{" term" : {". The user first. The keyword ":" zhang "}}, {" term ": {"user.last.keyword": "four"}}]}}}

2.2 set upnestedtype

As shown in the figure above, when the user field type is set to Nested, within Lucene it will be stored as three documents, where each element of the Nested type exists as a separate document

Note: Querying Nested fields requires the use of the Nested query syntax

DELETE yztest PUT yztest/ { "mappings": { "properties": { "user": { "type": "Nested"}}}} PUT yztest _doc / {1 "group" : "fans", "user" : [{" first ":" zhang ", "last" : "3"}, {" first ":" li ", "last" : "Four"}}] GET yztest / _search {" query ": {" nested" : {" path ":" user ", "query" : {" bool ": {" filter" : [{" term ": {" the user first. The keyword ":" zhang "}}, {" term ": {" user. Last. Keyword" : "3"}}]}}}}}

2.3 Usage Scenario

Subdocuments (e.g., User above), rarely updated, frequently queried scenarios

2.4 Restrictions

Three,join

3.1 definejoinfield

Similar to the join association function in MySQL, ES has a corresponding join type, which can define the parent-child association relationship through the join type

DELETE yztest

PUT yztest/
{
  "mappings": {
    "properties": {
      "my_id": {
        "type": "keyword"
      },
      "my_join_field": {
        "type": "join",
        "relations": {
          "question": "answer"
        }
      }
    }
  }
}

3.2 Usage Scenario

If the data contains a one-to-many relationship, and the subfields of the association relationship may have significantly more values than the parent-child segment (for example, a 1-to-many relationship is used to price goods and goods, and the same item may be priced in many different ways, then you can use the Join type definition).

3.3 Restrictions on use

3.4 the query

GET yztest/_search {"query": {"has_child": {"type": "answer", "query": {"match": {"text": "Is answer"}}}}} # GET yztest/_search {"query": {"has_parent": {"parent_type": "question", "query": { "match": { "text": "is question" } } } } }
My_join_filed #question _id GET yztest/_search {"size": 0, "aggs": {"a1": {"terms": { "field": "my_join_field#question", "size": 10 } } } }

Four,flattened

4.1 Generation background

By default, all subfields of an object type field are indexed and mapped (flattened) separately, which can cause mapping to explode 💥,

ES, via the Flattened type, provides a compromise solution,

(1) The Flattened type maps the entire object into an entire field (2), but the Flattened type only provides partial query functionality

4.2 Definition and use

DELETE yztest

PUT yztest/
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "labels": {
        "type": "flattened"
      }
    }
  }
}
# POST yztest/_doc/1 {"title": "Results are not sorted correctly.", "labels": {"priority": "urgent", "release": ["v1.2.5", "v1.3.0"], "timestamp": {"created": 1541458026, "closed": 1541457010}}} POST yztest/_doc/2 {"title": "Results are not sorted correctly.", "labels": { "other": "this is a test label" } }
# term query GET yztest/_search {"query": {"term": {"label. Release ": {"value": "V1.2.5"}}}} # queryString GET yztest/_search {"profile": true, "query": {"query_string": { "default_field": "labels.other", "query": "*this*" } } }

4.3 Query Restrictions