前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ES路径分词器

ES路径分词器

作者头像
小石头
发布2022-11-10 21:25:02
3260
发布2022-11-10 21:25:02
举报
文章被收录于专栏:小石头小石头

附:ElasticSearch version :7.10

path_hierarchy tokenizer 把分层的值看成是文件路径,用路径分隔符分割文本,输出树上的各个节点。

代码语言:javascript
复制
POST _analyze
{
  "tokenizer": "path_hierarchy",
  "text": "/one/two/three"
}

输出为

代码语言:javascript
复制
[ /one, /one/two, /one/two/three ]

Configurationedit

The path_hierarchy tokenizer accepts the following parameters:

delimiter

The character to use as the path separator. Defaults to /.

replacement

An optional replacement character to use for the delimiter. Defaults to the delimiter.

buffer_size

The number of characters read into the term buffer in a single pass. Defaults to 1024. The term buffer will grow by this size until all the text has been consumed. It is advisable not to change this setting.

reverse

If set to true, emits the tokens in reverse order. Defaults to false.

skip

The number of initial tokens to skip. Defaults to 0.

实例数据:

In this example, we configure the path_hierarchy tokenizer to split on - characters, and to replace them with /. The first two tokens are skipped:

代码语言:javascript
复制
PUT my-index-000001
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "path_hierarchy",
          "delimiter": "-",
          "replacement": "/",
          "skip": 2
        }
      }
    }
  }
}

POST my-index-000001/_analyze
{
  "analyzer": "my_analyzer",
  "text": "one-two-three-four-five"
}

The above example produces the following terms:

代码语言:javascript
复制
[ /three, /three/four, /three/four/five ]

If we were to set reverse to true, it would produce the following:(倒序输出)

代码语言:javascript
复制
[ one/two/three/, two/three/, three/ ]

详细示例:(路径查询)

代码语言:javascript
复制
PUT file-path-test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_path_tree": {
          "tokenizer": "custom_hierarchy"
        },
        "custom_path_tree_reversed": {
          "tokenizer": "custom_hierarchy_reversed"
        }
      },
      "tokenizer": {
        "custom_hierarchy": {
          "type": "path_hierarchy",
          "delimiter": "/"
        },
        "custom_hierarchy_reversed": {
          "type": "path_hierarchy",
          "delimiter": "/",
          "reverse": "true"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "file_path": {
        "type": "text",
        "fields": {
          "tree": {
            "type": "text",
            "analyzer": "custom_path_tree"
          },
          "tree_reversed": {
            "type": "text",
            "analyzer": "custom_path_tree_reversed"
          }
        }
      }
    }
  }
}

POST file-path-test/_doc/1
{
  "file_path": "/User/alice/photos/2017/05/16/my_photo1.jpg"
}

POST file-path-test/_doc/2
{
  "file_path": "/User/alice/photos/2017/05/16/my_photo2.jpg"
}

POST file-path-test/_doc/3
{
  "file_path": "/User/alice/photos/2017/05/16/my_photo3.jpg"
}

POST file-path-test/_doc/4
{
  "file_path": "/User/alice/photos/2017/05/15/my_photo1.jpg"
}

POST file-path-test/_doc/5
{
  "file_path": "/User/bob/photos/2017/05/16/my_photo1.jpg"
}

match匹配,通过相关性进行匹配,如果没搜到,也会按照相关性进行匹配。

代码语言:javascript
复制
GET file-path-test/_search
{
  "query": {
    "match": {
      "file_path": "/User/bob/photos/2017/05"
    }
  }
}

精准匹配,term来进行匹配路径,通常使用该方式来进行路径查询匹配

代码语言:javascript
复制
GET file-path-test/_search
{
  "query": {
    "term": {
      "file_path.tree": "/User/alice/photos/2017/05/16"
    }
  }
}

同样,该查询也可用于多条件进行组合查询。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-1-05 2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Configurationedit
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档