Elasticsearch进阶笔记第二十九篇

85 阅读2分钟

Elasticsearch高手进阶篇(62)

数据建模实战_对每个用户发表的博客进行分组

构造更多测试数据

 PUT /waws_webblog/users/3
 {
   "name": "waws520",
   "email": "waws520@sina.com",
   "birthday": "1970-10-24"
 }
 
 PUT /waws_webblog/blogs/3
 {
   "title": "我是waws520",
   "content": "我是waws520啊,各位同学们!!!",
   "userInfo": {
     "userId": 1,
     "username": "waws520"
   }
 }
 
 PUT /waws_webblog/users/2
 {
   "name": "waws521",
   "email": "waws521@sina.com",
   "birthday": "1980-02-02"
 }
 
 PUT /waws_webblog/blogs/4
 {
   "title": "waws521的身世揭秘",
   "content": "大家好,我是waws521",
   "userInfo": {
     "userId": 2,
     "username": "waws521"
   }
 }
 
 PUT /waws_webblog/blogs/5
 {
   "title": "waws的身世揭秘",
   "content": "大家好,我是waws, 哈哈",
   "userInfo": {
     "userId": 1,
     "username": "waws"
   }
 }

对每个用户发表的博客进行分组

比如说,waws发表的那些博客,花无缺发表了哪些博客,黄药师发表了哪些博客

 GET /website/blogs/_search 
 {
   "size": 0, 
   "aggs": {
     "group_by_username": {
       "terms": {
         "field": "userInfo.username.keyword"
       },
       "aggs": {
         "top_blogs": {
           "top_hits": {
             "_source": {
               "include": "title"
             }, 
             "size": 5
           }
         }
       }
     }
   }
 }
 ​
 {
   "took": 24,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 4,
     "max_score": 0,
     "hits": []
   },
   "aggregations": {
     "group_by_username": {
       "doc_count_error_upper_bound": 0,
       "sum_other_doc_count": 0,
       "buckets": [
         {
           "key": "waws",
           "doc_count": 2,
           "top_blogs": {
             "hits": {
               "total": 2,
               "max_score": 1,
               "hits": [
                 {
                   "_index": "waws_webblog",
                   "_type": "blogs",
                   "_id": "5",
                   "_score": 1,
                   "_source": {
                     "title": "waws的身世揭秘"
                   }
                 },
                 {
                   "_index": "waws_webblog",
                   "_type": "blogs",
                   "_id": "1",
                   "_score": 1,
                   "_source": {
                     "title": "waws的第一篇博客"
                   }
                 }
               ]
             }
           }
         },
         {
           "key": "waws520",
           "doc_count": 1,
           "top_blogs": {
             "hits": {
               "total": 1,
               "max_score": 1,
               "hits": [
                 {
                   "_index": "waws_webblog",
                   "_type": "blogs",
                   "_id": "3",
                   "_score": 1,
                   "_source": {
                     "title": "我是waws520"
                   }
                 }
               ]
             }
           }
         },
         {
           "key": "waws521",
           "doc_count": 1,
           "top_blogs": {
             "hits": {
               "total": 1,
               "max_score": 1,
               "hits": [
                 {
                   "_index": "waws_webblog",
                   "_type": "blogs",
                   "_id": "4",
                   "_score": 1,
                   "_source": {
                     "title": "waws521的身世揭秘"
                   }
                 }
               ]
             }
           }
         }
       ]
     }
   }
 }

Elasticsearch高手进阶篇(63)

数据建模实战_对文件系统进行数据建模以及文件搜索实战

数据建模,对类似文件系统这种的有多层级关系的数据进行建模

文件系统数据构造

 PUT /waws_fs
 {
   "settings": {
     "analysis": {
       "analyzer": {
         "paths": { 
           "tokenizer": "path_hierarchy"
         }
       }
     }
   }
 }
 
 GET /waws_fs/_analyze
 {
   "text":"/a/b/c/d",
   "analyzer": "paths"
 }
 
 {
   "tokens": [
     {
       "token": "/a",
       "start_offset": 0,
       "end_offset": 2,
       "type": "word",
       "position": 0
     },
     {
       "token": "/a/b",
       "start_offset": 0,
       "end_offset": 4,
       "type": "word",
       "position": 0
     },
     {
       "token": "/a/b/c",
       "start_offset": 0,
       "end_offset": 6,
       "type": "word",
       "position": 0
     },
     {
       "token": "/a/b/c/d",
       "start_offset": 0,
       "end_offset": 8,
       "type": "word",
       "position": 0
     }
   ]
 }

path_hierarchy tokenizer讲解

/a/b/c/d --> path_hierarchy -> /a/b/c/d, /a/b/c, /a/b, /a

fs: filesystem

 PUT /waws_fs/_mapping/file
 {
   "properties": {
     "name": { 
       "type":"keyword"
     },
     "path": { 
       "type":"keyword",
       "fields": {
         "tree": { 
           "type":"text",
           "analyzer": "paths"
         }
       }
     }
   }
 }
  • 添加数据
 PUT /waws_fs/file/1
 {
   "name":"README.txt", 
   "path":"/workspace/projects/helloworld", 
   "contents": "这是我的第一个elasticsearch程序"
 }

对文件系统执行搜索

  • 文件搜索需求:查找一份,内容包括elasticsearch,在/workspace/projects/hellworld这个目录下的文件
 GET /waws_fs/file/_search 
 {
   "query": {
     "bool": {
       "must": [
         {
           "match": {
             "contents": "elasticsearch"
           }
         },
         {
           "constant_score": {
             "filter": {
               "term": {
                 "path": "/workspace/projects/helloworld"
               }
             }
           }
         }
       ]
     }
   }
 }
 ​
 {
   "took": 38,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 1.284885,
     "hits": [
       {
         "_index": "waws_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 /waws_fs/file/_search 
 {
   "query": {
     "bool": {
       "must": [
         {
           "match": {
             "contents": "elasticsearch"
           }
         },
         {
           "constant_score": {
             "filter": {
               "term": {
                 "path.tree": "/workspace"
               }
             }
           }
         }
       ]
     }
   }
 }
 
 {
   "took": 2,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 1.284885,
     "hits": [
       {
         "_index": "waws_fs",
         "_type": "file",
         "_id": "1",
         "_score": 1.284885,
         "_source": {
           "name": "README.txt",
           "path": "/workspace/projects/helloworld",
           "contents": "这是我的第一个elasticsearch程序"
         }
       }
     ]
   }
 }