【问题标题】:elasticsearch 7 nest aggregation text keyword errorelasticsearch 7嵌套聚合文本关键字错误
【发布时间】:2019-11-15 01:08:51
【问题描述】:

我有一个包含以下映射的索引:

{
    "winnings": {
        "mappings": {
            "properties": {
                "handId": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "playerId": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "value": {
                    "type": "float"
                }
            }
        }
    }
}

从类中生成:

public class ElasticWinnings
    {
        public Guid Id { get; set; }
        public Guid HandId { get; set; }
        public Guid PlayerId { get; set; }
        public decimal Value { get; set; }
    }

我使用 ConnectionSettings 在嵌套中创建了它:

.DefaultMappingFor<ElasticWinnings>(u =>
    u.IndexName("winnings")
         .IdProperty(x => x.Id)
 );

当我尝试运行以下查询时:

var result = _client.Search<ElasticWinnings>(s =>
    s.Aggregations(a =>
            a.Terms("term_Agg", t =>
                t.Field(f => f.PlayerId)
                    .Aggregations(aa =>
                        aa.Sum("sum", sum => 
                            sum.Field(f => f.Value))
                        )
            ))
  );

我得到了 400 的回复,但出现了错误:

type: illegal_argument_exception Reason: "Fielddata is disabled on text fields by default

它创建了这个查询:

{  
   "aggs":{  
      "term_Agg":{  
         "aggs":{  
            "sum":{  
               "sum":{  
                  "field":"value"
               }
            }
         },
         "terms":{  
            "field":"playerId"
         }
      }
   }
}

如果我将该查询更改为:

{  
   "aggs":{  
      "term_Agg":{  
         "aggs":{  
            "sum":{  
               "sum":{  
                  "field":"value"
               }
            }
         },
         "terms":{  
            "field":"playerId.keyword"
         }
      }
   }
}

并在邮递员中使用它,它可以工作。

我不确定为什么它没有将.keyword 放入查询中。是嵌套客户端的配置方式,索引还是查询?

【问题讨论】:

    标签: elasticsearch nest elasticsearch-aggregation


    【解决方案1】:

    您需要稍微更改您的查询以告诉 NEST 使用 keyword 字段而不是 text,您可以使用 .Suffix 扩展方法来做到这一点。 Link to docs.

    var result = _client.Search<ElasticWinnings>(s =>
        s.Aggregations(a =>
                a.Terms("term_Agg", t =>
                    t.Field(f => f.PlayerId.Suffix("keyword"))
                        .Aggregations(aa =>
                            aa.Sum("sum", sum => 
                                sum.Field(f => f.Value))
                            )
                ))
      );
    

    希望对您有所帮助。

    【讨论】:

    • 谢谢罗伯。我最终找到了一个不同的解决方案,但很高兴了解这些扩展
    【解决方案2】:

    我找到的解决方案是将[Keyword] 添加到ElasticWinnings 类中的PlayerId 属性中。

    我在创建ConnectionSettings 类时保留了.DefaultMappingFor&lt;ElasticWinnings&gt;(u =&gt; u.IndexName("winnings"),但在返回Elastic 客户端之前添加了这个:

    var client = new ElasticClient(settings);
    
    client.Indices.Create("winnings", c =>
        c.Map<ElasticWinnings>(m => m.AutoMap())
    );
    

    没有添加上面的部分,它没有应用属性。这将我的映射 (http://localhost:9200/winnings/_mappings) 更改为

    {
        "winnings": {
            "mappings": {
                "properties": {
                    "handId": {
                        "type": "keyword"
                    },
                    "id": {
                        "type": "keyword"
                    },
                    "playerId": {
                        "type": "keyword"
                    },
                    "value": {
                        "type": "double"
                    }
                }
            }
        }
    }
    

    这是关于设置映射https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/fluent-mapping.html的文档

    【讨论】:

      猜你喜欢
      • 2015-07-30
      • 2021-12-14
      • 2015-11-18
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 2017-09-13
      • 2021-04-25
      • 2023-04-03
      相关资源
      最近更新 更多