Ошибка ключевого слова в тексте агрегации гнезд 7 - PullRequest
0 голосов
/ 04 июля 2019

У меня есть индекс со следующими сопоставлениями:

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

1 голос
/ 04 июля 2019

Вам нужно немного изменить запрос, чтобы NESt использовал поле keyword вместо text, это можно сделать с помощью метода расширения .Suffix. Ссылка на документы .

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

Надеюсь, это поможет.

0 голосов
/ 04 июля 2019

Я нашел решение добавить [Keyword] к свойству PlayerId в классе ElasticWinnings.

Я сохранил .DefaultMappingFor<ElasticWinnings>(u => u.IndexName("winnings") при создании класса ConnectionSettings, но добавил его до возвращения клиента 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

...