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

Mapping an object

The object is the base structure (analogous to a record in SQL). ElasticSearch extends the traditional use of objects, allowing the use of recursive embedded objects.

Getting ready

You need a working ElasticSearch cluster.

How to do it...

You can rewrite the mapping of the order type form of the Mapping base types recipe using an array of items:

{
  "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 following are the most important attributes for an object:

  • properties: This is a collection of fields or objects (we consider them as columns in the SQL world).
  • enabled: This is enabled if the object needs to be processed. If it's set to false, the data contained in the object is not indexed as it cannot be searched (the default value is true).
  • dynamic: This allows ElasticSearch to add new field names to the object using reflection on the values of inserted data (the default value is true). If it's set to false, when you try to index an object containing 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 and the index process is skipped. Controlling the dynamic parameter allows you to be safe about changes in the document structure.
  • include_in_all: This adds the object values (the default value is true) to the special _all field (used to aggregate the text of all the document fields).

The most-used attribute is properties, which allows you to map the fields of the object in ElasticSearch fields.

Disabling the indexing part of the document reduces the index size; however, the data cannot be searched. In other words, you end up with a smaller file on disk, but there is a cost incurred in functionality.

There's more...

There are other properties also which are also rarely used, such as index_name and path, which change how Lucene indexes the object, modifying the index's inner structure.

See also

Special objects, which are described in the Mapping a document, Managing a child document, and Mapping a nested objects recipes in this chapter.