Nest SuggestCompletion usage, throws 'is not a completion suggest field' exception

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 12:19:29

UPDATE

You are try to create a property with the name "companyName.completion" but at that position its not valid and it will use the last token "completion". So its actually mapping a field called completion.... try changing the call to: .Name(p => p.CompanyName)

Other observations

You specify a mapping for the Contact but while indexing you use the Person and Company types.

In elasticsearch terms you mapped:

/index/contact/

but your documents are going into:

/index/person/ and /index/company

NEST won't automatically map all implementation of a specific class and elasticsearch has no way of knowing the three are related.

I would refactor the mapping to a method and call it for all the types involved.

 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()
           )                        
       );
 } 

That method returns the descriptor so you can further chain on it.

Then when you do a search for contacts:

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

That types hint will cause the search to looking /index/person and /index/company and will know how to give you back a covariant list of documents.

So you can do result.Documents.OfType<Person>() after the previous call.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!