ElasticSearch Cookbook
上QQ阅读APP看书,第一时间看更新

Mapping an object

The object is the base structure (also called as record in SQL). ElasticSearch extends the traditional use of object, allowing recursive embedded objects.

Getting ready

You will require a working ElasticSearch cluster.

How to do it...

We can rewrite the order mapping of the Mapping base types recipe using an array of items as shown in the following code:

{
  "order" : {
    "properties" : {
      "id" : {"type" : "string", 
      "store" : "yes", "index":"not_analyzed"},
      "date" : {"type" : "date", "store" : "no", 
        "index":"not_analyzed"},
      "customer_id" : {"type" : "string", "store" : "yes",
        "index":"not_analyzed"},
      "sent" : {"type" : "boolean", "store" : "no", 
        "index":"not_analyzed"},
      "item" : {
        "type" : "object",
        "properties" : {
          "name" : {"type" : "string", "store" : "no",
            "index":"analyzed"},
          "quantity" : {"type" : "integer", 
            "store" : "no",
            "index":"not_analyzed"},
          "vat" : {"type" : "double", "store" : "no",
            "index":"not_analyzed"}
        }
      }
    }
  }
}

How it works...

ElasticSearch speaks native JSON. So, every complex JSON structure can be mapped into it.

When ElasticSearch is parsing an object type, it tries to extract fields and processes them as its defined mapping; otherwise, it learns the structure of the object using reflection.

The most important properties for an object are as follows:

  • properties: This is a collection of fields or objects (we can consider them as columns in the SQL world).
  • enabled (defaults to true if the object should be processed): If this is set to false, the data contained in the object is not indexed and it cannot be searched.
  • dynamic (defaults to true): This property allows ElasticSearch to add new field names to the object using reflection on values of inserted data. If it's set to false, when you try to index an object contained in a new field type, it'll be rejected silently. If it's set to strict, when a new field type is present in the object an error is raised skipping the index process. Control dynamic parameters allows being safe about change in the document structure.
  • include_in_all (defaults to true): This property adds the object values to the special _all field (used to aggregate the text of all document fields).

The most used property is properties that allows mapping the fields of the object in ElasticSearch fields.

Disabling the indexing part of a document reduces the index size and allows storing data that must not be searched.

There's more...

Also, there are properties that are rarely used, such as index_name and path, which changes the how Lucene index the object, modifying the inner structure of an index.

See also

  • There are special objects that are described in the Mapping a document, Managing a child document, and Mapping a nested document recipes.