Spring数据elasticsearch查询嵌套对象数组

Spring data elasticsearch query array of nested objects

我有以下模型,文章,可以分配标签(如主题标签):

@Entity
@Table(name="article")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
        name="post_type",
        discriminatorType=DiscriminatorType.STRING
)
@DiscriminatorValue("text")
@Document(indexName="article", type="article")
public class Article implements Serializable {
    @org.springframework.data.annotation.Id
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="articleId")
    private int articleId;
    ... data for article...
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id", nullable = false)
    private User userId;

    @Field( type = FieldType.Nested)
    @ManyToMany(fetch = FetchType.EAGER)
    private Set<Tag> tags = new LinkedHashSet<>();Simple {tagName, tagId}

这是标签模型:

@Entity
@Table(name = "tag")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
        name="tag_type",
        discriminatorType=DiscriminatorType.STRING
)
@DiscriminatorValue("tag")
public class Tag {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id")
    private int tagId;

    @Column(name="tag_name")
    private String tagName;

使用 spring-data-elasticsearch 我正在尝试使用 tagName 进行查询。我尝试了以下 QueryBuilder 查询:

QueryBuilder builder = nestedQuery("tags.tagName", matchQuery("tags.tagName", tagName));
QueryBuilder builder = nestedQuery("tags", matchQuery("tags.tagName", tagName));

但最终出现以下堆栈错误:

org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to execute phase[dfs], all shards failed;
shardFailures {
    [10277 cVjTUK3mhchd - uBmw][article][0]: SearchParseException[[article][0]: from[0], size[10]: Parse Failure[Failed to parse source[{ "from": 0, "size": 10, "query": { "nested": { "query": { "match": { "tags.tagName": { "query": "holiday", "type": "boolean" } } }, "path": "tags.tagName" } }, "sort": [{ "number_likes": { "order": "desc" } }] }]]];
    nested: QueryParsingException[[article][nested] failed to find nested object under path[tags.tagName]]; } {
    [10277 cVjTUK3mhchd - uBmw][article][1]: SearchParseException[[article][1]: from[0], size[10]: Parse Failure[Failed to parse source[{ "from": 0, "size": 10, "query": { "nested": { "query": { "match": { "tags.tagName": { "query": "holiday", "type": "boolean" } } }, "path": "tags.tagName" } }, "sort": [{ "number_likes": { "order": "desc" } }] }]]];
    nested: QueryParsingException[[article][nested] failed to find nested object under path[tags.tagName]]; } {
    [10277 cVjTUK3mhchd - uBmw][article][2]: SearchParseException[[article][2]: from[0], size[10]: Parse Failure[Failed to parse source[{ "from": 0, "size": 10, "query": { "nested": { "query": { "match": { "tags.tagName": { "query": "holiday", "type": "boolean" } } }, "path": "tags.tagName" } }, "sort": [{ "number_likes": { "order": "desc" } }] }]]];
    nested: QueryParsingException[[article][nested] failed to find nested object under path[tags.tagName]]; } {
    [10277 cVjTUK3mhchd - uBmw][article][3]: SearchParseException[[article][3]: from[0], size[10]: Parse Failure[Failed to parse source[{ "from": 0, "size": 10, "query": { "nested": { "query": { "match": { "tags.tagName": { "query": "holiday", "type": "boolean" } } }, "path": "tags.tagName" } }, "sort": [{ "number_likes": { "order": "desc" } }] }]]];
    nested: QueryParsingException[[article][nested] failed to find nested object under path[tags.tagName]]; } {
    [10277 cVjTUK3mhchd - uBmw][article][4]: SearchParseException[[article][4]: from[0], size[10]: Parse Failure[Failed to parse source[{ "from": 0, "size": 10, "query": { "nested": { "query": { "match": { "tags.tagName": { "query": "holiday", "type": "boolean" } } }, "path": "tags.tagName" } }, "sort": [{ "number_likes": { "order": "desc" } }] }]]];
    nested: QueryParsingException[[article][nested] failed to find nested object under path[tags.tagName]]; }

我做错了什么?

尝试使用以下 SearchQuery 设置:

QueryBuilder builder = matchAllQuery();
FilterBuilder filterBuilder = boolFilter().must(termFilter("tags.tagName", tagName));

SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.filteredQuery(builder, filterBuilder))

您应该使用过滤查询并匹配所有实体。