映射和索引弹性NEST中的路径层次结构,用于在目录路径中进行搜索 [英] Mapping and indexing Path hierarchy in Elastic NEST to search with in directory paths

查看:144
本文介绍了映射和索引弹性NEST中的路径层次结构,用于在目录路径中进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在特定目录中搜​​索文件和文件夹。为了做到这一点,弹性要求我们创建分析器并将tokenizer设置为 path_hierarchy

I need to search for files and folder with in specific directories. In order to do that, elastic asks us to create the analyzer and set the tokenizer to path_hierarchy

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

然后,使用两个属性创建映射:name(保存文件的名称)和path(存储目录路径):

Then, create the mapping as illustrated below with two properties: name (holding the name of the file) and path (to store the directory path):

PUT /fs/_mapping/file
{
    "properties": {
        "name": {
            "type": "string",
            "index": "not_analyzed"
        },
        "path": {
            "type": "string",
            "index": "not_analyzed",
            "fields": {
                "tree": {
                    "type": "string",
                    "analyzer": "paths"
                }
            }
        }
    }
}

这需要我们索引文件所在目录的路径:

This requires us to index the path of the directory where the file lives:

PUT /fs/file/1
{
  "name": "README.txt",
  "path": "/clinton/projects/elasticsearch",
}




问题:
如何在NEST弹性中创建此映射使用c#?

The Question: How can i create this mapping in NEST Elastic using c#?


推荐答案

分析器是通过声明一个自定义分析器创建的,然后设置它tokenizer到 path_tokenizer

The analyzer is created by declaring a custom analyzer, and then setting its tokenizer to "path_tokenizer":

                //name of the tokenizer  "path_tokenizer"
                string pathTokenizerName = "path_tokenizer";

                //the name of the analyzer
                string pathAnalyzerName = "path";

                PathHierarchyTokenizer pathTokenizer = new PathHierarchyTokenizer();

                AnalyzerBase pathAnalyzer = new CustomAnalyzer
                {
                    Tokenizer = pathTokenizerName,
                };

第二步是使用所需的分析器和映射创建索引,在属性LogicalPath 将保留系统中目录的位置

The second step is creating the index with required analyzer and mapping, in the code below the property "LogicalPath" will keep the locations of directories in the system"

                //Create the index,
                     elastic.CreateIndex(_indexName, i => i
                        .NumberOfShards(1).NumberOfReplicas(0)
                        // Adding the path analyzers to the index.
                            .Analysis(an => an
                                .Tokenizers(tokenizers => tokenizers
                                    .Add(pathTokenizerName, pathTokenizer)
                                )
                                .Analyzers(analyzer => analyzer
                                    .Add(pathAnalyzerName, pathAnalyzer)
                                )
                            )
                        // Add the mappings
                            .AddMapping<Document>(t => t
                                .MapFromAttributes()
                                    .Properties(props => props
                                    //associating path tokenizer with required property  "Logical Path"
                                        .String(myPathProperty => myPathProperty
                                             .Name(_t => _t.LogicalPath)
                                             .IndexAnalyzer(pathAnalyzerName)
                                    )
                            )
                        ));

这篇关于映射和索引弹性NEST中的路径层次结构,用于在目录路径中进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆