Elasticsearch - 订购搜索结果ASC

时间:2015-06-18 08:37:57

标签: c# sorting elasticsearch nest

我的弹性搜索有问题。

设置:使用数据字段“companyName”的公司类。 我的搜索将使用搜索的术语搜索和回复所有公司。

如果我尝试排序

.Sort(x=> x.OnField(x => x.CompanyName).Descending())

数据未正确排序 - 参考stackOverflow

我尝试了给定的解决方案,但是如果我将我的companyName设置为“not_analyzed”,我甚至无法再搜索公司名称或者像一个开头“goo”(google) 所以我尝试设置一个带有后缀的多字段映射,后缀没有被分析,而是一个被分析的映射。

我的索引设置如下:

  client.CreateIndex(IndexName, c => c       
    .AddMapping<Exhibitor>(m =>m
        .MapFromAttributes()                        
        .Properties(o => o
        .MultiField(mf=>mf
            .Name(x=>x.CompanyName)             
            .Fields(fs => fs
                    .String(s=>s.Name(t=>t.CompanyName).Index(FieldIndexOption.Analyzed).Analyzer("standard"))
            .String(s=>s.Name(t=>t.CompanyName.Suffix("raw")).Index(FieldIndexOption.NotAnalyzed))))
        )

        )
    )
); 

我的搜索结果如下:

string SearchTerm ="my search term"
results = GetClient().Search<Company>(s => s 
    .Query(qa => qa 
       .MatchPhrasePrefix(m => m
       .OnField(f=>f.CompanyName)
      .Query(SearchTerm)
    ))

   .Sort(x => x.OnField(x => x.CompanyName.Suffix("raw")).Descending())

  .Size(maxResults).Skip(page * pageSize).Take(pageSize)

);

但那仍然行不通。 有什么想法吗?

提前致谢。

更新1:

对于不区分大小写的排序,我添加了一个自定义分析器:

var companyAnalyzer = new CustomAnalyzer
{
     Filter = new List<string> { "standard", "lowercase" },
      Tokenizer = "keyword"
};
client.CreateIndex(IndexName, c => c
       .Analysis(analysis => analysis
            .Analyzers(a => a
               .Add("companyanalyzer", companyAnalyzer)
             )
        )
         .AddMapping<Exhibitor>(m => m
              .MapFromAttributes()
              .Properties(o => o
                  .MultiField(mf => mf
                     .Name(x => x.CompanyName)
                         .Fields(fs => fs
                               .String(s => s.Name(t => t.CompanyName).Index(FieldIndexOption.Analyzed).Analyzer("standard"))
                               .String(s => s.Name(t => t.CompanyName.Suffix("raw")).Index(FieldIndexOption.Analyzed).Analyzer("companyanalyzer"))))
                 )

          )
 );

1 个答案:

答案 0 :(得分:1)

这个例子很有用,也许它可以解决你的问题。

var indicesResponse = client.DeleteIndex(descriptor => descriptor.Index(indexName));

client.CreateIndex(indexName, c => c
    .AddMapping<Exhibitor>(m => m
        .MapFromAttributes()
        .Properties(o => o
            .MultiField(mf => mf
                .Name(x => x.CompanyName)
                .Fields(fs => fs
                    .String(s => s.Name(t => t.CompanyName).Index(FieldIndexOption.Analyzed).Analyzer("standard"))
                    .String(s => s.Name(t => t.CompanyName.Suffix("raw")).Index(FieldIndexOption.NotAnalyzed))))
        )));


client.Index(new Exhibitor { Id = 1, CompanyName = "a test" });
client.Index(new Exhibitor { Id = 2, CompanyName = "b test" });
client.Index(new Exhibitor { Id = 3, CompanyName = "c test" }); 

client.Refresh();

string SearchTerm = "test";
var results = client.Search<Exhibitor>(s => s
    .Query(qa => qa
        .MatchPhrasePrefix(m => m
            .OnField(f => f.CompanyName)
            .Query(SearchTerm)
        ))
    .Sort(x => x.OnField(f => f.CompanyName.Suffix("raw")).Descending())
    );

此查询的结果:

{
   "took": 2,
   "timed_out": false,
   "_shards": {..}
   "hits": {
      "total": 3,
      "max_score": null,
      "hits": [
         {
            "_index": "my_index",
            "_type": "exhibitor",
            "_id": "3",
            "_score": null,
            "_source": {
               "id": 3,
               "companyName": "c test"
            },
            "sort": [
               "c test"
            ]
         },
         {
            "_index": "my_index",
            "_type": "exhibitor",
            "_id": "2",
            "_score": null,
            "_source": {
               "id": 2,
               "companyName": "b test"
            },
            "sort": [
               "b test"
            ]
         },
         {
            "_index": "my_index",
            "_type": "exhibitor",
            "_id": "1",
            "_score": null,
            "_source": {
               "id": 1,
               "companyName": "a test"
            },
            "sort": [
               "a test"
            ]
         }
      ]
   }
}

索引映射:

{
    "my_index" : {
        "mappings" : {
            "exhibitor" : {
                "properties" : {
                    "companyName" : {
                        "type" : "string",
                        "analyzer" : "standard",
                        "fields" : {
                            "raw" : {
                                "type" : "string",
                                "index" : "not_analyzed"
                            }
                        }
                    },
                    "id" : {
                        "type" : "integer"
                    }
                }
            }
        }
    }
}

希望它有所帮助。