提取ID为路径和文件名的文档,然后按ID(路径和文件名)搜索

时间:2019-02-22 12:22:09

标签: c# elasticsearch elastic-stack

我有这个POCO:

public class Document
{
    public string Id { get; set; }
    public string Content { get; set; }
    public Attachment Attachment { get; set; }
}

此处,该ID是要建立索引的本地文件系统中文档的唯一路径。

此索引定义如下:

private static void CreateIndex(ElasticClient client, string indexName)
{
    if (!client.IndexExists(indexName).Exists)
    {
    var indexResponse = client.CreateIndex(indexName, c => c
        .Settings(s => s
        .Analysis(a => a
            .Analyzers(ad => ad
            .Custom("windows_path_hierarchy_analyzer", ca => ca
                .Tokenizer("windows_path_hierarchy_tokenizer")
            )
            )
            .Tokenizers(t => t
            .PathHierarchy("windows_path_hierarchy_tokenizer", ph => ph
                .Delimiter('\\')
            )
            )
        )
        )
        .Mappings(m => m
        .Map<Document>(mp => mp
            .AutoMap()
            .AllField(all => all
            .Enabled(false)
            )
            .Properties(ps => ps
            .Object<Attachment>(a => a
                .Name(n => n.Attachment)
                .AutoMap()
            )
            )
        )
        )
    );
    }
}

我按填充量对文件进行批量索引:

var document = new Document {     ID = pathToFile,     内容= Convert.ToBase64String(File.ReadAllBytes(pathToFile)) };

将它们添加到列表(文档)并执行批量上传/索引:

var bulkResponse = client.Bulk(b => b
    .Pipeline("attachments")
    .IndexMany(documents)
);

然后我尝试按其唯一ID /路径查找文档,如下所示:

var searchResponse = client.Search<Document>(s => s
    .Query(q => q
    .Match(m => m
        .Field(a => a.Id)
        .Query(pathToFile)
    )
    )
);

我收到几个具有不同ID(与唯一的pathToFile不同)的文档的响应。我在这里做错什么了吗?谢谢!

0 个答案:

没有答案