elasticsearch(20) 資料建模
1、檔案系統資料構造
PUT /fs { "settings": { "analysis": { "analyzer": { "paths": { "tokenizer": "path_hierarchy" } } } } }
path_hierarchy tokenizer講解
/a/b/c/d --> path_hierarchy -> /a/b/c/d, /a/b/c, /a/b, /a
fs: filesystem
PUT /fs/_mapping/file { "properties": { "name": { "type": "keyword" }, "path": { "type": "keyword", "fields": { "tree": { "type": "text", "analyzer": "paths" } } } } }
PUT /fs/file/1 { "name": "README.txt", "path": "/workspace/projects/helloworld", "contents": "這是我的第一個elasticsearch程式" }
2、對檔案系統執行搜尋
檔案搜尋需求:查詢一份,內容包括elasticsearch,在/workspace/projects/hellworld這個目錄下的檔案
GET /fs/file/_search { "query": { "bool": { "must": [ { "match": { "contents": "elasticsearch" } }, { "constant_score": { "filter": { "term": { "path": "/workspace/projects/helloworld" } } } } ] } } }
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 1.284885, "hits": [ { "_index": "fs", "_type": "file", "_id": "1", "_score": 1.284885, "_source": { "name": "README.txt", "path": "/workspace/projects/helloworld", "contents": "這是我的第一個elasticsearch程式" } } ] } }
搜尋需求2:搜尋/workspace目錄下,內容包含elasticsearch的所有的檔案
/workspace/projects/helloworld doc1 /workspace/projects doc1 /workspace doc1
GET /fs/file/_search { "query": { "bool": { "must": [ { "match": { "contents": "elasticsearch" } }, { "constant_score": { "filter": { "term": { "path.tree": "/workspace" } } } } ] } } }