关于文档的基本操作
基本操作
添加数据
PUT /quanzhan/user/1
{
"name": "xzM",
"age": 18,
"desc": "西西",
"tags": ["技术宅", "温暖"]
}
获取数据
GET quanzhan/user/1
更新数据
PUT /quanzhan/user/1
{
"name": "xzM",
"age": 18,
"desc": "东东",
"tags": ["技术宅", "温暖"]
}
Post _update 推荐使用这种
POST quanzhan/user/1/_update
{
"doc": {
"name": "全栈自学社区"
}
}
复杂操作
精确(匹配)查询
GET quanzhan/user/_search?q=name:全栈自学社区
反馈
#! 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, /** 未来如果存在多条查询出来的结果 **/
"hits" : [
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "1",
"_score" : 6.033172,
"_source" : {
"name" : "全栈自学社区",
"age" : 18,
"desc" : "西西",
"tags" : [
"技术宅",
"温暖"
]
}
}
]
}
}
复杂搜索操作 SELECT(排序, 分页, 高亮,模糊查询,精准查询)
GET quanzhan/user/_search
{
/** 查询的参数体 **/
"query": {
"match": {
"name": "全"
}
}
}
反馈
#! 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" : "全栈自学社区",
"age" : 18,
"desc" : "西西",
"tags" : [
"技术宅",
"温暖"
]
}
}
]
}
}
多条数据添加测试
GET quanzhan/user/_search
{
"query": {
"match": {
"name": "王"
}
}
}
反馈
#! 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" : "王六",
"age" : 18,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "6",
"_score" : 0.7721133,
"_source" : {
"name" : "王五",
"age" : 18,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
}
]
}
}
hit: 索引和文档信息, 查询结果总数, 然后就是查询出来的具体文档
_score 分数 我们可以通过来判断谁更加符合结果
指定查询结果
GET quanzhan/user/_search
{
"query": {
"match": {
"name": "王"
}
},
"_source": ["name", "desc"] /** 结果过滤 **/
}
反馈结果
#! 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" : "王六",
"desc" : "靓仔"
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "6",
"_score" : 0.7721133,
"_source" : {
"name" : "王五",
"desc" : "靓仔"
}
}
]
}
}
排序
GET quanzhan/user/_search
{
"query": {
"match": {
"name": "王"
}
},
"_source": ["name", "desc"]
}
反馈结果
#! 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, /** 由于排序查询, 所以 分值没了 **/
"_source" : {
"name" : "王五",
"age" : 20,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
},
"sort" : [
20
]
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "5",
"_score" : null,
"_source" : {
"name" : "王六",
"age" : 18,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
},
"sort" : [
18
]
}
]
}
}
分页查询
分页参数 from
:从第几条开始 size
:返回多少条数据(单页面数据)
GET quanzhan/user/_search
{
"query": {
"match": {
"name": "王"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 0,
"size": 1
}
反馈结果
#! 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" : "王老吉呀",
"age" : 30,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
},
"sort" : [
30
]
}
]
}
}
数据下标是在 0 开始的, 和所学的数据结构是一样的
/search/{current}/{pagesize}
布尔值查询
bool
多条件查询
must
must
命令(mysql 中的 and), 所有条件都要符合. 比如 where id = 1 and name = xxx
GET quanzhan/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "王"
}
},
{
"match": {
"age": "12"
}
}
]
}
}
}
反馈结果
#! 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" : "王老刘",
"age" : 12,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
}
]
}
}
should
should
(or) :
GET quanzhan/user/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "王"
}
},
{
"match": {
"age": "12"
}
}
]
}
}
}
反馈结果
#! 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" : "王老刘",
"age" : 12,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "5",
"_score" : 0.45239353,
"_source" : {
"name" : "王六",
"age" : 18,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "6",
"_score" : 0.45239353,
"_source" : {
"name" : "王五",
"age" : 20,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "7",
"_score" : 0.3930416,
"_source" : {
"name" : "王五aaaa",
"age" : 20,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "10",
"_score" : 0.3930416,
"_source" : {
"name" : "王老吉",
"age" : 21,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "11",
"_score" : 0.34745687,
"_source" : {
"name" : "王老吉呀",
"age" : 30,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "8",
"_score" : 0.311347,
"_source" : {
"name" : "王五哈哈哈",
"age" : 20,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
}
]
}
}
not
GET quanzhan/user/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"age": "12"
}
}
]
}
}
}
反馈结果
#! 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" : "张三",
"age" : 18,
"desc" : "法外狂徒",
"tags" : [
"渣男"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "3",
"_score" : 0.0,
"_source" : {
"name" : "李四",
"age" : 18,
"desc" : "靓女",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "1",
"_score" : 0.0,
"_source" : {
"name" : "全栈自学社区",
"age" : 18,
"desc" : "西西",
"tags" : [
"技术宅",
"温暖"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "5",
"_score" : 0.0,
"_source" : {
"name" : "王六",
"age" : 18,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "6",
"_score" : 0.0,
"_source" : {
"name" : "王五",
"age" : 20,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "7",
"_score" : 0.0,
"_source" : {
"name" : "王五aaaa",
"age" : 20,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "8",
"_score" : 0.0,
"_source" : {
"name" : "王五哈哈哈",
"age" : 20,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "10",
"_score" : 0.0,
"_source" : {
"name" : "王老吉",
"age" : 21,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "11",
"_score" : 0.0,
"_source" : {
"name" : "王老吉呀",
"age" : 30,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
}
]
}
}
过滤器
filter
可以使用它进行数据过滤
- ge 大于
- gte 大于等于
- lt 小于
- lte 小于等于
GET quanzhan/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "王"
}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
}
}
反馈结果
#! 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" : "王六",
"age" : 18,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "6",
"_score" : 0.45239353,
"_source" : {
"name" : "王五",
"age" : 20,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "7",
"_score" : 0.3930416,
"_source" : {
"name" : "王五aaaa",
"age" : 20,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "9",
"_score" : 0.3930416,
"_source" : {
"name" : "王老刘",
"age" : 12,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "8",
"_score" : 0.311347,
"_source" : {
"name" : "王五哈哈哈",
"age" : 20,
"desc" : "靓仔",
"tags" : [
"靓仔"
]
}
}
]
}
}
匹配多个条件
GET quanzhan/user/_search
{
"query": {
"match": {
"tags": "术 男" /** 多个条件使用空格隔开, 只要满足其中一个结果就可以被查出 **/
}
}
}
反馈结果
#! 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" : "张三",
"age" : 18,
"desc" : "法外狂徒",
"tags" : [
"渣男"
]
}
},
{
"_index" : "quanzhan",
"_type" : "user",
"_id" : "1",
"_score" : 1.3460207,
"_source" : {
"name" : "全栈自学社区",
"age" : 18,
"desc" : "西西",
"tags" : [
"技术宅",
"温暖"
]
}
}
]
}
}
精确查询
term
查询是直接通过倒排索引指定的词条进行精确查找
关于分词
term
直接查询精确的match
会使用分词解析器(先分析文档, 然后通过分析文档进行查询)
两个类型 text keyword
创建索引
PUT testdb
{
"mappings": {
"properties": {
"name":{
"type":"text"
},
"desc": {
"type": "keyword"
}
}
}
}
反馈结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "testdb"
}
插入数据
PUT testdb/_doc/1
{
"name": "全栈自学社区",
"desc": "微信公众号"
}
PUT testdb/_doc/2
{
"name": "全栈自学社区",
"desc": "微信公众号2"
}
反馈结果
{
"_index" : "testdb",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
查询
GET _analyze
{
"analyzer": "keyword",
"text": "全栈自学社区"
}
/** 可以看到被拆分 **/
GET _analyze
{
"analyzer": "standard",
"text": "全栈自学社区"
}
反馈结果
{
"tokens" : [
{
"token" : "全栈自学社区",
"start_offset" : 0,
"end_offset" : 6,
"type" : "word",
"position" : 0
}
]
}
{
"tokens" : [
{
"token" : "全",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "栈",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "自",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "学",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "社",
"start_offset" : 4,
"end_offset" : 5,
"type" : "<IDEOGRAPHIC>",
"position" : 4
},
{
"token" : "区",
"start_offset" : 5,
"end_offset" : 6,
"type" : "<IDEOGRAPHIC>",
"position" : 5
}
]
}
查询
GET testdb/_search
{
"query": {
"term": {
"name":"全"
}
}
}
反馈结果
{
"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" : "全栈自学社区",
"desc" : "微信公众号"
}
},
{
"_index" : "testdb",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.18232156,
"_source" : {
"name" : "全栈自学社区",
"desc" : "微信公众号2"
}
}
]
}
}
GET testdb/_search
{
"query": {
"term": {
"desc": "微信公众号"
}
}
}
反馈结果
{
"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" : "全栈自学社区",
"desc" : "微信公众号"
}
}
]
}
}
keyword字段不会被分词器解析
多个值匹配的精确查询
GET testdb/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"t1": "22"
}
},
{
"term": {
"t1": "23"
}
}
]
}
}
}
反馈结果
{
"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"
}
}
]
}
}
高亮查询
GET testdb/_search
{
"query": {
"match": {
"name": "全栈自学社区"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
反馈结果
{
"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" : "全栈自学社区",
"desc" : "微信公众号"
},
"highlight" : {
"name" : [
"<em>全</em><em>栈</em><em>自</em><em>学</em><em>社</em><em>区</em>"
]
}
},
{
"_index" : "testdb",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0939294,
"_source" : {
"name" : "全栈自学社区",
"desc" : "微信公众号2"
},
"highlight" : {
"name" : [
"<em>全</em><em>栈</em><em>自</em><em>学</em><em>社</em><em>区</em>"
]
}
}
]
}
}
定义标签
GET testdb/_search
{
"query": {
"match": {
"name": "全栈自学社区"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red>",
"post_tags": "</p>",
"fields": {
"name": {}
}
}
}
反馈结果
{
"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" : "全栈自学社区",
"desc" : "微信公众号"
},
"highlight" : {
"name" : [
"<p class='key' style='color:red>全</p><p class='key' style='color:red>栈</p><p class='key' style='color:red>自</p><p class='key' style='color:red>学</p><p class='key' style='color:red>社</p><p class='key' style='color:red>区</p>"
]
}
},
{
"_index" : "testdb",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0939294,
"_source" : {
"name" : "全栈自学社区",
"desc" : "微信公众号2"
},
"highlight" : {
"name" : [
"<p class='key' style='color:red>全</p><p class='key' style='color:red>栈</p><p class='key' style='color:red>自</p><p class='key' style='color:red>学</p><p class='key' style='color:red>社</p><p class='key' style='color:red>区</p>"
]
}
}
]
}
}
总结
这些其实 MySql也可以做, 效率低
- 匹配
- 按照条件匹配
- 精确匹配
- 区间范围匹配
- 匹配字段过滤
- 多条件查询
- 高亮查询
文章已上传gitee https://gitee.com/codingce/hexo-blog
项目地址: https://github.com/xzMhehe/codingce-java