白话Elasticsearch56-数据建模之 Path Hierarchy Tokenizer 对文件系统进行数据建模以及文件搜索

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: 白话Elasticsearch56-数据建模之 Path Hierarchy Tokenizer 对文件系统进行数据建模以及文件搜索

20190806092132811.jpg

概述

继续跟中华石杉老师学习ES,第56篇

课程地址https://www.roncoo.com/view/55


官网

简言之,就是对类似文件系统这种的有多层级关系的数据进行分词

Path Hierarchy Tokenizer戳这里

Path Hierarchy Tokenizer Examples:戳这里

20190901114916122.png

20190901114930869.png

20190901114953659.png

20190901115008916.png


示例

模拟:文件系统数据构造


PUT /filesystem
{
  "settings": {
    "analysis": {
      "analyzer": {
        "paths": { 
          "tokenizer": "path_hierarchy"
        }
      }
    }
  }
}

测试path_hierarchy分词

POST filesystem/_analyze
{
  "tokenizer": "path_hierarchy",
  "text": "/home/elasticsearch/image"
}


返回:

{
  "tokens": [
    {
      "token": "/home",
      "start_offset": 0,
      "end_offset": 5,
      "type": "word",
      "position": 0
    },
    {
      "token": "/home/elasticsearch",
      "start_offset": 0,
      "end_offset": 19,
      "type": "word",
      "position": 0
    },
    {
      "token": "/home/elasticsearch/image",
      "start_offset": 0,
      "end_offset": 25,
      "type": "word",
      "position": 0
    }
  ]
}



path_hierarchy tokenizer: 会把/a/b/c/d路径通过path_hierarchy 分词为 /a/b/c/d, /a/b/c, /a/b, /a


需求一: 查找一份,内容包括ES,


在/workspace/workspace/projects/helloworld这个目录下的文件

手动指定字段类型,并模拟个数据到索引

#指定字段类型
PUT /filesystem/_mapping/file
{
  "properties": {
    "name": { 
      "type":  "keyword"
    },
    "path": { 
      "type":  "keyword",
      "fields": {
        "tree": { 
          "type":     "text",
          "analyzer": "paths"
        }
      }
    }
  }
}
#查看映射
GET /filesystem/_mapping
#写入数据
PUT /filesystem/file/1
{
  "name":     "README.txt", 
  "path":     "/workspace/projects/helloworld", 
  "contents": "小工匠跟石杉老师学习ES"
}

需求DSL:

#文件搜索需求:查找一份,内容包括ES,在/workspace/workspace/projects/helloworld这个目录下的文件
GET /filesystem/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "contents": "ES"
          }
        }
      ],
      "filter": {
        "term": {
          "path": "/workspace/projects/helloworld"
        }
      }
    }
  }
}


返回:


20190901172846894.png


需求二: 搜索/workspace目录下,内容包含ES的所有的文件

再写几条数据进去

PUT /filesystem/file/2
{
  "name":     "README.txt", 
  "path":     "/workspace/projects", 
  "contents": "小工匠跟石杉老师学习ES"
}
PUT /filesystem/file/3
{
  "name":     "README.txt", 
  "path":     "/workspace/xxxxx", 
  "contents": "小工匠跟石杉老师学习ES"
}
PUT /filesystem/file/4
{
  "name":     "README.txt", 
  "path":     "/home/artisan", 
  "contents": "小工匠跟石杉老师学习ES"
}
PUT /filesystem/file/5
{
  "name":     "README.txt", 
  "path":     "/workspace", 
  "contents": "小工匠跟石杉老师学习ES"
}

需求DSL: "path.tree": "/workspace"

GET filesystem/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
             "contents": "ES"
          }
        }
      ],
      "filter": {
        "term": {
          "path.tree": "/workspace"
        }
      }
    }
  }
}


返回:

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "filesystem",
        "_type": "file",
        "_id": "5",
        "_score": 0.2876821,
        "_source": {
          "name": "README.txt",
          "path": "/workspace",
          "contents": "小工匠跟石杉老师学习ES"
        }
      },
      {
        "_index": "filesystem",
        "_type": "file",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "README.txt",
          "path": "/workspace/projects/helloworld",
          "contents": "小工匠跟石杉老师学习ES"
        }
      },
      {
        "_index": "filesystem",
        "_type": "file",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "name": "README.txt",
          "path": "/workspace/xxxxx",
          "contents": "小工匠跟石杉老师学习ES"
        }
      },
      {
        "_index": "filesystem",
        "_type": "file",
        "_id": "2",
        "_score": 0.18232156,
        "_source": {
          "name": "README.txt",
          "path": "/workspace/projects",
          "contents": "小工匠跟石杉老师学习ES"
        }
      }
    ]
  }
}


可以看到id=4的数据,不符合需求,没有被查询出来,OK。


相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
相关文章
|
1月前
|
缓存 监控 前端开发
顺企网 API 开发实战:搜索 / 详情接口从 0 到 1 落地(附 Elasticsearch 优化 + 错误速查)
企业API开发常陷参数、缓存、错误处理三大坑?本指南拆解顺企网双接口全流程,涵盖搜索优化、签名验证、限流应对,附可复用代码与错误速查表,助你2小时高效搞定开发,提升响应速度与稳定性。
|
1月前
|
存储 Linux iOS开发
Elasticsearch Enterprise 9.1.5 发布 - 分布式搜索和分析引擎
Elasticsearch Enterprise 9.1.5 (macOS, Linux, Windows) - 分布式搜索和分析引擎
235 0
|
2月前
|
JSON 监控 Java
Elasticsearch 分布式搜索与分析引擎技术详解与实践指南
本文档全面介绍 Elasticsearch 分布式搜索与分析引擎的核心概念、架构设计和实践应用。作为基于 Lucene 的分布式搜索引擎,Elasticsearch 提供了近实时的搜索能力、强大的数据分析功能和可扩展的分布式架构。本文将深入探讨其索引机制、查询 DSL、集群管理、性能优化以及与各种应用场景的集成,帮助开发者构建高性能的搜索和分析系统。
249 0
|
存储 自然语言处理 BI
从 Elasticsearch 到 Apache Doris 腾讯音乐内容库升级,统一搜索分析引擎,成本直降 80%
实现写入性能提升 4 倍、使用成本节省达 80% 的显著成效
453 1
从 Elasticsearch 到 Apache Doris 腾讯音乐内容库升级,统一搜索分析引擎,成本直降 80%
|
6月前
|
存储 安全 Linux
Elasticsearch Enterprise 9.0 发布 - 分布式搜索和分析引擎
Elasticsearch Enterprise 9.0 (macOS, Linux, Windows) - 分布式搜索和分析引擎
300 0
|
6月前
|
存储 Linux iOS开发
Elasticsearch Enterprise 8.18 发布 - 分布式搜索和分析引擎
Elasticsearch Enterprise 8.18 (macOS, Linux, Windows) - 分布式搜索和分析引擎
247 0
|
11月前
|
数据采集 人工智能 运维
从企业级 RAG 到 AI Assistant,阿里云Elasticsearch AI 搜索技术实践
本文介绍了阿里云 Elasticsearch 推出的创新型 AI 搜索方案
641 3
从企业级 RAG 到 AI Assistant,阿里云Elasticsearch AI 搜索技术实践
|
11月前
|
机器学习/深度学习 人工智能 运维
阿里云技术公开课直播预告:基于阿里云 Elasticsearch 构建 AI 搜索和可观测 Chatbot
阿里云技术公开课预告:Elastic和阿里云搜索技术专家将深入解读阿里云Elasticsearch Enterprise版的AI功能及其在实际应用。
550 2
阿里云技术公开课直播预告:基于阿里云 Elasticsearch 构建 AI 搜索和可观测 Chatbot
|
10月前
|
人工智能 自然语言处理 搜索推荐
云端问道12期实操教学-构建基于Elasticsearch的企业级AI搜索应用
本文介绍了构建基于Elasticsearch的企业级AI搜索应用,涵盖了从传统关键词匹配到对话式问答的搜索形态演变。阿里云的AI搜索产品依托自研和开源(如Elasticsearch)引擎,提供高性能检索服务,支持千亿级数据毫秒响应。文章重点描述了AI搜索的三个核心关键点:精准结果、语义理解、高性能引擎,并展示了架构升级和典型应用场景,包括智能问答、电商导购、多模态图书及商品搜索等。通过实验部分,详细演示了如何使用阿里云ES搭建AI语义搜索Demo,涵盖模型创建、Pipeline配置、数据写入与检索测试等步骤,同时介绍了相关的计费模式。
330 3
|
10月前
|
人工智能 算法 API
构建基于 Elasticsearch 的企业级 AI 搜索应用
本文介绍了基于Elasticsearch构建企业级AI搜索应用的方案,重点讲解了RAG(检索增强生成)架构的实现。通过阿里云上的Elasticsearch AI搜索平台,简化了知识库文档抽取、文本切片等复杂流程,并结合稠密和稀疏向量的混合搜索技术,提升了召回和排序的准确性。此外,还探讨了Elastic的向量数据库优化措施及推理API的应用,展示了如何在云端高效实现精准的搜索与推理服务。未来将拓展至多模态数据和知识图谱,进一步提升RAG效果。
392 1

热门文章

最新文章