ElasticSearch 6.x 學習筆記:7.文件
阿新 • • 發佈:2019-01-31
7.1 新建文件
index/type/id
- 1
(1)一般格式
PUT blog/csdn/1
{
"id":1,
"title":"Elasticsearch簡介",
"author":"chengyuqiang",
"content":"Elasticsearch是一個基於Lucene的搜尋引擎"
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
{
"_index": "blog",
"_type": "csdn",
"_id": "1",
"_version": 1,
"result ": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0},
"created": true}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
名稱 | 引數 | 說明 |
---|---|---|
blog | _index | 索引名 |
csdn | _type | 型別名 |
1 | _id | 文件ID |
1 | _version | 版本號 |
繼續新增一條資料
POST blog/csdn/2
{
"id":2,
"title":"Git簡介",
"author":"chengyuqiang" ,
"content":"Git是一個版本控制軟體"
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
{
"_index": "blog",
"_type": "csdn",
"_id": "2",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0},
"created": true}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
(2)未指定文件ID
POST blog/csdn
{
"id":3,
"title":"Java程式設計",
"author" :"chengyuqiang",
"content":"Java面向物件程式設計"
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
{
"_index": "blog",
"_type": "csdn",
"_id": "wkv472AB5R2olyYk97rN",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0},
"_seq_no": 0,
"_primary_term": 1}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
7.2 獲取文件
GET blog/csdn/1
- 1
{
"_index": "blog",
"_type": "csdn",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"id": 1,
"title": "Elasticsearch簡介",
"author": "chengyuqiang",
"content": "Elasticsearch是一個基於Lucene的搜尋引擎"}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
GET blog/csdn/100
- 1
{
"_index": "blog",
"_type": "csdn",
"_id": "100",
"found": false}
- 1
- 2
- 3
- 4
- 5
- 6
HEAD blog/csdn/1
- 1
200 - OK
- 1
HEAD blog/csdn/100
- 1
404 - Not Found
- 1
GET blog/csdn/_mget
{
"ids":["1","2"]
}
- 1
- 2
- 3
- 4
{
"docs": [
{
"_index": "blog",
"_type": "csdn",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"id": 1,
"title": "Elasticsearch簡介",
"author": "chengyuqiang",
"content": "Elasticsearch是一個基於Lucene的搜尋引擎"}
},
{
"_index": "blog",
"_type": "csdn",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"id": 2,
"title": "Git簡介",
"author": "chengyuqiang",
"content": "Git是一個版本控制軟體"}
}
]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
7.3 文件搜尋
這裡介紹一下簡單的文件檢索操作,後面章節會詳細介紹。
(1)檢索全部文件
GET blog/_search
- 1
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "blog",
"_type": "csdn",
"_id": "2",
"_score": 1,
"_source": {
"id": 2,
"title": "Git簡介",
"author": "chengyuqiang",
"content": "Git是一個版本控制軟體"}
},
{
"_index": "blog",
"_type": "csdn",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"title": "Elasticsearch簡介",
"author": "chengyuqiang",
"content": "Elasticsearch是一個基於Lucene的搜尋引擎"}
},
{
"_index": "blog",
"_type": "csdn",
"_id": "wkv472AB5R2olyYk97rN",
"_score": 1,
"_source": {
"id": 3,
"title": "Java程式設計",
"author": "chengyuqiang",
"content": "Java面向物件程式設計"}
}
]
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
(2)term查詢
term查詢用於查詢指定欄位中包含指定分詞的檔案,只有當查詢分詞和文件中的分詞精確匹配時才被檢索到。
GET blog/_search
{
"query": {
"term": {
"title": "程"
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
由於未使用IK中文分詞,每個漢字被看做獨立的一個詞。
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "blog",
"_type": "csdn",
"_id": "wkv472AB5R2olyYk97rN",
"_score": 0.2876821,
"_source": {
"id": 3,
"title": "Java程式設計",
"author": "chengyuqiang",
"content": "Java面向物件程式設計"}
}
]
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
當查詢”程式”時,title欄位中找不到這樣的分詞,預設漢字被分為單字詞。
GET blog/_search
{
"query": {
"term": {
"title": "程式"
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
(3)terms查詢
查詢文件彙總包含多個詞的文件
GET blog/_search
{
"query": {
"terms": {
"title": ["java","git"]
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
注意,經過分詞後英文單詞變成了小寫,比如”Java”詞項變成了”java”
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "blog",
"_type": "csdn",
"_id":