简体   繁体   English

Spring Data Elasticsearch嵌套查询

[英]Spring Data Elasticsearch Nested query

I am using Spring Data Elasticsearch support via jHipster, and am trying to learn a bit more about using search as it relates to extending some new relationships in my database. 我正在通过jHipster使用Spring Data Elasticsearch支持,并试图学习更多有关使用搜索的知识,因为它与扩展数据库中的一些新关系有关。

I've got a new nested relationship with it's own index: 我有一个新的嵌套关系,它具有自己的索引:

@Entity
@Table(name = "friends")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "friend")
public class Friend {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne
    @Field(type=FieldType.Nested, index=FieldIndex.analyzed, store=true)
    private User friend1;

    @OneToOne
    @Field(type=FieldType.Nested, index=FieldIndex.analyzed, store=true)
    private User friend2;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public User getFriend1() {
        return friend1;
    }

    public void setFriend1(User friend1) {
        this.friend1 = friend1;
    }

    public User getFriend2() {
        return friend2;
    }

    public void setFriend2(User friend2) {
        this.friend2 = friend2;
    }

}

I want to filter the results to a specific user "friend1.id", and than search for matching details on the nested document? 我想将结果过滤到特定用户“ friend1.id”,而不是在嵌套文档中搜索匹配的详细信息?

     StreamSupport
        .stream(friendSearchRepository.search(
            QueryBuilders.boolQuery()               
.must(QueryBuilders.matchQuery("friend1.id",SecurityUtils.getCurrentUserId()))
            .must(QueryBuilders.matchQuery("friend2.login",query))).spliterator(), false)
            .collect(Collectors.toList());

This doesn't seem to return any results though? 这似乎没有返回任何结果吗?

To answer your question about how to search this, an Elasticsearch query can be constructed using QueryBuilders . 为了回答有关如何进行搜索的问题,可以使用QueryBuilders构造一个Elasticsearch查询。 For example, querySearchQuery can be combined with other types of queries using a boolQuery : 例如, querySearchQuery可以与其它类型的使用的查询被组合boolQuery

userSearchRepository.search( QueryBuilders.boolQuery() .must(queryStringQuery(query)) .must(termsQuery("firstName", "John")))

This will search for User s where the query matches and firstName is John . 这将搜索查询匹配且firstNameJohn User

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM