Article for reference, the original link https://www.dubby.cn/detail.html?id=9077
Basic syntax
1. Using Tools
Postman
2. Index creation
PUT http://127.0.0.1:9200/people/
{" settings": { "number_of_shards": 3,//number of shards, default 5 "Number_of_replicas": 1///Number of backups },
Settings can not be set, by default
Construct a structured index that maps "mappings": { "person": {//struct type "Properties": {//Set property "name": { "type": "Text"// Data type }, "age": { "type": "Integer" }, "Sex": { "type": "Text" }, "birthday ": { " type ":" Date ", " format ":" Yyyy-mm-dd HH:mm:ss | | Yyyy-mm-dd | | Epoch_millis "//Epoch_millis timestamp
}
3. Inserting data
PUT HTTP://127.0.0.1:9200/PEOPLE/PERSON/1
{ "name": "Lyc", "sex": "Male", " Birthday": "1993-01-20", "Country": "China", "age": 20}
The 1 after this URI represents the ID of this data, or it can be a string. If you do not want to specify the ID, you can not pass, but you must use post to add, so that Elasticsearch will give this data to generate a random string.
If you want to update this data, you can re-request the URI, the key is to specify the ID, and then modify the JSON content, so that the data can be updated.
4. Retrieving data
Retrieve a specific piece of data by ID:GET HTTP://127.0.0.1:9200/PEOPLE/PERSON/1
Query effect:
{ "_index": "People", "_type": "Person", " _id": "1", "_version": 1, "found": True, "_ Source ": { " name ":" Lyc ", " sex ":" Male "," Birthday ":" 1993-01-20 ", " Country ":" China ", " age ": 20 }}
Where _source is the data information we store, and if the request method GET is changed to delete, the data is deleted
5. The simplest search
GET Http://127.0.0.1:9200/people/_search
Results:
{"Took": 9, "timed_out": false, "_shards": {"Total": 3, "successful": 3, "skipped": 0, "Failed": 0}, "hits": {"Total": 2, "Max_score": 1, "hits": [{ "_index": "People", "_type": "Person", "_id": "2", "_score": 1, "_source": {"name": "Lyc", "Sex": "Male", "Birthday": "1993-01-20 "," Country ":" China "," Age ": 18}}, { "_index": "People", "_type": "Person", "_id": "1", "_score": 1, "_source": {"name": "Nini", "Sex": "female", "Birthday": "1995 -01-20 "," Country ":" China "," Age ": 18}}]}}
Searches for all documents of type under the specified index, the default is only 10 per page, you can change this setting through the Size field, and you can specify the displacement from the From field (by default, starting at position 0). The took field of the returned result indicates the time-consuming (in milliseconds) of the operation, the Timed_out field indicates whether the time-out, and the Hits field represents the hit record
6. Simple Condition Search
Querying data with name LYC
GET HTTP://127.0.0.1:9200/PEOPLE/_SEARCH?Q=NAME:LYC
7. Conditional Search
POST Http://127.0.0.1:9200/people/_search
Parameters:
{" query": {" match": { "name": "Nini"}} }
Results:
{ "took": 9, "Timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "Failed": 0 }, "hits": { "total": 1, "Max_score": 0.2876821, "hits": [ { "_ Index ":" People "," _type ":" Person ", " _id ":" 1 ", " _score ": 0.2876821, " _source ": { " name ": "Nini", "sex": "female", "Birthday": "1995-01-20", "Country": "China", "age": [ ]} ] }}
This query is the same as the example above, but the parameters are changed from simple parameters to a complex JSON, but the advantage of complexity is that control is more powerful, we can make more granular control of the query.
8. More complex search
POST Http://127.0.0.1:9200/people/_search
{"Query": {"bool": {"must": {" match": { "name": "Nini" } }, "filter": { " Range ": {" Age ": {" GT ": Ten}}}}}
Here is a new range filter, GT means _ greater than (_great than)
9. Full-Text Search
POST Http://127.0.0.1:9200/people/_search
{" query": {" match": { "name": "Nini Lyc"}} }
Search for data with the word Nini or LYC in name, and if exact match, change match to Match_phrase
10. Highlight Search
POST Http://127.0.0.1:9200/people/_search
{" query": {"match": {" name": "Nini Lyc" } }, "highlight": {"Fields ": { "name": {} } }}
Results:
{"Took": "Timed_out": false, "_shards": {"Total": 3, "successful": 3, "skipped": 0, "failed": 0}, "hits": {"Total": 2, "Max_score": 0.2876821, "hits": [{ "_index": "People", "_type": "Person", "_id": "2", "_score": 0. 2876821, "_source": {"name": "Lyc", "Sex": "Male", " Birthday ":" 1993-01-20 "," Country ":" China "," Age ": 18}, "Highlight": {"name": ["<em>Lyc</em>"]}}, {" _index ":" People "," _type ":" Person "," _id ":" 1 "," _score ": 0.2876821," _source ": { "Name": "Nini", "Sex": "female", "Birthday": "1995-01-20", "Coun Try ":" China "," Age ": +}," highlight ": {" name ": [ "<em>Nini</em>" ] } } ] }}
Returns a highlight part of the result, the default is to use a <em></em>
package
11. Simple Aggregation
Before aggregating, you need to make some changes because Elasticsearch does not support data aggregation for text type by default, so you need to first turn on the"fielddata": true
PUT http://127.0.0.1:9200/people/
{" settings": { "number_of_shards": 3, "Number_of_replicas": 1 }, "mappings": { "person" : { "Properties": { "name": {" fielddata": True, "type": "Text" }, "age": { "Type": "Integer" }, "Sex": { "type": "Text" }, "Birthday": { "type": "Date", " Format ":" Yyyy-mm-dd HH:mm:ss | | Yyyy-mm-dd | | Epoch_millis " }, " Country ": { " type ":" Keyword " } }} }
Aggregation by name
POST Http://127.0.0.1:9200/people/_search
{"Aggs": {"all_interests": {" terms": { "field": "Name" }}}
Results:
{"Took": "Timed_out": false, "_shards": {"Total": 3, "successful": 3, "skipped": 0, "Failed": 0}, "hits": {"Total": 3, "Max_score": 1, "hits": [{ "_index": "People", "_type": "Person", "_id": "2", "_score": 1, "_source": {"name": "Liyingchun", "Sex": "Male", "Birthday": "19 93-01-20 "," Country ":" China "," Age ": 20}}, { "_index": "People", "_type": "Person", "_id": "1", "_score": 1, "_source": {"name": "Lyc", "Sex": "Male", "Birthday" : "1993-01-20", "Country": "China", "Age": 20}, {"_indeX ":" People "," _type ":" Person "," _id ":" 3 "," _score ": 1," _so Urce ": {" Aggs ": {" all_interests ": {" Terms ": { "Field": "Name"}}} }}]}, "aggregations": {"all_interests": {"Doc_count_error_upper_ Bound ": 0," Sum_other_doc_count ": 0," buckets ": [{" Key ":" Liying Chun "," Doc_count ": 1}, {" Key ":" Lyc ", "Doc_count": 1}]}}
Post-Filtration Aggregation
{" query": {" match": { "name": "Lyc" } }, "Aggs": {" all_interests": {" terms": {" Field ":" Name "}}}}
Results:
{ "_index": "People", "_type": "Person", " _id": "3", "_version": 4, "result": "deleted", "_shards": { "total": 2, "successful": 2, "failed": 0 }, "_seq_no": 8, "_ Primary_term ": 1}
12. Counting
POST Http://127.0.0.1:9200/_count
{" query": { "Match_all": {}} }
Results:
{ "Count": 2, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }}
Count on a type
POST Http://127.0.0.1:9200/people/person
{" query": { "name": "Lyc" }}
Results:
{ "_index": "People", "_type": "Person", "_id": "9-31kgybiqswquax7d-s", "_version": 1, " Result ":" Created "," _shards ": { " total ": 2, " successful ": 2, " failed ": 0 }, " _seq_no ": 0, " _primary_term ": 1}
13. Cluster Health
GET Http://127.0.0.1:9200/_cluster/health
Results:
{ "cluster_name": "Liyingchun", "status": "Green", "timed_out": false, "Number_of_nodes": 2, " Number_of_data_nodes ": 2, " Active_primary_shards ": 3, " Active_shards ": 6, " Relocating_shards ": 0, "Initializing_shards": 0, "unassigned_shards": 0, "delayed_unassigned_shards": 0, "number_of_ Pending_tasks ": 0, " Number_of_in_flight_fetch ": 0, " Task_max_waiting_in_queue_millis ": 0, " active_ Shards_percent_as_number ": 100}
14, monitoring a single node
GET Http://127.0.0.1:9200/_nodes/stats
15. Cluster statistics
GET Http://127.0.0.1:9200/_cluster/stats
Elasticsearch (b)