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

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: Elasticsearch数据建模实战之对文件系统进行数据建模以及文件搜索实战

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

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

搜索需求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"
              }
            }
          }
        }
      ]
    }
  }
}

结果:

1. {
2.   "took": 2,
3.   "timed_out": false,
4.   "_shards": {
5.     "total": 5,
6.     "successful": 5,
7.     "failed": 0
8.   },
9.   "hits": {
10.     "total": 1,
11.     "max_score": 1.284885,
12.     "hits": [
13.       {
14.         "_index": "fs",
15.         "_type": "file",
16.         "_id": "1",
17.         "_score": 1.284885,
18.         "_source": {
19.           "name": "README.txt",
20.           "path": "/workspace/projects/helloworld",
21.           "contents": "这是我的第一个elasticsearch程序"
22.         }
23.       }
24.     ]
25.   }
26. }

 


相关实践学习
利用Elasticsearch实现地理位置查询
本实验将分别介绍如何使用Elasticsearch7.10版本进行全文检索、多语言检索和地理位置查询三个Elasticsearch基础检索子场景的实现。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
1天前
|
运维 监控 Java
在大数据场景下,Elasticsearch作为分布式搜索与分析引擎,因其扩展性和易用性成为全文检索首选。
【7月更文挑战第1天】在大数据场景下,Elasticsearch作为分布式搜索与分析引擎,因其扩展性和易用性成为全文检索首选。本文讲解如何在Java中集成Elasticsearch,包括安装配置、使用RestHighLevelClient连接、创建索引和文档操作,以及全文检索查询。此外,还涉及高级查询、性能优化和故障排查,帮助开发者高效处理非结构化数据。
11 0
|
4天前
|
缓存 数据处理 数据安全/隐私保护
Elasticsearch索引状态管理实战指南
Elasticsearch索引状态管理实战指南
7 0
|
4天前
|
存储 索引
Elasticsearch索引之嵌套类型:深度剖析与实战应用
Elasticsearch索引之嵌套类型:深度剖析与实战应用
|
5天前
|
缓存 Java API
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——聚合与搜索(三)
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧——聚合与搜索(三)
|
6天前
|
存储 自然语言处理 NoSQL
D7 Elasticsearch-Mongodb(搜索记录)
D7 Elasticsearch-Mongodb(搜索记录)
10 0
|
10天前
|
存储 数据采集 负载均衡
Elasticsearch系列---搜索执行过程及scroll游标查询
Elasticsearch系列---搜索执行过程及scroll游标查询
|
18天前
|
存储 JSON 搜索推荐
Springboot2.x整合ElasticSearch7.x实战(三)
Springboot2.x整合ElasticSearch7.x实战(三)
16 0
|
18天前
|
存储 自然语言处理 关系型数据库
Springboot2.x整合ElasticSearch7.x实战(二)
Springboot2.x整合ElasticSearch7.x实战(二)
14 0
|
4天前
|
自然语言处理 搜索推荐
在Elasticsearch 7.9.2中安装IK分词器并进行自定义词典配置
在Elasticsearch 7.9.2中安装IK分词器并进行自定义词典配置
7 1
|
4天前
|
Windows
Windows安装Elasticsearch 7.9.2
Windows安装Elasticsearch 7.9.2
5 0