elasticsearch common command operation set

1. Request command format

The components of the request sent to Elasticsearch are the same as other ordinary HTTP requests:

curl -H "Content-Type: application/json" -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
  • VERB HTTP method. GET, POST, PUT, HEAD, DELETE
  • PROTOCOL. http or https protocol (only available when there is https proxy in front of Elasticsearch)
  • HOST . Elasticsearch is the host name of any node in the cluster. If it is a local node, it is called localhost
  • PORT. The port of Elasticsearch HTTP service. The default is 9200
  • PATH . API path (for example, _count will return the number of documents in the cluster). The path can contain multiple components, such as_ cluster/stats or_ nodes/stats/jvm
  • QUERY_STRING. Some optional query request parameters, such as? The pretty parameter will make the request return more beautiful and readable JSON data
  • BODY . A request body in JSON format (if required by the request)

For example:

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}'

2. Close the service

curl -XPOST 'http://localhost:9200/_shutdown'  

3. Add employee information

PUT /{index}/{type}/{id}
curl -H "Content-Type: application/json"  -XPUT 'http://localhost:9200/megacorp/employee/3'  -d '
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}'

4. Retrieve individual employee information

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/1?pretty'

5. Retrieve all employee information

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search?pretty'

6. Simple query last_ Employee information with name Smith: full match

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search?q=last_name:Smith&_source=first_name'

7. DSL statement query last_ Employee information with name Smith: full match

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d '{
	"query" : {
		"match" : {
            "last_name" : "Smith"
        }
    }
}'

8. Query last_ Information of employees whose name is Smith and whose age is over 30

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d  '{
	"query" : {
        "bool" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 }
                }
            },
            "must" : {
                "match" : {
                    "last_name" : "Smith"
                }
            }
        }
    }
}'

9. Full text search to search the information of employees who like rock clicking

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d  '{
	"query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}'

10. Phrase search, query employee records that contain both "rock" and "clicking" (and are adjacent)

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d  '{
	"query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}'

11. Highlight keywords in search results

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d  '{
	"query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
	"highlight": {
        "fields" : {
            "about" : {}
        }
    }
}'

12. Aggregate query

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search' -d  '{
	"aggs": {
		"all_interests": {
			"terms": { "field": "interests" }
    }
  }
}'

13. Cluster status description:

green all primary and replica tiles are available
yellow all primary tiles are available, but not all replica tiles are available
Not all major partitions of red are available

14. Determination of split copy

The number of segments is determined according to the actual business
The number of replicas is determined by the number of cluster nodes

15. Create index

curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/blogs' -d '{
   "settings" : {
      "number_of_shards" : 3,
      "number_of_replicas" : 1
   }
}'

16. Increase the number of copies

curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/blogs/_settings' -d '{
   "number_of_replicas" : 4
}'

17. Check whether the document exists

curl -i -H "Content-Type: application/json" -XHEAD http://localhost:9200/megacorp/employee/123

18. Update, create a new document_ The create parameter indicates that if it already exists, it will not be created

curl -i -H "Content-Type: application/json"  -XPUT 'http://localhost:9200/megacorp/employee/3/_create?pretty'  -d '
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}'

19. Delete document

curl -H "Content-Type: application/json" -XDELETE  'http://localhost:9200/megacorp/employee/1?pretty'

20. Version control

curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/website/blog/2/_create?pretty' -d '{
  "title": "safasdfadsfasdf",
  "text":  "ddddddddddddddddddddddddddd"
}'

curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/website/blog/2? Version = 1 '- D' {-- update, which means that the old version takes effect only when it is 1
  "title": "cccccccccccccccccccccc",
  "text":  "ccccccccccccccccccccc"
}'

21. Local update operation

curl -H "Content-Type: application/json" -XPOST  'http://localhost:9200/megacorp/employee/1/_update' -d '{
   "doc" : {
      "first_name" : "weiyuan",
      "views": 0
   }
}'

22. mget Batch Search

curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_mget?pretty' -d '{
   "docs" : [
      {
         "_index" : "megacorp",
         "_type" :  "employee",
         "_id" :    2
      },
      {
         "_index" : "website",
         "_type" :  "blog",
         "_id" :    1,
         "_source": "views"
      }
   ]
}'

23. mget Batch Search. Search under the same index and type

curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/megacorp/employee/_mget?pretty' -d '{  
   "ids" : [ "2", "1","3","234" ]
}'

24. bulk batch addition

curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_bulk/?pretty' -d '{
	{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}
	
	{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
	{ "title":    "My first blog post" }
	
	{ "index":  { "_index": "website", "_type": "blog" }}
	{ "title":    "My second blog post" }
	
	{ "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
	{ "doc" : {"title" : "My updated blog post"} } 
}'


curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_bulk/?pretty' -d '{

	{ "delete":{ "_index":"website", "_type": "blog", "properties" : {"_id": "123" }}}
	
}'


curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_bulk/?pretty' -d '{
{"delete":{"_index":"test_index","_type":"test_type","_id":"2"}}
{"create":{"_index":"test_index","_type":"test_type","_id":"3"}}
{"test_field":"test3"}
{"create":{"_index":"test_index","_type":"test_type","_id":"2"}}
{"test_field":"test2"}
{"index":{"_index":"test_index","_type":"test_type","_id":"4"}}
{"test_field":"test4"}
{"index":{"_index":"test_index","_type":"test_type","_id":"1"}}
{"test_field":"replaced test1111","test_field2":"test_field2"}
{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field2" : "bulk test1"} }
}'


POST /_bulk
'{"delete":{"_index":"test_index","_type":"test_type","_id":"2"}}
{"create":{"_index":"test_index","_type":"test_type","_id":"3"}}
{"test_field":"test3"}
{"create":{"_index":"test_index","_type":"test_type","_id":"2"}}
{"test_field":"test2"}
{"index":{"_index":"test_index","_type":"test_type","_id":"4"}}
{"test_field":"test4"}
{"index":{"_index":"test_index","_type":"test_type","_id":"1"}}
{"test_field":"replaced test1111","test_field2":"test_field2"}
{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field2" : "bulk test1"} }'

Routing: an algorithm that determines which slice the operation is on
shard = hash(routing) % number_of_primary_shards

25. View a certain type of mapping

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/_mapping/employee/?pretty'

26. Specify word breaker

curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_analyze?pretty=true' -d '{  
   "analyzer": "standard", "text": "Text to analyze"
}'

27. Specify the word splitter and test the Chinese word splitter

curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_analyze/?pretty' -d '{  
   "analyzer":"ik_max_word","text":"The People's Republic of China"
}'

28. Empty query, query all data

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_search/?pretty' -d '{
    "query": {
        "match_all": {}
    }
}'

29. Query clause, match

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_search/?pretty' -d '{
    "query": {
        "match" : {
            "last_name" : "Smith"
        }
    }
}'

30. Merge multiple clauses

{
    "bool": {
        "must":     { "match": { "last_name": "Smith" }},
        "must_not": { "match": { "name":  "mary" }},
        "should":   { "match": { "tweet": "full text" }}
    }
}


curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_search/?pretty' -d  '{
	"query" : {
        "bool": {
			"must":     { "match": { "last_name": "Smith" }},
			"must_not": { "match": { "name":  "mary" }},
			"should":   { "match": { "tweet": "full text" }}
		}
    }
}'

31. term filtering

It is mainly used to exactly match which values, such as date, number, Boolean, or not_analyzed string
{ "term": { "age": 26 }}
{ "term": { "date": "2014-09-01" }}
{ "term": { "public": true }}
{ "term": { "tag": "full_text" }}

32. terms filtering

Terms is a bit similar to term, but terms allows you to specify multiple matching criteria. If a field specifies multiple values, the document needs to match together

{
    "terms": {
        "last_name": [ "sam", "tom", "smith" ]
    }
}
GET /my_store/products/_search
{
    "query" : {
        "filtered" : {
            "filter" : {
                "term" : {
                    "productID" : "XHDK-A-1293-#fJ3"
                }
            }
        }
    }
}

33. range filtering

Range filtering allows us to find a batch of data according to a specified range
{
"range": {
"age": {
"gte": 20,
"lt": 30
}
}
}

GET /my_store/products/_search
{
    "query" : {
        "filtered" : {
            "filter" : {
                "range" : {
                    "price" : {
                        "gte" : 20,
                        "lt"  : 40
                    }
                }
            }
        }
    }
}

"range" : {
    "timestamp" : {
        "gt" : "2014-01-01 00:00:00",
        "lt" : "2014-01-07 00:00:00"
    }
}

34. exists and missing filtering

exists and missing filters can be used to find whether a document contains a specified field or does not have a field, similar to is in an SQL statement_ Null condition
{
"exists": {
"field": "title"
}
}

35. bool filtering

bool filtering can be used to merge the Boolean logic of query results of multiple filtering conditions. It includes the following operators:
must:: exact match of multiple query criteria, equivalent to and.
must_not: the opposite matching of multiple query criteria, which is equivalent to not.
should:: at least one query condition matches, which is equivalent to or.
{
"bool": {
"must": { "term": { "folder": "inbox" }},
"must_not": { "term": { "tag": "spam" }},
"should": [
{ "term": { "starred": true }},
{ "term": { "unread": true }}
]
}
}

match_all query: using match_all can query all documents. It is the default statement without query conditions.
{
"match_all": {}
}

36. Single filter statement

GET /_search
{
    "query": {
        "filtered": {
            "filter":   { "term": { "folder": "inbox" }}
        }
    }
}

Filtering in query statements

GET /_search
{
    "query": {
        "filtered": {
            "filter":   {
                "bool": {
                    "must":     { "term":  { "folder": "inbox" }},
                    "must_not": {
                        "query": {
                            "match": { "email": "urgent business proposal" }
                        }
                    }
                }
            }
        }
    }
}

37. Re index data

GET /old_index/_search?search_type=scan&scroll=1m
{
    "query": {
        "range": {
            "date": {
                "gte":  "2014-01-01",
                "lt":   "2014-02-01"
            }
        }
    },
    "size":  1000
}

38. Index alias

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_search/?pretty'

39. Create index

curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/my_index_v1/?pretty'

40. Set index alias

curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/my_index_v1/_alias/my_index/?pretty'

41. Refresh API: refresh index

POST /_refresh <1>
POST /blogs/_refresh <2>

<1> Refresh all indexes
<2> refresh index blogs only

flush API: persistent data to the hard disk and delete the transaction log once (once every 30 minutes by default)
The flush API can be used to manually flush once:

POST /blogs/_flush <1> 
POST /_flush?wait_for_ongoing  <2>

<1> Flush index blogs
<2> Flush all indexes and wait for the operation to end before returning
You rarely need to flush manually, and usually automatic is enough.

When you want to restart or close an index, flush the index is very useful. When ES attempts to restore or reopen an index,
It must replay all operations in the transaction log, so the smaller the log, the faster the recovery.

42. Full text retrieval, match query

GET /my_index/my_type/_search
{
    "query": {
        "match": {
            "title": "QUICK!"
        }
    }
}

43. match multi word search

GET /my_index/my_type/_search
{
    "query": {
        "match": {
            "title": "BROWN DOG!"
        }
    }
}
GET /my_index/my_type/_search
{
    "query": {
        "match": {
            "title": {      
                "query":    "BROWN DOG!",  
                "operator": "and"    -- Control matching degree and requirements brown and dog All exist
            }
        }
    }
}

GET /my_index/my_type/_search
{
  "query": {
    "match": {
      "title": {
        "query": "quick brown dog",
        "minimum_should_match": "75%"  --Set matching accuracy percentage
      }
    }
  }
}

44. Combined query

GET /my_index/my_type/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title": "brown" }},
        { "match": { "title": "fox"   }},
        { "match": { "title": "dog"   }}
      ],
      "minimum_should_match": 76%  --Set matching accuracy percentage
    }
  }
}

45multi_match cross field object query

{
  "query": {
    "multi_match": {
      "query":       "Poland Street W1V",
      "type":        "most_fields",
      "fields":      [ "street", "city", "country", "postcode" ]
    }
  }
}

Tags: ElasticSearch

Posted by mnetsys on Mon, 09 May 2022 17:35:00 +0300