简体   繁体   English

Elasticsearch - 订购搜索结果ASC

[英]Elasticsearch - Order search results ASC

having a problem with my elasticsearch. 我的弹性搜索有问题。

Setup: Having a Company-Class with the data field "companyName". 设置:使用数据字段“companyName”的公司类。 My search shall search and response all companys with the searched term. 我的搜索将使用搜索的术语搜索和回复所有公司。

If I try to sort via 如果我尝试排序

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

The data aren't sorted rightly - reference stackOverflow 数据未正确排序 - 参考stackOverflow

I tried the given solution, but if I set my companyName to "not_analyzed" I cant even search anymore for the comnpany name or like a beginning "goo" (google) So I tried to set up a multifield mapping, with a suffix, which isn't analyzed and one which is analyzed. 我尝试了给定的解决方案,但是如果我将我的companyName设置为“not_analyzed”,我甚至无法搜索公司名称或者像开头“goo”(google)那么我试图建立一个带有后缀的多字段映射,没有分析和分析。

My Index is set up like: 我的索引设置如下:

  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))))
        )

        )
    )
); 

My search looks like: 我的搜索看起来像:

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)

);

But that still doesn't work. 但那仍然行不通。 Any ideas? 有任何想法吗?

Thanks in advance. 提前致谢。

Update 1: 更新1:

For case insensitive sorting I added an custom analyzer: 对于不区分大小写的排序我添加了一个自定义分析器:

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"))))
                 )

          )
 );

This example is working, maybe it will put some light on your issue. 这个例子很有效,也许它可以解决你的问题。

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())
    );

Result of this query: 此查询的结果:

{
   "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"
            ]
         }
      ]
   }
}

Index mapping: 索引映射:

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

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM