elasticsearch 7嵌套聚合文本关键字错误

时间:2019-07-04 09:59:48

标签: elasticsearch nest elasticsearch-aggregation

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

{
    "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放入查询中。嵌套客户端的配置方式,索引还是查询?

2 个答案:

答案 0 :(得分: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))
                        )
            ))
  );

希望有帮助。

答案 1 :(得分:0)

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

我在创建.DefaultMappingFor<ElasticWinnings>(u => u.IndexName("winnings")类时保留了ConnectionSettings,但是在返回弹性客户端之前添加了它:

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

的文档