弹弹性:什么是正确的方式使用嵌套的领域?

0

的问题

我是相当新的以. 我工作上的一个项目,我们需要寻找一个对象(提供),其中包含设置的两个(OfferTranslation). 我们的目标是使研究基于一些提供的领域,但也有很多OfferTranslation领域。 这里有一个经过压缩的版本的这两类:

Offer.class (注意,我带注释的定与@领域(type=FieldType.嵌套),所以我可以做嵌套的查询,以mentionned在官方文件) :

@org.springframework.data.elasticsearch.annotations.Document(indexName = "offer")
@DynamicMapping(DynamicMappingValue.False)
public class Offer implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    @Field(type = FieldType.Long)
    private Long id;

    @OneToMany(mappedBy = "offer", targetEntity = OfferTranslation.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    @JsonIgnoreProperties(
        value = { "pictures", "videos", "owner", "contexts", "offer", "personOfInterests", "followers" },
        allowSetters = true
    )
    @Field(type = FieldType.Nested, store = true)
    private Set<OfferTranslation> offersTranslations = new HashSet<>();


}

OfferTranslation.class :

@DynamicMapping(DynamicMappingValue.False)
public class OfferTranslation implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    @Field(type = FieldType.Long)
    private Long id;

    @NotNull
    @Size(max = 100)
    @Column(name = "title", length = 100, nullable = false)
    @Field(type = FieldType.Text)
    private String title;

    @NotNull
    @Size(max = 2000)
    @Column(name = "summary", length = 2000, nullable = false)
    @Field(type = FieldType.Text)
    private String summary;

    @Size(max = 2000)
    @Column(name = "competitor_context", length = 2000)
    @Field(type = FieldType.Text)
    private String competitorContext;

    @NotNull
    @Size(max = 2000)
    @Column(name = "description", length = 2000, nullable = false)
    @Field(type = FieldType.Text)
    private String description;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "maturity", nullable = false)
    @Field(type = FieldType.Auto)
    private RefMaturity maturity;

    @ManyToOne
    @Field(type = FieldType.Object, store = true)
    private RefLanguage language;

    @NotNull
    @Column(name = "created_at", nullable = false)
    @Field(type = FieldType.Date)
    private Instant createdAt;
}

预期的行为将是我可以让nestedQueries为这样:

QueryBuilder qBuilder = nestedQuery("offersTranslations",boolQuery().must(termQuery("offersTranslations.language.code",language)), ScoreMode.None);

但是我得到的是一个例外:未能创造查询:[嵌套]嵌套的对象的根据路径[offersTranslations]不是嵌套的类型"

编辑:我可以访问offersTranslations.语言。代码使用正常的查询(其中并没有真正困扰我的时刻). 但我还是真的不了解。

我说映该领域offersTranslations不是嵌套类型的你可以见上文,但自从我使用@领域(type=FieldType.嵌套)我真的不明白这种行为。 有人能解释一下吗?

{
  "offer" : {
    "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "categories" : {
          "properties" : {
            "id" : {
              "type" : "long"
            }
          }
        },
        "criteria" : {
          "properties" : {
            "id" : {
              "type" : "long"
            }
          }
        },
        "id" : {
          "type" : "long"
        },
        "keywords" : {
          "properties" : {
            "id" : {
              "type" : "long"
            }
          }
        },
        "offersTranslations" : {
          "properties" : {
            "competitorContext" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "createdAt" : {
              "type" : "date"
            },
            "description" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "id" : {
              "type" : "long"
            },
            "language" : {
              "properties" : {
                "code" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "id" : {
                  "type" : "long"
                },
                "name" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
              }
            },
            "maturity" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "state" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "summary" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "title" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "updatedAt" : {
              "type" : "date"
            }
          }
        },
        "units" : {
          "properties" : {
            "id" : {
              "type" : "long"
            }
          }
        }
      }
    }
  }
}
2

最好的答案

1

怎么样的指数映创建的? 它看起来并不像弹簧一步深入使用这些数据写这映射。 嵌套的类型 offerTranslations 缺少正如你看到的文本领域都有 .keyword 子场。 这看起来像数据插入索引的,而不具有定义的映射,并因此略任没有一个自动映射。 在这种情况下,值 @Field 注释都没有使用。

你需要有弹簧数据访问用户创建该指数的映射。 这将自动发生,如果指数不存在和你都在使用弹簧一步深入使用这些数据储存库,或者需要使用 IndexOperations.createWithMapping 能在你的申请。

另一件我注意到:它似乎您使用的是相同的实体类不同的春数据储存、混合的大量的注释。 你应该使用不同的实体对不同的商店。

2021-11-22 17:12:04

我已经试图删除的映射使用功能涵盖:删除提供/_mapping和重建索引我的提议。 但是,也许利用一个明确的"createWithMapping"可能更好地工作,我会让你知道,如果这有帮助! 谢谢你的好意
Aymane EL Jahrani

你好,所以我试图使用createWithMapping,但它似乎并不是简单的。 你能确定这是正确的方式使用它吗? github.com/spring-projects/spring-data-elasticsearch/blob/main/...
Aymane EL Jahrani
0

步骤来解决:

  • 使用功能涵盖确保删除<index_name>/_mapping
  • 看看你的实体类为对象的需要,可能是在一个@JsonIgnoreProperties
  • 确保你急切地加载你toMany关系的属性(否则的弹性不会创建一映射数据,你从没给过)
  • 从这一点经验,我要说,避免使用嵌套的领域,我看不出有任何优势的使用他们。 因此,检查如果是这样的你呢!
2021-11-25 23:17:34

弹簧一步深入使用这些数据不做任何事情的 @JsonIgnoreProperties 注释。 略任没有关系数据库并没有一概念的实体之间的关系。
P.J.Meisch

这是真的,但是春天的不当serialising数据。 这就是我怎么得到我的实体...
Aymane EL Jahrani

弹簧本身不这样做。 春天访问用户的数据不为例。 弹簧一步深入使用这些数据并没有这样做。 这些都是不同的春数据模块。
P.J.Meisch

图书馆fasterxml.杰克逊,和它的工作原理使用的注释在访问用户/休眠的实体。 这就是我的使用在这个项目的正如你可以看到,在我的代码...
Aymane EL Jahrani

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................