Mình có sử dụng Golang để create index trên elasticsearch.
mapping := map[string]interface{}{ "mappings": map[string]interface{}{ "properties": map[string]interface{}{ "TimeStamp": map[string]interface{}{ "type": "date", }, "City": map[string]interface{}{ "type": "text", "fields": map[string]interface{}{ "keyword": map[string]interface{}{ "type": "keyword", }, }, }, "District": map[string]interface{}{ "type": "text", "fields": map[string]interface{}{ "keyword": map[string]interface{}{ "type": "keyword", }, }, }, "Ward": map[string]interface{}{ "type": "text", "fields": map[string]interface{}{ "keyword": map[string]interface{}{ "type": "keyword", }, }, }, "PostTitle": map[string]interface{}{ "type": "text", "fields": map[string]interface{}{ "keyword": map[string]interface{}{ "type": "keyword", }, }, }, "LandArea": map[string]interface{}{ "type": "float", }, "Price": map[string]interface{}{ "type": "float", }, "PostOwner": map[string]interface{}{ "type": "text", "fields": map[string]interface{}{ "keyword": map[string]interface{}{ "type": "keyword", }, }, }, "Priority": map[string]interface{}{ "type": "keyword", }, "NumberOfBedroom": map[string]interface{}{ "type": "integer", }, }, }, }
Giờ chúng ta mổ sẻ từng bổ phần nhỏ
“mappings”:
- This is the root field for the mapping definition.
- It contains the properties that define how the fields in the document will be mapped.
“properties”:
- This field defines the fields that will be present in the documents that are indexed.
- Each property/field is defined with a name and its type, along with additional properties.
"time-stamp": map[string]interface{}{ "type": "date", },
"type": "date"
: Specifies that the field will store date values.
"City": map[string]interface{}{ "type": "text", "fields": map[string]interface{}{ "keyword": map[string]interface{}{ "type": "keyword", }, }, }
“City”: This is the name of the field in Elasticsearch.
map[string]interface{}: This is a Go map, where the key is a string
and the value can be any type (interface{}
). This map represents the configuration or settings of the “City” field in Elasticsearch.
“type”: “text”: The “City” field is of type “text” in Elasticsearch. This means that this field will store textual data and will be analyzed during indexing. Analyzed fields can be searched for partial matches. For example, if you have a city named “San Francisco”, you can search for just “San” and still get a match.
“fields”: Multi-fields allow you to index a single piece of text in multiple ways, typically once as a full text text
field and once as a keyword
field. This is useful for fields where you want to perform full-text search as well as sorting or aggregation.
“type”: “keyword”: This specifies that the “keyword” multi-field is of type “keyword” in Elasticsearch. A keyword is an unanalyzed field, meaning it is taken as-is without any analysis. This is useful for filtering, sorting, and aggregations. When you search against a keyword field, you have to provide the exact term.
Metaphor:
Imagine a library where books are categorized by their titles. The “City” is like the title of a book. You can search for a book by typing only part of its title, say just “Harry” for “Harry Potter”. This is similar to the text
type in Elasticsearch.
Now, imagine you also want to sort the books alphabetically by their exact titles. For this, you need the exact title, like “Harry Potter”, without any variations. This is similar to the keyword
type in Elasticsearch.