Elasticsearch tạo trường tham gia (Nodejs)

Nov 21 2020

Tôi có các tài liệu sau:

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

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

Tôi muốn CustomerServiceResultTypecó một mối quan hệ với CustomerTypevới lĩnh vực này: customerRelation.

đây là bản đồ của tôi:

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

Đây là lỗi tôi nhận được:

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

Không có chi tiết về lỗi này ...

Cảm ơn

Trả lời

1 JoeSorocin Nov 26 2020 at 20:54

Không có gì sai trái với yêu cầu của bạn mỗi se - Tôi nghĩ rằng nó chỉ đòi hỏi một lựa chọn thêm: include_type_name: true.

Nó undefinedtheo mặc định trong nodejs nhưng được yêu cầu trong ES 7.x ở phía máy chủ. Thêm lý do đằng sau điều này là ở đây .

Vì vậy, điều này sẽ thực hiện thủ thuật:

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

Các chỉ mục đã nhập sẽ bị xóa trong 8.x vì vậy cách tiếp cận tốt nhất thực sự sẽ là:

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

BTW: các kiểu typecript của bạn không thực sự đóng vai trò gì ở đây vì ES là giao diện chỉ dành cho JSON và mặc dù có typekhía cạnh không được dùng nữa đối với ES, hai khái niệm này rất khác nhau.