Elasticsearch utwórz pole łączenia (Nodejs)

Nov 21 2020

Mam następujące dokumenty:

export class CustomerServiceResultType{
  id: string;
  body:{customerRelation: string};
}

export class CustomerType{
  id: string;
  body:{name: string};
}

Chcę CustomerServiceResultTypemieć stosunek do CustomerTypez pola: customerRelation.

to jest moje mapowanie:

await this.elasticsearchService.indices.putMapping({
  "index": "db",
  "type": "CustomerServiceResultType",
  "body" : {
      "properties": {
        "customerRelation": {
          "type": "join",
          "relations": {
            "CustomerServiceResultType": "CustomerType"
          }
      }
    }
  }
});

Oto błąd, który otrzymuję:

[Nest] 421512   - 11/21/2020, 6:40:42 PM   [ExceptionsHandler] illegal_argument_exception +96414ms
ResponseError: illegal_argument_exception

Nie ma szczegółów dotyczących tego błędu ...

Dzięki

Odpowiedzi

1 JoeSorocin Nov 26 2020 at 20:54

Nie ma nic złego w swoim wniosku per se - Myślę, że po prostu wymaga jedną dodatkową opcję: include_type_name: true.

Jest to undefineddomyślnie w nodejs, ale jest wymagane w ES 7.x po stronie serwera. Więcej argumentów za tym jest tutaj .

Więc to powinno załatwić sprawę:

await client.indices.putMapping({
  include_type_name: true,
  index: "db",
  type: "CustomerServiceResultType",
  body : {
      properties: {
        customerRelation: {
          type: "join",
          relations: {
            CustomerServiceResultType: "CustomerType"
          }
      }
    }
  }
});

Indeksy typowane zostaną usunięte w wersji 8.x, więc najlepszym rozwiązaniem byłoby:

await client.indices.putMapping({
  index: "db",
  body : {
      properties: {
        customerRelation: {
          type: "join",
          relations: {
            CustomerServiceResultType: "CustomerType"
          }
      }
    }
  }
});

Przy okazji: typy maszynopisów tak naprawdę nie odgrywają tutaj roli, ponieważ ES jest interfejsem tylko w formacie JSON i chociaż istnieje przestarzały typeaspekt ES, te dwie koncepcje są bardzo odległe.