更新时间:2023-02-18 23:41:38
这是你如何重现自己附加的自动完成示例文章。
我的简单类(我们将在上实现自动完成名称
属性)
public class Document
{
public int Id {get;组; }
public string Name {get;组; }
}
为了在NEST中创建多字段映射,我们必须以这种方式定义映射:
var indexesOperationResponse = 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。后缀(autocomplete))IndexAnalyzer(shingle_analyzer)))))))
.Analysis(a => a
.Analyzers(b => b.Add(shingle_analyzer新的CustomAnalyzer
{
Tokenizer =standard,
Filter = new List< string> {smallcase,shingle_过滤器}
}))
.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)));
此查询将获得我们
震颤2:余震
颤抖3:回到完美
颤抖4:传奇开始
Tron
真血
True Grit
希望这将有助于您。
来自NEST的人们准备了关于NEST和弹性搜索的伟大的教程。有关建议的部分内容,对于你。
I am trying to create some completion suggesters on some of my fields. My document class looks like this:
[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>(); } }
}
And here is how I am trying to create the completion fields
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;
}
My problem is that this is only creating a single completion field named "comp". I was under the impression that this will create two completion fields, one named filename.comp and the other named tags.comp.
I then tried the answer on this SO question but this complicated the matter even worse as now my two fields were mapped as a completion field only.
Just to be clear, I want to create a multi-field (field) that has a data, sort and completion fileds. Much like the one in this example
This is how you can reproduce auto-complete example from attached by you article.
My simple class(we are going to implement auto-complete on Name
property)
public class Document
{
public int Id { get; set; }
public string Name { get; set; }
}
To create multi field mapping in NEST we have to define mapping in such manner:
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
}))));
Let's index some documents:
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();
Now, we are ready to write prefix query :)
var searchResponse = client.Search<Document>(s => s
.Query(q => q
.Prefix("name.autocomplete", "tr"))
.SortAscending(sort => sort.Name.Suffix("sortable")));
This query will get us
Tremors 2: Aftershocks
Tremors 3: Back to Perfection
Tremors 4: The Legend Begins
Tron
True Blood
True Grit
Hope this will help you.
Recently, guys from NEST prepared great tutorial about NEST and elasticsearch. There is a part about suggestions, it should be really useful for you.