Nest(Elasticsearch)创建自动完成

时间:2015-07-22 08:38:48

标签: .net elasticsearch elasticsearch-net

我有一个索引文件的服务。任何文件都是这样的:

  [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_nametitletext
  • 自动完成功能只应建议tileobject_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();
}

0 个答案:

没有答案