1. IK tokenizer test
GET _analyze
{
"analyzer": "ik_smart",
"text": "我是程序员"
}
return result
{
"tokens" : [
{
"token" : "我",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "是",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "程序员",
"start_offset" : 2,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 2
}
]
}
2. DSL query
2.1 match query
get csdn/_search
{
"query":{
"match": {
# "name" 查询的字段 "赵牛修改1" 分词后匹配的词
"name": "赵牛修改1"
}
}
}
return result
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.6407243,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 0.6407243,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 11
}
}
]
}
}
2.1.1 term query
get csdn/_search
{
"query":{
"term": {
"name": "赵牛修改1"
}
}
}
return result
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
The query result is 0. Why can the same query conditions be found with match, but similar with term?
2.1.1.1 The difference between match and term
The match query will analyze the query conditions, first segment the query conditions, then query, and find the set, similar to like
The term query will not segment the query conditions, similar to =
2.1.2 Terms query
get csdn/_search
{
"query":{
"terms": {
"age": [
11,
18
]
}
}
}
return result
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
The terms query can set multiple query values for a field. Similar to in MySQL
2.1.3 match_all query
post csdn/_search
{
"query":{
"match_all": {}
}
}
return result
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
Querying all documents is the same as GET /indexname/_search
2.1.4 wildcard query
get csdn/_search
{
"query":{
"wildcard": {
"name": "*修改"
}
}
}
return result
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
Allows the use of wildcards * and ? for query (note the matching of terms) *: indicates 0 or more characters?: indicates any character
2.1.5 PrefixQuery query
get csdn/_search
{
"query":{
"prefix": {
"name": "修"
}
}
}
return result
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
get csdn/_search
{
"query":{
"prefix": {
"name": "改"
}
}
}
return result
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
Perform word segmentation field test on name
GET _analyze
{
"analyzer": "ik_smart",
"text": "赵牛修改"
}
return result
{
"tokens" : [
{
"token" : "赵",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "牛",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "修改",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 2
}
]
}
Conclusions can be drawn from the above three-level query results
Note that the prefix query is the prefix query of the entry after the word segmentation. If you use "repair" to match the result, but use "change", you can't find any results
2.2 range (range query)
get csdn/_search
{
"query":{
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
return result
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
get greater than or equal to gt greater than lte less than or equal to lt less than
2.3 bool (combined query)
get csdn/_search
{
"query":{
"bool": {
"filter": [
{
"range": {
"age": {
"gte": 1,
"lte": 100
}
}
}
]
, "must": [
{
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
]
,
"should": [
{
"match_all": {}
}
]
,
"must_not": [
{
"range": {
"id": {
"gte": 30,
"lte": 40
}
}
}
]
}
}
}
return result
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 2.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 2.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
}
}
Result analysis
- must The term (query) must appear on matching documents and will contribute to the score
- The should clause (query) should appear in the matching document, to calculate the score
- The filter clause (query) must appear in matching documents, no score is calculated
- must_not clause (query) cannot appear in matching documents, no score is calculated
2.4 aggs (aggregation query)
get csdn/_search
{
"aggs": {
#group_by_age 分组后结果集合名
"group_by_age": {
"terms":{
#根据 id 字段分组
"field":"age"
}
}
}
}
return result
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
},
"aggregations" : {
"group_by_age" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 11,
"doc_count" : 1
},
{
"key" : 18,
"doc_count" : 1
}
]
}
}
}
get csdn/_search
{
"aggs": {
"sum_age": {
#求age的和
"sum":{
"field":"age"
}
}
}
}
return result
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 21,
"name" : "王五",
"age" : 11
}
},
{
"_index" : "csdn",
"_type" : "_doc",
"_id" : "gPc9On8BcPj1STPlFokb",
"_score" : 1.0,
"_source" : {
"id" : 20,
"name" : "赵牛修改",
"age" : 18
}
}
]
},
"aggregations" : {
"sum_age" : {
"value" : 29.0
}
}
}
Aggregate query
1. sum:求总数值,等同sum()
2. min:求最小值,等同min()
3. max:求最大值,等同max()
4. avg:求平均值,等同avg()
5. cardinality:求基数,等同count()
6. terms:分组,等同group by