在 Elastic NEST 中映射和索引路径层次结构以在目录路径中进行搜索
Mapping and indexing Path hierarchy in Elastic NEST to search with in directory paths
我需要在特定目录中搜索文件和文件夹。为此,elastic 要求我们创建分析器并将分词器设置为 path_hierarchy
PUT /fs
{
"settings": {
"analysis": {
"analyzer": {
"paths": {
"tokenizer": "path_hierarchy"
}
}
}
}
}
然后,使用两个属性创建如下图所示的映射:名称(保存文件的名称)和路径(用于存储目录路径):
PUT /fs/_mapping/file
{
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
},
"path": {
"type": "string",
"index": "not_analyzed",
"fields": {
"tree": {
"type": "string",
"analyzer": "paths"
}
}
}
}
}
这需要我们索引文件所在目录的路径:
PUT /fs/file/1
{
"name": "README.txt",
"path": "/clinton/projects/elasticsearch",
}
The Question:
How can i create this mapping in NEST Elastic using c#?
分析器是通过声明自定义分析器创建的,然后将其分词器设置为“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" 将保留系统中目录的位置
//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)
)
)
));
我需要在特定目录中搜索文件和文件夹。为此,elastic 要求我们创建分析器并将分词器设置为 path_hierarchy
PUT /fs
{
"settings": {
"analysis": {
"analyzer": {
"paths": {
"tokenizer": "path_hierarchy"
}
}
}
}
}
然后,使用两个属性创建如下图所示的映射:名称(保存文件的名称)和路径(用于存储目录路径):
PUT /fs/_mapping/file
{
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
},
"path": {
"type": "string",
"index": "not_analyzed",
"fields": {
"tree": {
"type": "string",
"analyzer": "paths"
}
}
}
}
}
这需要我们索引文件所在目录的路径:
PUT /fs/file/1
{
"name": "README.txt",
"path": "/clinton/projects/elasticsearch",
}
The Question: How can i create this mapping in NEST Elastic using c#?
分析器是通过声明自定义分析器创建的,然后将其分词器设置为“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" 将保留系统中目录的位置
//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)
)
)
));