Common operation commands for Elasticsearch

Keywords: ElasticSearch Apache less MongoDB

--------------------------- 
Index creation, update and deletion in single mode
---------------------------

--------------------------- 
Initialize Index
Indexes can be initialized before they are created.
For example, specify the number of shards and the number of replicas

PUT http://localhost:9200/library/
{
"settings":{
"index":{
"number_of_shards": 5,
"number_of_replicas": 1
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

The number_of_replicas above can also be replaced with:
blocks.read_only: set to true, the current index only allows reading, not writing or updating
blocks.read: Set to true to disable read operations
blokcs.write: Set to true to disable write operations
blocks.metadata: set to true to prohibit metadata operations

--------------------------- 
Create an index

PUT /library/books/1
{
"title": "Elasticsearch: The Definitive Guide",
"name": {
"first": "Zachary",
"last": "Tong"
},
"publish_data": "2015-02-06",
"price": "49.99"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

--------------------------- 
ID can choose not to set

POST /library/books/
{
"title": "Elasticsearch: Blueprints",
"name": {
"first": "Vineeth",
"last": "Mohan"
},
"publish_data": "2015-06-06",
"price": "35.99"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

--------------------------- 
Get Document Information by ID
GET /library/books/1 
GET /library/books/2 
GET /library/books/AU_A-EDnU9duEv19TRm8

--------------------------- 
Get the specified field from _source
GET /library/books/1?_source=title 
GET /library/books/1?_source=title,price 
GET /library/books/1?_source

--------------------------- 
Update documents under the same ID, which can be overwritten

PUT /library/books/1
{
"title": "Elasticsearch: The Definitive Guide",
"name": {
"first": "Zachary",
"last": "Tong"
},
"publish_data": "2015-02-06",
"price": "59.99"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

----------------------------- 
Update the fields you want to update individually by using the _update API

POST /library/books/1/_update
{
"doc": {
"price": 10
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

------------------------------ 
How to delete a document
DELETE /library/books/1 
DELETE /library/books 
DELETE /library

------------------------------ 
Get multiple documents at once

GET /_megt{
"docs": [
{
"_index": "shakespeare",
"_type": "line",
"_id": 6,
"_source": "play_name"
},
{
"_index": "shakespeare",
"_type": "line",
"_id": 28,
"_source": "play_name"
}
]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

------------------------------ 
Specify multiple _source fields, in the form of arrays

GET /_mget
{
"docs": [
{
"_index": "shakespeare",
"_type": "line",
"_id": 6
},
{
"_index": "shakespeare",
"_type": "line",
"_id": 28,
"_source": ["play_name", "speaker", "text_entry"]
}
]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

------------------------------- 
Get documents with different ID s under the same index and type

GET /shakespeare/line/_megt
{
"ids": ["6", "28"]
}
  • 1
  • 2
  • 3
  • 4

------------------------------- 
Multiple modes
-------------------------------

------------------------------- 
Simultaneous operation bulk

POST /library/books/_bulk
{"index": {"_id": 1}}
{"title": "Elasticsearch: The Definitive Guide", "price": 5}
{"index": {"_id": 2}}
{"title": "The ElasticSearch cookbook", "price": 12}
  • 1
  • 2
  • 3
  • 4
  • 5

------------------------------- 
You can also delete, update, and so on.
Note that there is no specific request body under delete

POST /library/books/_bulk
{"delete": {"_index": "library", "type": "books", "_id": "1"}}
{"create": {"_index": "music", "_type": "classical", "_id": "1"}}
{"title": "Ave Verum Corpus"}
{"index": {"_index": "music", "_type": "classical"}}
{"title": "Litaniac de Venerabili Altaris Sacromento"}
{"update": {"_index": "library", "_type": "books", "_id": "2"}}
{"doc": {"price": "18"}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

------------------------------- 
Mapping Mapping
Define type:

Define attributes:

------------------------------- 
Mapping

POST /library
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"books": {
"properties": {
"title": {"type": "string"},
"name": {"type": "string", "index": "not_analyzed"},
"publish_date": {"type": "date", "index": "not_analyzed"},
"price": {"type", "double"},
"number": {"type": "integer"}
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

------------------------------- 
Dynamic Mapping

PUT /library
{
"mappings": {
"books": {
"dynamic": "strict",
"properties": {
"title": {"type": "string"},
"name": {"type": "string", "index": "not_analyzed"},
"publish_date": {"type": "date", "index": "not_analyzed"},
"price": {"type", "double"},
"number":
{
"type": "object",
"dynamic": true
}
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

------------------------------- 
Get mapping information for an index

GET /library/_mapping
  • 1

------------------------------- 
Get mapping information for a type under an index

GET /library/_mapping/books
  • 1

------------------------------- 
Get all the mapping information in this cluster

GET /_all/_mapping
  • 1

------------------------------- 
Mapping information for one or more type s within a cluster

GET /_all/_mapping/books,bank_account
  • 1

------------------------------- 
Update Modify Mapping Mapping
Once a mapping is established, existing field mappings cannot be modified.
If you want to push down an existing map, you need to rebuild the index and redefine the map
Then import the data from the previous index into the newly created index.
Specific method:
1. Define an alias for the data in the existing index and point the existing index at the alias.
2. Run: PUT / Existing Index /_alias / Alias A
3. Create a new index and define the latest mappings
4. Point the alias to the new index and deselect the previous index
5. Run:

POST /_aliases
{
"actions": [
{"remove": {"index": "Existing Index Name", "alias": "alias A"}},
{"add": {"index": "New Index Name"," alias": "alias A"}}
]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

------------------------------- 
delete mapping

DELETE /library/books
DELETE /library/books/_mapping
DELETE /library/_mapping/books
  • 1
  • 2
  • 3

------------------------------- 
Basic Query
-------------------------------

------------------------------- 
Simple Query
Specify search for index name and type name

GET /library/books/_search?q=title:elasticsearch
  • 1

Specify index name search without type

GET /llibrary/_search?q=title:MongoDB
  • 1

Search without index or type name

GET /_search?q=title:elasticsearch
  • 1

------------------------------- 
term query: Query documents with a keyword in a field

GET /library/books/_search
{
"query": {
"term": {
"preview": "elasticsearch"
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

terms query: Query documents with multiple keywords in a field
minimum_match: Minimum match set: 1 - Indicates that there is at least one of the two keywords
2- Both keywords exist in the description document

GET /library/books/_search
{
"query": {
"terms": {
"preview": ["elasticsearch", "book"],
"minimum_match": 1
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

-------------------------------- 
Controlling the number of returns from a query
from and size
Equivalent to limit in MySQL
form: From which result to return
size: defines the maximum number of results returned

GET /library/books/_search?q=title:elasticsearch
GET /library/books/_search
{
"from": 1,
"size": 2,
"query": {
"term": {
"title": "elasticsearch"
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

------------------------------- 
Return version number_version

GET /library/books/_search
{
"version": true,
"query": {
"term": {
"perview": "elasticsearch"
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

------------------------------- 
match query
A match query can accept data types such as text, number date, etc.
The difference between match and term is that when a match query is executed, elasticsearch provides the appropriate analyzer for the fields you specify.
term queries do not have an analyzer analysis process

GET /library/books/_search
{
"query": {
"match": {
"preview": "elasticsearch"
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Query by match_all
Query all documents under specified index

GET /library/books/_search
{
"query": {
"match_all": {}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Query by match_phrase
Phrase query, slop defines how many unknown words are separated between keywords

GET /library/books/_search
{
"query": {
"match_phrase": {
"preview": {
"query": "elasticsearch, distributed",
"slop": 1
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

muti_match query
Multiple fields can be specified
Examples include querying documents with Elasticsearch keywords in both the title and preview fields

GET /library/books/_search
{
"query": {
"multi_match": {
"query": "Elasticsearch",
"fields": ["title", "preview"]
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

--------------------------------- 
Specify the field to return
Note that only fields with store yes can be returned

GET /library/books/_search
{
"fields": ["preview", "title"]
"query": {
"match": {
"preview": "elasticsearch"
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

--------------------------------- 
Controlling loaded fields through partial_fields

GET /library/books/_search
{
"partial_fields": {
"partial": {
"include": ["preview"],
"exclude": ["title", "price"]
}
},
"query": {
"match_all": {}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

--------------------------------- 
Sort
Sort results by sort

GET /library/books/_search
{
"query": {
"match_all": {}
},
sort: [
{
"price": {
"order": "desc"
}
}
]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

------------------------------- 
prefix matching query

GET /library/books/_search
{
"query": {
"prefix": {
"title": {
"value": "r"
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

---------------------------- 
range Query: range Query
There are from,to,include_lower,inculde_upper,boost parameters
include_lower: Whether to include the left boundary of the range, defaulting to true
include_upper: Includes the right boundary of the range, defaulting to true

GET /library/books/_search
{
"query": {
"range": {
"publish_date": {
"from": "2015-01-01",
"to": "2015-06-30"
}
}
}
}
GET /library/books/_search
{
"query": {
"range": {
"price": {
"from": "10",
"to": "20",
"include_lower": true,
"include_upper": false
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

------------------------------ 
wildcard Query: Allow you to use wildcards * and?Do not query
Note: This query can affect performance

GET /library/books/_search
{
"query": {
"wildcard": {
"preview": "rab*"
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

------------------------------ 
fuzzy Fuzzy Query
value:Keyword for the query
boost: Sets the weight of the query, defaulting to 1.0
min_similarity: Set the minimum similarity of matches
The default value is 0.5; 0-1 for strings; greater than 1 for values; 1d,2d,1m for date types, where 1D represents a day
prefix_length: Indicates the common prefix length of the distinguished item, defaulting to 0
max_expansions: Indicates the number of extensible terms in the query, which can be infinite by default

GET /library/books/_search
{
"query": {
"fuzzy": {
"preview": {
"value": "rabit",
"min_similarity": 0.5
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

fuzzy_like_this Query
Query to get all documents similar to the given content
fileds: Field group, default is _all
like_text: Set keywords
ignore_tf: Sets the frequency of omitted terms, defaulting to false
max_query_terns: Indicates the maximum number of query terms in the generated query, defaulting to 25
min_similarity: Indicates the smallest degree of similarity to distinguish terms, defaulting to 0.5
prefix_length: Indicates the length that distinguishes the common prefix of the term, defaulting to 0
boost: Set weight, default is 1.0
Analyze:Indicates the analyzer used to analyze a given content

GET /library/books/_search
{
"query": {
"fuzzy_like_this": {
"fields": ["preview"],
"like_text": "open source software",
"min_similarity": 0.5,
"prefix_length": 0.2
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

fuzzy_like_this_field query
Only works in one field
Other functions like fuzzy_like_this

GET /library/books/_search
{
"query": {
"fuzzy_like_this_field": {
"preview": {
"like_text": "open source software",
"min_similarity": 0.5,
"prefix_length": 0.2
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

more_like_this query
fields: Define field group, default is _all
like_text: Defines the keyword to be queried
precent_terms_to_match: This parameter specifies the proportion of terms a document must match to be considered similar.The default value is 0.3, which means 30% of the total.
min_term_freq: This parameter indicates the maximum number of query terms in the generated query, with a default value of 25
stop_words: This parameter indicates the set of words that will be ignored
min_doc_freq: This parameter indicates how many documents the term should appear in before it is ignored, defaulting to 5
max_doc_freq: This parameter indicates the maximum number of terms that can occur to prevent them from being ignored and defaults to infinite
min_word_len: This parameter indicates the maximum length of a single word. Words above this value are ignored and default is infinite.
max_word_len: The weight value used by this parameter to raise the weight of each word.Default is 1
boost: Indicates to raise the weight of a query.Default 1.0
analyer: Specify the analyzer to use for analysis

GET /library/books/_search
{
"query": {
"more_like_this": {
"fields": ["preview"],
"like_text": "Apache open source",
"min_term_freq": 1,
"min_doc_freq": 1
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

more_like_this_field query
Only works in one field
Other functions like more_like_this

GET /library/books/_search
{
"query": {
"more_like_this_field" {
"preview": {
"like_text": "Apache open source",
"min_trem_freq": 1,
"min_doc_freq": 1
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

------------------------------- 
Filter filter filter query

SELECT document FROM products WHERE price=20
  • 1

filtered queries for items with a price of 20

GET /store/products/_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"term": {
"price": 20
}
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Multiple values can also be specified

GET /store/products/_search
{
"query": {
"filtered": {
"filter": {
"terms": {
"price": [10, 20]
}
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

----------------------------- 
bool filter query, can do combination filter query

SELECT product FROM products WHERE (price = 20 OR productID = "SD1002136") AND (price != 30)
  • 1

Similarly, elasticsearch has query methods for combined conditions such as and, or, not

{
"bool": {
"must": [],
"should": []'
"must_not": []
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Must: condition must be satisfied, equivalent to AND
should: condition can be satisfied or not, equivalent to OR
must_not: condition does not need to be met, equivalent to NOT

GET /store/products/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{"term": {"price": 20}},
{"term": {"productID": "SD1002136"}}
],
"must_not": {
"term": {"price": 30}
}
}
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

nested queries

SELECT document FORM products WHERE productID="SD1002136" OR (productID="SD4535233" AND price=30)
  • 1
GET /store/products/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{"term": {"productID": "SD1002136"}},
{"bool": {
"must": [
{"term": {"productID": "SD4535233"}},
{"term": {"price": 30}}
]
}}
]
}
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

Another and, or, not query
No bool, just use and, or, not
Query price is both $10 and productID is the result of SD1002136

GET /store/products/_search
{
"query": {
"filtered": {
"filter": {
"and":[
{
"term": {
"productID": "SD1002136"
}
}
]
},
"query": {
"match_all": {}
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

range Range Filtering

SELECT document FROM products WHRE price BETWEEN 20 AND 40
  • 1

Gt: > greater than
Lt: <less than
Gte: >=greater than or equal to
Lte: <=less than or equal to

GET /store/products/_search
{
"query": {
"filtered": {
"filter": {
"range": {
"price": {
"gt": 20,
"lt": 40
}
}
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

-------------------------------- 
Methods for handling null null values

SELECT tags FROM test WHERE tags IS NOT NULL
SELECT tags FROM test WHERE tags IS NULL
  • 1
  • 2
GET /test_index/test/_search
{
"query": {
"filtered": {
"filter": {
"exists": {field: "tags"}
}
}
}
}
GET /test_index/test/_search
{
"query": {
"filtered": {
"filter": {
"missing": {"field": "tags"}
}
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

-------------------------------- 
Cross-index Query

GET /_search
{
"query": {
"indices": {
"indices": [library],
"query": {
"term": {
"title": "elasticsearch"
}
},
"no_match_query": {
"term": {
"price": 10
}
}
}
}
} 

Posted by NSW42 on Sat, 11 May 2019 20:28:43 -0700