嵌套建议完成用法,引发"不是完成建议字段"异常



我是Elasticsearch的完整初学者,我一直在尝试使用Elasticsearch的完成建议使用NEST在属性上使用NEST进行自动完成。

这是我的映射(如下所述:):

  var createResult = client.CreateIndex(indexName, index => index                
            .AddMapping<Contact>(tmd => tmd
                .Properties(props => props
                    .Completion(s =>
                        s.Name(p => p.CompanyName.Suffix("completion"))
                        .IndexAnalyzer("standard")
                        .SearchAnalyzer("standard")
                        .MaxInputLength(20)
                        .Payloads()
                        .PreservePositionIncrements()
                        .PreserveSeparators())                        
                )
            )
        );
var resultPerson = client.IndexMany(documents.OfType<Person>(), new SimpleBulkParameters { Refresh = true });
var resultCompany = client.IndexMany(documents.OfType<Company>(), new SimpleBulkParameters { Refresh = true });

在索引时,我只是使用IndexMany方法并通过IEnumberable<Contact>(联系人具有命名公司名称的属性,联系人是一个抽象类,个人和公司都是它的具体实现)。搜索引发了一个例外,说 elasticsearchException [field [companyName]不是完成建议字段] 。查询看起来如下:

SearchDescriptor<Contact> descriptor = new SearchDescriptor<Contact>();
descriptor = descriptor.SuggestCompletion("suggest", c => c.OnField(f => f.CompanyName).Text(q));
var result = getElasticClientInstance("contacts").Search<Contact>(body => descriptor);
string qe = result.ConnectionStatus.ToString();

我在这里做错了什么,我研究了Nest在SuggestCompletion上的测试,但没有太大帮助,这意味着该测试仅描绘了如何获取建议,但没有在如何为pusscompletion设置索引映射。

我还尝试设置edgeNgram tokenizer,如本文所述,但也无法继续。

关于如何进行的任何方向或示例将极大地帮助您。

update

您正在尝试创建一个名称为" CompanyName.completion"的属性,但在此位置无效,它将使用最后的令牌"完成"。因此,它实际上映射了一个名为"完成"的字段。...尝试将呼叫更改为: .Name(p => p.CompanyName)

其他观察结果

您为Contact指定映射,但是在索引时,您使用PersonCompany类型。

用elasticsearch术语映射:

/index/contact/

但是您的文档正在进入:

/index/person//index/company

Nest不会自动映射特定类的所有实现,而Elasticsearch无法知道这三个相关。

我会将映射重构为方法,并将其称为所有涉及类型的类型。

 var createResult = client.CreateIndex(indexName, index => index                
      .AddMapping<Contact>(tmd => MapContactCompletionFields(tmd))
      .AddMapping<Person>(tmd => MapContactCompletionFields(tmd))
      .AddMapping<Company>(tmd => MapContactCompletionFields(tmd))   
  );
 private RootObjectMappingDescriptor<TContact>  MapContactCompletionFields<TContact>(
      RootObjectMappingDescriptor<TContact> tmd)
      where TContact : Contact
 {
      return  tmd.Properties(props => props
           .Completion(s => s
                .Name(p => p.CompanyName.Suffix("completion"))
                .IndexAnalyzer("standard")
                .SearchAnalyzer("standard")
                .MaxInputLength(20)
                .Payloads()
                .PreservePositionIncrements()
                .PreserveSeparators()
           )                        
       );
 } 

该方法返回描述符,因此您可以在其上进一步链条。

然后,当您搜索联系人时:

var result = getElasticClientInstance("contacts").Search<Contact>(
    body => descriptor
        .Types(typeof(Person), typeof(Company))
);

该类型提示将使搜索搜索查看/index/person/index/company,并知道如何将您的文档的协变量列表归还。

因此,您可以在上一个通话后进行result.Documents.OfType<Person>()

最新更新