您可以通过client.TermVector(..) 执行此操作。这是一个简单的例子:
文档类:
public class MyDocument
{
public int Id { get; set; }
[ElasticProperty(TermVector = TermVectorOption.WithPositionsOffsets)]
public string Description { get; set; }
[ElasticProperty(Type = FieldType.Attachment, TermVector =TermVectorOption.WithPositionsOffsetsPayloads, Store = true, Index = FieldIndexOption.Analyzed)]
public Attachment File { get; set; }
}
索引一些测试数据:
var indicesOperationResponse = client.CreateIndex(indexName, c => c
.AddMapping<MyDocument>(m => m.MapFromAttributes()));
var myDocument = new MyDocument {Id = 1, Description = "test cat test"};
client.Index(myDocument);
client.Index(new MyDocument {Id = 2, Description = "river"});
client.Index(new MyDocument {Id = 3, Description = "test"});
client.Index(new MyDocument {Id = 4, Description = "river"});
client.Refresh();
通过 NEST 检索词条统计信息:
var termVectorResponse = client.TermVector<MyDocument>(t => t
.Document(myDocument)
//.Id(1) //you can specify document by id as well
.TermStatistics()
.Fields(f => f.Description));
foreach (var item in termVectorResponse.TermVectors)
{
Console.WriteLine("Field: {0}", item.Key);
var topTerms = item.Value.Terms.OrderByDescending(x => x.Value.TotalTermFrequency).Take(10);
foreach (var term in topTerms)
{
Console.WriteLine("{0}: {1}", term.Key, term.Value.TermFrequency);
}
}
输出:
Field: description
cat: 1
test: 2
希望对你有帮助。
更新
当我检查索引的映射时,有一件事情很有趣:
{
"my_index" : {
"mappings" : {
"mydocument" : {
"properties" : {
"file" : {
"type" : "attachment",
"path" : "full",
"fields" : {
"file" : {
"type" : "string"
},
"author" : {
"type" : "string"
},
"title" : {
"type" : "string"
},
"name" : {
"type" : "string"
},
"date" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"keywords" : {
"type" : "string"
},
"content_type" : {
"type" : "string"
},
"content_length" : {
"type" : "integer"
},
"language" : {
"type" : "string"
}
}
},
"id" : {
"type" : "integer"
}
}
}
}
}
}
没有关于词向量的信息。
当我通过感觉创建索引时:
PUT http://localhost:9200/my_index/mydocument/_mapping
{
"mydocument": {
"properties": {
"file": {
"type": "attachment",
"path": "full",
"fields": {
"file": {
"type": "string",
"term_vector":"with_positions_offsets",
"store": true
}
}
}
}
}
}
我能够检索术语统计信息。
希望我稍后会带着通过 NEST 创建的工作映射回来。
UPDATE2
基于Greg's answer 试试这个流畅的映射:
var indicesOperationResponse = client.CreateIndex(indexName, c => c
.AddMapping<MyDocument>(m => m
.MapFromAttributes()
.Properties(ps => ps
.Attachment(s => s.Name(p => p.File)
.FileField(ff => ff.Name(f => f.File).TermVector(TermVectorOption.WithPositionsOffsets)))))
);