1. Document data format
Document-oriented search analysis engine
(1) The data structure of the application system is object-oriented
(2) Object data stored in the database, can only be disassembled, become more than one table, when queried to revert to the object format
(3) ES is document-oriented, like object-oriented data structures, based on the structure of the document, ES can provide complex indexes, full-text search, analysis, aggregation and other functions
{"Email": "[email protected]", "first_name": "San", "last_name": "Zhang", "info" {"Bio": "Curison and modest", "age": 30 , "Interests": ["Bike", "Climb"]}, "Join_date": "2017-01-01"}
2, e-commerce website Commodity Management case Background Introduction
(1) Crud The commodity information (add and revise)
(2) Perform a simple structured query
(3) Simple full-text search can be performed, as well as complex phrase (phrase) retrieval
(4) For the structure of full-text search, can be highlighted
(5) Simple aggregation analysis of the data
3. Simple Cluster Management
(1) Quickly check the health status of the cluster
Get _cat/health?v
Epoch Timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_t IME Active_shards_percent
1525610669 20:44:29 elasticsearch Green 1 1 0 0 0 0 0 0 -100%
How do you quickly understand the health of your cluster? Green, yellow, red?
Green: Primary shared and replica shared are active states for each index
Yellow: Primary shared for each index is active, part replica shared is not active
Red: Primary shared that is not an index is active, and some of the index data is missing
(2) quickly see which indexes are in the cluster?
Get _cat/indices?v
(3) Simple index operation
CREATE INDEX: Put/test_index?pretty
Delete index: Delete/test_index?pretty
4. CRUD Operations for Commodities
(1) New product: Create new document, index
Put/index/type/id
put/ecommerce/product/1{"name": "Gaolujie Yagao", "desc": "Gaoxiao Meibai", "price": +, "producer": "Gaolujie Produce R "," tags ": [" Meibai "," Fangzhu "]}put/ecommerce/product/2{" name ":" Jiajieshi Yagao "," desc ":" Gaoxiao Fangzhu "," Price ": +," producer ":" Jiajieshi producer "," tags ": [" Fangzhu "]}put/ecommerce/product/3{" name ":" Zhonghua Yagao "," desc ": "Caoben Zhiwu", "price": +, "producer": "Zhonghua producer", "tags": ["Qingxin"]}
return Result:
{"_index": "Ecommerce", "_type": "Product", "_id": "3", "_version": 1, "result": "Created", "_shards": {"Total" : 2, "successful": 1, "Failed": 0}, "_seq_no": 0, "_primary_term": 1}
ES automatically establishes index and type, does not need to be created in advance, and Es defaults to an inverted index on each field of document so that it can be searched
(2) Search for products, retrieve documents
Get/index/type/id
Get/ecommerce/product/2
(3) Modify the product, replace the document
put/ecommerce/product/1{"name": "Jiaqiangban Gaolujie Yagao", "desc": "Gaoxiao Meibai", "price": +, "producer": "Gaol Ujie producer "tags": ["Meibai", "Fangzhu"]}
Replace the way there is a bad place, must take all the field, in order to make information changes, otherwise it will lose data
(4) Modify the product, update the document
post/ecommerce/product/1/_update {"Doc": {"name": "Jiaqiangban Gaolujie Yagao"}}
(5) Deleting a document
Delete/ecommerce/product/1?pretty
Second, the query syntax of ES
1. Query string Search
Get/ecommerce/product/_search Get/ecommerce/product/_search?q=name:yagao&sort=price:desc
{"Took": 54, elapsed number of milliseconds "timed_out": false, whether the timeout "_shards": {"Total": 5, data is split into 5 shards "successful": 5, "skipped ": 0," failed ": 0}, hits": {"Total": 3, Number of query results, 3 document "Max_score": 1,search correlation, the more relevant, the more matching, the higher the score "hits ": Contains detailed data for the searched document
2. Query the language of the DSl-specific domain
Get/ecommerce/product/_search {"Query": {"Match_all": {}}}
Get/ecommerce/product/_search {"Query": {"match": {"name": "Yagao"}}, "Sort": [{"Price": "Desc"}]}
Get/ecommerce/product/_search {"Query": {"Match_all": {}}, "from": 1, "Size": 1}
Get/ecommerce/product/_search {"Query": {"Match_all": {}}, "_source": ["Name", "Price"]}
Search product name contains toothpaste, and the price is more than 25 yuan of goods
Get/ecommerce/product/_search {"Query": {"bool": {"must": [{"Match": {"name": "Ya Gao "}}", "filter": {"range": {"price": {"GTE": 30}}}}
put/ecommerce/product/4{"name": "Special Yagao", "desc": "Special Meibai", "Price": "Producer": "Special Yagao Pro" Ducer "," tags ": [" Meibai "]}
get/ecommerce/product/_search{"Query": {"match": {"producer": "Yagao Producer"}}}
In contrast to the full-text index, the full-text index will disassemble the input search string, to the inverted index inside the one by one match, as long as the match on any one of the disassembled words, can be returned as a result, phrase search requires the input of the searched string, must be in the specified field text, completely contain exactly the same, Just a match.
get/ecommerce/product/_search{"Query": {"match_phrase": {"producer": "Yagao Producer"}}}
get/ecommerce/product/_search{"Query": {"match": {"producer": "Producer"}}, "highlight": {"Fields": {"PR Oducer ": {}}}}
Third, using the RESTful API to do simple aggregation calculations
get/ecommerce/product/_search{"Aggs": {"group_by_tags": {"Terms": {"field": "Tags"}}}
Put/ecommerce/_mapping/product{"Properties": {"tags": {"type": "Text", "Fielddata": True}}}
get/ecommerce/product/_search{"Size": 0, "Aggs": {"group_by_tags": {"Terms": {"field": "Tags"} } }}
get/ecommerce/product/_search{"Size": 0, "query": {"match": {"name": "Yagao"}}, "Aggs": {"Group_by_tags": { "Terms": {"field": "Tags"}}}}
get/ecommerce/product/_search{"Size": 0, "Aggs": {"group_by_tags": {"Terms": {"field": "Tags"}, "Aggs": {"Avg_price": {"avg": {"field": "Price"}}}}}
get /ecommerce/product/_search{ "Size":0, " Aggs ": { " group_by_tags ": { " terms ": { "field": "tags", "Order": { "Avg_price": "desc " } }, "Aggs": { "Avg_price": { "AVG": { "field": "Price" } } } } }}
get /ecommerce/product/_search{ "Size":0, "Aggs": { " Group_by_price ": { " range ": { "field": "Price", "ranges": [{ "from": "0", "to":20 }, { "from": ", " "to": 40 } , { "from": ", " to ":60 &nbSP;} ] }, "Aggs": { "Group_by_tags": { "terms": { "field": "tags" }, "Aggs": { "Avg_price": { "AVG": { "field": "Price" } } } } } } }}
E-commerce website Commodity Management (i)