我有一个索引文件的服务。任何文件都是这样的:
[ElasticType(IdProperty = "r_object_id", Name = "dm_document")]
public class dm_document {
public string r_object_id { get; set; }
public string i_chronicle_id { get; set; }
public string object_name { get; set; } // title
public string title { get; set; } // also a title (don't ask :p )
public string text { get; set; } // contents of the document
public string Docbase { get; set; }
}
所以我想知道如何优化这些文档的索引。
因此,在设置我的分析器/搜索查询时,我想到了以下几点:
- 搜索应该在
object_name
,title
和上搜索text
。 tile
自动完成应该只建议来自和的完整单词object_name
我有以下索引,但我对弹性搜索不是很有经验,所以这可能不好:
client.CreateIndex("testindex", index => index
.Analysis(analysis => analysis
.Analyzers(a => a
.Add(
"autocomplete",
new Nest.CustomAnalyzer() {
Tokenizer = "edgeNGram",
Filter = new string[] { "lowercase" }
}
)
)
.Tokenizers(t => t
.Add(
"edgeNGram",
new Nest.EdgeNGramTokenizer() {
MinGram = 1,
MaxGram = 20
}
)
)
)
.AddMapping<dm_document>(tmd => tmd
.Properties(props => props
.MultiField(p => p
.Name(t => t.object_name)
.Fields(tf => tf
.String(s => s
.Name(t => t.object_name)
.Index(Nest.FieldIndexOption.NotAnalyzed)
)
.String(s => s
.Name(t => t.object_name.Suffix("autocomplete"))
.Index(Nest.FieldIndexOption.Analyzed)
.IndexAnalyzer("autocomplete")
)
.String(s => s
.Name(t => t.title)
.Index(Nest.FieldIndexOption.NotAnalyzed)
)
.String(s => s
.Name(t => t.title.Suffix("autocomplete"))
.Index(Nest.FieldIndexOption.Analyzed)
.IndexAnalyzer("autocomplete")
)
.String(s => s
.Name(t => t.text)
.Index(Nest.FieldIndexOption.Analyzed)
.IndexAnalyzer("snowball")
)
)
)
)
)
);
我使用了以下 API 函数:
// GET api/autocomplete
public IEnumerable<object> Get(string search = "", int from = 0, int size = 50) {
var c = new ElasticClient();
var results = c.Search<dm_document>(s => s
.From(from)
.Size(size)
.Query(q => q.Term(d => d.object_name.Suffix("autocomplete"), search))
);
return results.Documents.Select(d => new { d.title, d.r_object_id }).ToList();
}
和
// GET api/search
public List<dm_document> Get(string search = "", int from = 0, int size = 50) {
var indexservice = new IndexService();
var c = new ElasticClient();
var result = c.Search<dm_document>(s => s
.From(from)
.Size(size)
.Query(q => q.Term(d => d.text, search))
);
return result.Documents.ToList();
}