Elasticsearch NEST 客户端创建多字段并完成

标签 elasticsearch autocomplete nest autosuggest

我正在尝试在我的一些字段上创建一些完成建议。我的文档类如下所示:

[ElasticType(Name = "rawfiles", IdProperty = "guid")]
public class RAW
{
    [ElasticProperty(OmitNorms = true, Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String, Store = true)]
    public string guid { get; set; }

    [ElasticProperty(OmitNorms = true, Index = FieldIndexOption.Analyzed, Type = FieldType.String, Store = true, IndexAnalyzer = "def_analyzer", SearchAnalyzer = "def_analyzer_search", AddSortField = true)]
    public string filename { get; set; }

    [ElasticProperty(OmitNorms = true, Index = FieldIndexOption.Analyzed, Type = FieldType.String, Store = true, IndexAnalyzer = "def_analyzer", SearchAnalyzer = "def_analyzer_search")]
    public List<string> tags { get { return new List<string>(); } }
}

这是我尝试创建完成字段的方法

public bool CreateMapping(ElasticClient client, string indexName)
{
    IIndicesResponse result = null;
    try
    {
        result = client.Map<RAW>(
                c => c.Index(indexName)
                    .MapFromAttributes()
                    .AllField(f => f.Enabled(false))
                    .SourceField(s => s.Enabled())
                    .Properties(p => p
                        .Completion(s => s.Name(n => n.tags.Suffix("comp"))
                                    .IndexAnalyzer("standard")
                                    .SearchAnalyzer("standard")
                                    .MaxInputLength(20)
                                    .Payloads()
                                    .PreservePositionIncrements()
                                    .PreserveSeparators())
                        .Completion(s2 => s2.Name(n=>n.filename.Suffix("comp"))
                                    .IndexAnalyzer("standard")
                                    .SearchAnalyzer("standard")
                                    .MaxInputLength(20)
                                    .Payloads()
                                    .PreservePositionIncrements()
                                    .PreserveSeparators())
                        )
                   );
    }
    catch (Exception)
    {

    }

    return result != null && result.Acknowledged;
}

我的问题是,这只是创建一个名为“comp”的单个完成字段。我的印象是,这将创建两个完成字段,一个名为 filename.comp,另一个名为 Tags.comp。

然后我尝试了这个 SO question 的答案但这使事情变得更复杂,因为现在我的两个字段仅映射为完成字段。

需要明确的是,我想创建一个包含数据、排序和完成字段的多字段(字段)。很像 this example 中的那个

最佳答案

这就是您如何重现附件中的自动完成示例 article .

我的简单类(我们将在 Name 属性上实现自动完成)

public class Document
{
    public int Id { get; set; }
    public string Name { get; set; }
}

要在 NEST 中创建多字段映射,我们必须以这种方式定义映射:

var indicesOperationResponse = client.CreateIndex(descriptor => descriptor
    .Index(indexName)
    .AddMapping<Document>(m => m
        .Properties(p => p.MultiField(mf => mf
            .Name(n => n.Name)
            .Fields(f => f
                .String(s => s.Name(n => n.Name).Index(FieldIndexOption.Analyzed))
                .String(s => s.Name(n => n.Name.Suffix("sortable")).Index(FieldIndexOption.NotAnalyzed))
                .String(s => s.Name(n => n.Name.Suffix("autocomplete")).IndexAnalyzer("shingle_analyzer"))))))
    .Analysis(a => a
        .Analyzers(b => b.Add("shingle_analyzer", new CustomAnalyzer
        {
            Tokenizer = "standard",
            Filter = new List<string> {"lowercase", "shingle_filter"}
        }))
        .TokenFilters(b => b.Add("shingle_filter", new ShingleTokenFilter
        {
            MinShingleSize = 2,
            MaxShingleSize = 5
        }))));

让我们索引一些文档:

client.Index(new Document {Id = 1, Name = "Tremors"});
client.Index(new Document { Id = 2, Name = "Tremors 2: Aftershocks" });
client.Index(new Document { Id = 3, Name = "Tremors 3: Back to Perfection" });
client.Index(new Document { Id = 4, Name = "Tremors 4: The Legend Begins" });
client.Index(new Document { Id = 5, Name = "True Blood" });
client.Index(new Document { Id = 6, Name = "Tron" });
client.Index(new Document { Id = 7, Name = "True Grit" });
client.Index(new Document { Id = 8, Name = "Land Before Time" });
client.Index(new Document { Id = 9, Name = "The Shining" });
client.Index(new Document { Id = 10, Name = "Good Burger" });

client.Refresh();

现在,我们准备编写前缀查询:)

var searchResponse = client.Search<Document>(s => s
    .Query(q => q
        .Prefix("name.autocomplete", "tr"))
    .SortAscending(sort => sort.Name.Suffix("sortable")));

此查询将帮助我们

Tremors 2: Aftershocks
Tremors 3: Back to Perfection
Tremors 4: The Legend Begins
Tron
True Blood
True Grit

希望这对您有帮助。

最近,NEST 的小伙子们准备得很好tutorial关于 NEST 和 Elasticsearch。有一部分是关于suggestions ,它应该对你很有用。

关于Elasticsearch NEST 客户端创建多字段并完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30285065/

相关文章:

azure - 是否可以在 Elasticsearch 中记录 Azure 云服务性能计数器?

elasticsearch - 是否为ignore_unavailable设置为true的Elasticsearch创建NEST搜索请求?

elasticsearch - 筛选器构面返回所有文档的计数,而不是范围

elasticsearch - 如何在Elasticsearch中的多个字段中搜索?

sorting - Elasticsearch使用嵌套聚合中的值对公式进行排序

elasticsearch - 如何为数字和文本搜索建立索引的字段

javascript - 我的 jquery 自动完成功能不起作用

algorithm - 谷歌模糊搜索(又名 "suggestions"): What technique(s) are in use?

javascript - Google 自动完成 API 首先返回我所在州的地点,而不是最多 "expected"地点

elasticsearch - 在映射中设置自定义类型名称