Basic operations on documents
Basic operation
Add data
PUT /quanzhan/user/1
{
"name": "xzM"."age": 18."desc": "西西"."tags": ["Geek"."Warm"]}Copy the code
To get the data
GET quanzhan/user/1
Copy the code
Update the data
PUT /quanzhan/user/1
{
"name": "xzM"."age": 18."desc": "East east"."tags": ["Geek"."Warm"]}Copy the code
This is recommended for post_update
POST quanzhan/user/1/_update
{
"doc": {
"name": "Full Stack Self-learning Community"}}Copy the code
A complex operation
An exact (match) query
GET quanzhan/user/_search? Q =name: full stack self-learning communityCopy the code
feedback
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 19."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 1."relation" : "eq"
},
"max_score" : 6.033172./** If there are multiple query results **/
"hits": [{"_index" : "quanzhan"."_type" : "user"."_id" : "1"."_score" : 6.033172."_source" : {
"name" : "Full Stack Self-learning Community"."age" : 18."desc" : "西西"."tags" : [
"Geek"."Warm"]}}]}}Copy the code
Complex search operation SELECT(sort, paging, Highlighting, Fuzzy query, precision query)
GET quanzhan/user/_search
{
/** Query parameter body **/
"query": {
"match": {
"name": "Full"}}}Copy the code
feedback
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 1."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 1."relation" : "eq"
},
"max_score" : 0.7389809."hits": [{"_index" : "quanzhan"."_type" : "user"."_id" : "1"."_score" : 0.7389809."_source" : {
"name" : "Full Stack Self-learning Community"."age" : 18."desc" : "西西"."tags" : [
"Geek"."Warm"]}}]}}Copy the code
Multiple data add tests
GET quanzhan/user/_search
{
"query": {
"match": {
"name": "The king"}}}Copy the code
feedback
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 0."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 2."relation" : "eq"
},
"max_score" : 0.7721133."hits": [{"_index" : "quanzhan"."_type" : "user"."_id" : "5"."_score" : 0.7721133."_source" : {
"name" : "Six"."age" : 18."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "6"."_score" : 0.7721133."_source" : {
"name" : "Fifty"."age" : 18."desc" : "Handsome"."tags" : [
"Handsome"]}}]}}Copy the code
Hit: The index and document information, the total number of results, and then the specific documents that were queried
We can see who is more consistent with the results
Specifying query Results
GET quanzhan/user/_search
{
"query": {
"match": {
"name": "The king"}},"_source": ["name"."desc"] /** Filter results **/
}
Copy the code
Feedback the result
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 1."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 2."relation" : "eq"
},
"max_score" : 0.7721133."hits": [{"_index" : "quanzhan"."_type" : "user"."_id" : "5"."_score" : 0.7721133."_source" : {
"name" : "Six"."desc" : "Handsome"}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "6"."_score" : 0.7721133."_source" : {
"name" : "Fifty"."desc" : "Handsome"}}}Copy the code
The sorting
GET quanzhan/user/_search
{
"query": {
"match": {
"name": "The king"}},"_source": ["name"."desc"]}Copy the code
Feedback the result
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 1."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 2."relation" : "eq"
},
"max_score" : null."hits": [{"_index" : "quanzhan"."_type" : "user"."_id" : "6"."_score" : null./** the value of the query is not **/
"_source" : {
"name" : "Fifty"."age" : 20."desc" : "Handsome"."tags" : [
"Handsome"]},"sort" : [
20] {},"_index" : "quanzhan"."_type" : "user"."_id" : "5"."_score" : null."_source" : {
"name" : "Six"."age" : 18."desc" : "Handsome"."tags" : [
"Handsome"]},"sort" : [
18[}]}Copy the code
Paging query
Page parameter from: the number of items to start from Size: the number of items to return (single-page data)
GET quanzhan/user/_search
{
"query": {
"match": {
"name": "The king"}},"sort": [{"age": {
"order": "desc"}}]."from": 0."size": 1
}
Copy the code
Feedback the result
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 5."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 7."relation" : "eq"
},
"max_score" : null."hits": [{"_index" : "quanzhan"."_type" : "user"."_id" : "11"."_score" : null."_source" : {
"name" : "Wang Lao Ji"."age" : 30."desc" : "Handsome"."tags" : [
"Handsome"]},"sort" : [
30[}]}Copy the code
The data index starts at 0, which is the same data structure we learned
/search/{current}/{pagesize}
Boolean query
Bool Multi-condition query
must
Must command (and in mysql), all conditions must be met. Where id = 1 and name = XXX
GET quanzhan/user/_search
{
"query": {
"bool": {
"must": [{"match": {
"name": "The king"}}, {"match": {
"age": "12"}}}Copy the code
Feedback the result
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 10."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 1."relation" : "eq"
},
"max_score" : 1.3930416."hits": [{"_index" : "quanzhan"."_type" : "user"."_id" : "9"."_score" : 1.3930416."_source" : {
"name" : "Wang Lao Liu"."age" : 12."desc" : "Handsome"."tags" : [
"Handsome"]}}]}}Copy the code
should
should
(or) :
GET quanzhan/user/_search
{
"query": {
"bool": {
"should": [{"match": {
"name": "The king"}}, {"match": {
"age": "12"}}}Copy the code
Feedback the result
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 1."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 7."relation" : "eq"
},
"max_score" : 1.3930416."hits": [{"_index" : "quanzhan"."_type" : "user"."_id" : "9"."_score" : 1.3930416."_source" : {
"name" : "Wang Lao Liu"."age" : 12."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "5"."_score" : 0.45239353."_source" : {
"name" : "Six"."age" : 18."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "6"."_score" : 0.45239353."_source" : {
"name" : "Fifty"."age" : 20."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "Seven"."_score" : 0.3930416."_source" : {
"name" : "Fifty and aaaa"."age" : 20."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "10"."_score" : 0.3930416."_source" : {
"name" : "Wang Lao Ji"."age" : 21."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "11"."_score" : 0.34745687."_source" : {
"name" : "Wang Lao Ji"."age" : 30."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "8"."_score" : 0.311347."_source" : {
"name" : "Ha ha ha ha."."age" : 20."desc" : "Handsome"."tags" : [
"Handsome"]}}]}}Copy the code
not
GET quanzhan/user/_search
{
"query": {
"bool": {
"must_not": [{"match": {
"age": "12"}}}Copy the code
Feedback the result
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 3."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 9."relation" : "eq"
},
"max_score" : 0.0."hits": [{"_index" : "quanzhan"."_type" : "user"."_id" : "2"."_score" : 0.0."_source" : {
"name" : "Zhang"."age" : 18."desc" : "Fanatics outside the law."."tags" : [
"Deceiving a man who plays with a woman's affections."]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "3"."_score" : 0.0."_source" : {
"name" : "Bill"."age" : 18."desc" : "Pretty girl"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "1"."_score" : 0.0."_source" : {
"name" : "Full Stack Self-learning Community"."age" : 18."desc" : "西西"."tags" : [
"Geek"."Warm"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "5"."_score" : 0.0."_source" : {
"name" : "Six"."age" : 18."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "6"."_score" : 0.0."_source" : {
"name" : "Fifty"."age" : 20."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "Seven"."_score" : 0.0."_source" : {
"name" : "Fifty and aaaa"."age" : 20."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "8"."_score" : 0.0."_source" : {
"name" : "Ha ha ha ha."."age" : 20."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "10"."_score" : 0.0."_source" : {
"name" : "Wang Lao Ji"."age" : 21."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "11"."_score" : 0.0."_source" : {
"name" : "Wang Lao Ji"."age" : 30."desc" : "Handsome"."tags" : [
"Handsome"]}}]}}Copy the code
The filter
Filter You can use it to filter data
- Ge is greater than the
- Gte is greater than or equal to
- Lt is less than
- Lte is less than or equal to
GET quanzhan/user/_search
{
"query": {
"bool": {
"must": [{"match": {
"name": "The king"}}]."filter": {
"range": {
"age": {
"gte": 10."lte": 20
}
}
}
}
}
}
Copy the code
Feedback the result
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 63."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 5."relation" : "eq"
},
"max_score" : 0.45239353."hits": [{"_index" : "quanzhan"."_type" : "user"."_id" : "5"."_score" : 0.45239353."_source" : {
"name" : "Six"."age" : 18."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "6"."_score" : 0.45239353."_source" : {
"name" : "Fifty"."age" : 20."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "Seven"."_score" : 0.3930416."_source" : {
"name" : "Fifty and aaaa"."age" : 20."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "9"."_score" : 0.3930416."_source" : {
"name" : "Wang Lao Liu"."age" : 12."desc" : "Handsome"."tags" : [
"Handsome"]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "8"."_score" : 0.311347."_source" : {
"name" : "Ha ha ha ha."."age" : 20."desc" : "Handsome"."tags" : [
"Handsome"]}}]}}Copy the code
Matching multiple conditions
GET quanzhan/user/_search
{
"query": {
"match": {
"tags": "Art of male" /** Multiple criteria are separated by Spaces, so only one of them can be found **/}}}Copy the code
Feedback the result
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 1."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 2."relation" : "eq"
},
"max_score" : 2.1047382."hits": [{"_index" : "quanzhan"."_type" : "user"."_id" : "2"."_score" : 2.1047382."_source" : {
"name" : "Zhang"."age" : 18."desc" : "Fanatics outside the law."."tags" : [
"Deceiving a man who plays with a woman's affections."]}}, {"_index" : "quanzhan"."_type" : "user"."_id" : "1"."_score" : 1.3460207."_source" : {
"name" : "Full Stack Self-learning Community"."age" : 18."desc" : "西西"."tags" : [
"Geek"."Warm"]}}]}}Copy the code
Precise query
A term query is a direct lookup of the term specified by the inverted index
About participles
term
Direct query accuratematch
Will use a word segmentation parser (analyze the document first and then query through the analysis of the document)
Two types are text keyword
Create indexes
PUT testdb
{
"mappings": {
"properties": {
"name": {"type":"text"
},
"desc": {
"type": "keyword"}}}}Copy the code
Feedback the result
{
"acknowledged" : true."shards_acknowledged" : true."index" : "testdb"
}
Copy the code
Insert data
PUT testdb/_doc/1
{
"name": "Full Stack Self-learning Community"."desc": "Wechat Official Account"
}
PUT testdb/_doc/2
{
"name": "Full Stack Self-learning Community"."desc": "Wechat Official Account 2"
}
Copy the code
Feedback the result
{
"_index" : "testdb"."_type" : "_doc"."_id" : "1"."_version" : 1."result" : "created"."_shards" : {
"total" : 2."successful" : 1."failed" : 0
},
"_seq_no" : 0."_primary_term" : 1
}
Copy the code
The query
GET _analyze
{
"analyzer": "keyword"."text": "Full Stack Self-learning Community"
}
/** Can see the split **/
GET _analyze
{
"analyzer": "standard"."text": "Full Stack Self-learning Community"
}
Copy the code
Feedback the result
{
"tokens": [{"token" : "Full Stack Self-learning Community"."start_offset" : 0."end_offset" : 6."type" : "word"."position" : 0} {}]"tokens": [{"token" : "Full"."start_offset" : 0."end_offset" : 1."type" : "<IDEOGRAPHIC>"."position" : 0
},
{
"token" : "Stack"."start_offset" : 1."end_offset" : 2."type" : "<IDEOGRAPHIC>"."position" : 1
},
{
"token" : "Since"."start_offset" : 2."end_offset" : 3."type" : "<IDEOGRAPHIC>"."position" : 2
},
{
"token" : "To learn"."start_offset" : 3."end_offset" : 4."type" : "<IDEOGRAPHIC>"."position" : 3
},
{
"token" : "Social"."start_offset" : 4."end_offset" : 5."type" : "<IDEOGRAPHIC>"."position" : 4
},
{
"token" : "Area"."start_offset" : 5."end_offset" : 6."type" : "<IDEOGRAPHIC>"."position" : 5}}]Copy the code
The query
GET testdb/_search
{
"query": {
"term": {
"name":"Full"}}}Copy the code
Feedback the result
{
"took" : 2."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 2."relation" : "eq"
},
"max_score" : 0.18232156."hits": [{"_index" : "testdb"."_type" : "_doc"."_id" : "1"."_score" : 0.18232156."_source" : {
"name" : "Full Stack Self-learning Community"."desc" : "Wechat Official Account"}}, {"_index" : "testdb"."_type" : "_doc"."_id" : "2"."_score" : 0.18232156."_source" : {
"name" : "Full Stack Self-learning Community"."desc" : "Wechat Official Account 2"}}}Copy the code
GET testdb/_search
{
"query": {
"term": {
"desc": "Wechat Official Account"}}}Copy the code
Feedback the result
{
"took" : 0."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 1."relation" : "eq"
},
"max_score" : 0.6931471."hits": [{"_index" : "testdb"."_type" : "_doc"."_id" : "1"."_score" : 0.6931471."_source" : {
"name" : "Full Stack Self-learning Community"."desc" : "Wechat Official Account"}}}Copy the code
Keyword fields are not parsed by the participle
A precise query where multiple values match
GET testdb/_search
{
"query": {
"bool": {
"should": [{"term": {
"t1": "22"}}, {"term": {
"t1": "23"}}}Copy the code
Feedback the result
{
"took" : 2."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 2."relation" : "eq"
},
"max_score" : 0.6931471."hits": [{"_index" : "testdb"."_type" : "_doc"."_id" : "3"."_score" : 0.6931471."_source" : {
"t1" : "22"."t2" : "2021-1-19"}}, {"_index" : "testdb"."_type" : "_doc"."_id" : "4"."_score" : 0.6931471."_source" : {
"t1" : "23"."t2" : "2021-1-3"}}}Copy the code
Highlighting the query
GET testdb/_search
{
"query": {
"match": {
"name": "Full Stack Self-learning Community"}},"highlight": {
"fields": {
"name": {}}}}Copy the code
Feedback the result
{
"took" : 199."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 2."relation" : "eq"
},
"max_score" : 1.0939294."hits": [{"_index" : "testdb"."_type" : "_doc"."_id" : "1"."_score" : 1.0939294."_source" : {
"name" : "Full Stack Self-learning Community"."desc" : "Wechat Official Account"
},
"highlight" : {
"name" : [
All "< em > < / em > < em > stack < / em > < em > from < / em > < em > to learn < / em > < / em > < em > club area < em > < / em >"]}}, {"_index" : "testdb"."_type" : "_doc"."_id" : "2"."_score" : 1.0939294."_source" : {
"name" : "Full Stack Self-learning Community"."desc" : "Wechat Official Account 2"
},
"highlight" : {
"name" : [
All "< em > < / em > < em > stack < / em > < em > from < / em > < em > to learn < / em > < / em > < em > club area < em > < / em >"]}}]}}Copy the code
Definition tags
GET testdb/_search
{
"query": {
"match": {
"name": "Full Stack Self-learning Community"}},"highlight": {
"pre_tags": "<p class='key' style='color:red>"."post_tags": "</p>"."fields": {
"name": {}}}}Copy the code
Feedback the result
{
"took" : 2."timed_out" : false."_shards" : {
"total" : 1."successful" : 1."skipped" : 0."failed" : 0
},
"hits" : {
"total" : {
"value" : 2."relation" : "eq"
},
"max_score" : 1.0939294."hits": [{"_index" : "testdb"."_type" : "_doc"."_id" : "1"."_score" : 1.0939294."_source" : {
"name" : "Full Stack Self-learning Community"."desc" : "Wechat Official Account"
},
"highlight" : {
"name" : [
"< p class = 'key' style =" color: red > < / p > < p class = "key" style = "color: red stack > < / p > < p class =" key "style =" color: red > from < / p > < p Class = 'key' style = "color: red > to learn < / p > < p class =" key "style =" color: red < / p > < p > club class = 'key' style = "color: red area > < / p >"]}}, {"_index" : "testdb"."_type" : "_doc"."_id" : "2"."_score" : 1.0939294."_source" : {
"name" : "Full Stack Self-learning Community"."desc" : "Wechat Official Account 2"
},
"highlight" : {
"name" : [
"< p class = 'key' style =" color: red > < / p > < p class = "key" style = "color: red stack > < / p > < p class =" key "style =" color: red > from < / p > < p Class = 'key' style = "color: red > to learn < / p > < p class =" key "style =" color: red < / p > < p > club class = 'key' style = "color: red area > < / p >"]}}]}}Copy the code
conclusion
MySql can also do this, but it’s not very efficient
- matching
- Match by condition
- An exact match
- Interval range matching
- Filtering of Matched fields
- Multi-condition Query
- Highlighting the query
This article has been uploaded to gitee gitee.com/codingce/he… Project address: github.com/xzMhehe/cod…