elasticsearch 在弹性搜索中,如何使用path_层次结构从根到1级子体获取对象?,elasticsearch,tree,parent-child,hierarchy,elasticsearch,Tree,Parent Child,Hierarchy" /> elasticsearch 在弹性搜索中,如何使用path_层次结构从根到1级子体获取对象?,elasticsearch,tree,parent-child,hierarchy,elasticsearch,Tree,Parent Child,Hierarchy" />

elasticsearch 在弹性搜索中,如何使用path_层次结构从根到1级子体获取对象?

elasticsearch 在弹性搜索中,如何使用path_层次结构从根到1级子体获取对象?,elasticsearch,tree,parent-child,hierarchy,elasticsearch,Tree,Parent Child,Hierarchy,我在弹性搜索中使用path_层次结构来维护树状结构 PUT file-path-test { "settings": { "analysis": { "analyzer": { "custom_path_tree": { "tokenizer": "custom_hierarchy" },

我在弹性搜索中使用path_层次结构来维护树状结构

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/6
{
  "file_path": "/folder1"
}

POST file-path-test/_doc/7
{
  "file_path": "/folder1/folder2"
}

POST file-path-test/_doc/8
{
  "file_path": "/folder1/folder2/folder3"
}

POST file-path-test/_doc/9
{
  "file_path": "/folder1/folder2/folder3/folder4"
}
所以我希望查询只显示从根级别到1级别对象的对象 e、 g如果我得到路径'/folder1/folder2',则查询应返回

/folder1
/folder1/folder2
/folder1/folder2/folder3
它不应包括以下值

/folder1/folder2/folder3/folder4
下面的查询返回整个树的值,即从根节点到叶节点

GET file-path-test/_search
{
  "query": {
    "term": {
       "file_path.tree": "/folder1/folder2"
    }
  }
  }
结果:

/folder1
/folder1/folder2
/folder1/folder2/folder3
/folder1/folder2/folder3/folder4

查询时间文件路径需要转换为可比较的子路径列表-与
自定义路径树
分析器生成的路径相同

繁重的工作可能发生在脚本内部,但为了访问此类脚本中的
文件\u path.tree
成员,需要对映射进行轻微但重要的修改:

PUT文件路径测试
{
“设置”:{
“分析”:{
...
}
},
“映射”:{
“财产”:{
“文件路径”:{
“类型”:“文本”,
“字段”:{
“树”:{
“类型”:“文本”,
“现场数据”:正确,