Elasticsearch Mapping Study

Source: Internet
Author: User
Tags curl elasticsearch mapping

Mapping is the definition of the field name and data type of the index library in ES, and it can control whether the field is stored, participle, etc. ES is similar to the relational database we use, and mapping is similar to the table structure. Typically, when the index data is first created, the mapping structure is automatically generated based on the data structure and is not created manually. It is also possible to manually create the JSON-type mapping data via the API, which is more convenient by using the mapping configuration file.

The configuration file is used in our project. Create mapping online A lot of information, no longer nonsense. The main record is the use of the parent-child structure in mapping. To create the mapping of a parent-child structure, you first configure the structure, which is roughly as follows: (music creates two type (song, rock), where Song-rock is a parent-child structure)

{"Music": {"mappings": {"song": {"Properties": {"name": {
                    "type": "string",
                    "index" : "Not_analyzed"
                  },
                  "author": {
                      "type": "String"}}
            ,
            "Rock": {
               "_parent": {     
                    "type": "Song"  
                },
                "Properties": {
                  "lyric": {
                    "type": "string"
        }}} }
    }
}</span>


Parent-child structure in the child index of the increase, delete when the same as normal, need to add parentid parameters. For example, add a rock:

Curl-xpost ' http://localhost:9200/music/rock?parent=3 '-d ' {"lyric": "Yo Yo Yo, Spacek"} '

The parent=3 here corresponds to song's data with ID 3. What if you don't have the parent parameter at this time? Of course, the error:

{"
  error": {"
    root_cause": [
      {
        "type": "Null_pointer_exception",
        "Reason": "ID must not is null"
      }
    ],
    "type": "Null_pointer_exception",
    "Reason": "ID must not is null"
  },
  "status": 500
}

In general, the index of the parent-child structure is created in order to query to the parent through the son, or vice versa. Let's introduce the way to get the parent through the child index content. In the query API of ES, there are has_child and has_parent that can satisfy the index data query of Parent-child structure.

Curl-xpost ' Http://localhost:9200/music/song/_search '-d ' {
    "query": {
        "Has_child": {
            "type": "Rock",
            "Query": {
                "match": {
                    "lyric": "Spacek"}
        } 
    }}
'
Results:

{
  "took": "
  timed_out": false,
  "_shards": {"Total
    ": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "Max_score": 1,
    "hits": [
      {
        "_index": "Music",
        "_type": " Song ",
        " _id ":" 4 ",
        " _score ": 1,
        " _source ": {
          " name ":" The most dazzling national wind ",
          " author ":" Phoenix Legend "
        }
      }
    ]
  }
}
By using the parent to find the child, the opposite is true, as long as the has_child is replaced with has_parent.

Note: In the es2.0+ version, the creation of mapping no longer supports the creation of new mapping when the existing as its own parent must be created at the same time, while es1.0+ does not have this limitation.



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.