进阶-第62__数据建模实战_对文件系统进行数据建模以及文件搜索实战

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

 

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

测试

GET /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

    }

  ]

}

 

fs: filesystem

创建mapping

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": 6,

  "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"

              }

            }

          }

        }

      ]

    }

  }

}

结果:

{

  "took": 1,

  "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程序"

        }

      }

    ]

  }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值