ES關於文件的基本操作
阿新 • • 發佈:2021-01-20
技術標籤:Elasticsearcheselasticsearch搜尋引擎solrfilter
關於文件的基本操作
基本操作
新增資料
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