如何使用Spring Data Solr搜索嵌套对象?

时间:2012-12-20 02:55:01

标签: spring solr spring-data spring-data-solr

我有两个这样的Java对象:

public class PSubject
{

    @Column 
    @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
    @org.apache.solr.client.solrj.beans.Field("name")
    private String name;       

    @Column 
    @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
    @org.apache.solr.client.solrj.beans.Field("type")
    private String type;        

    @Column 
    @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
    @org.apache.solr.client.solrj.beans.Field("uri")
    private String uri;

    @OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @IndexedEmbedded
    @org.apache.solr.client.solrj.beans.Field("attributes")
    private Set<PAttribute> attributes = new HashSet<PAttribute>();

    .....
}

@Entity
@Indexed
@Table(name="PAttribute")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class PAttribute extends PEntity
{

    private static final long serialVersionUID = 1L;

    @Column
    @Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES)
    @org.apache.solr.client.solrj.beans.Field("attr_name")
    private String name;

    @Column
    @Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES)
    @org.apache.solr.client.solrj.beans.Field("attr_value")
    private String value;

    .....

}

我的Spring Data Solr查询界面:

public interface DerivedSubjectRepository extends SolrCrudRepository<PSubject, String> {

    Page<PSubject> findByName(String name, Pageable page);

    List<PSubject> findByNameStartingWith(String name);

    Page<PSubject> findBy(Pageable page);

    @Query("name:*?0* or description:*?0* or type:*?0* or mac_address:*?0* or uri:*?0* or attributes:*?0*")
    Page<PSubject> find(String keyword,Pageable page);
    @Query("name:*?0* or description:*?0* or type:*?0* or mac_address:*?0* or uri:*?0* or attributes:*?0*")
    List<PSubject> find(String keyword);
}

我可以按名称,描述,类型和mac_address进行搜索,但不能按属性搜索任何结果。

更新

例如,当用户搜索“ipod”时,它可能表示主题的类型或主题的名称,或属性的名称或属性的值。我希望在一个请求中获得所有匹配的主题。我知道我可以在单独的查询中搜索属性对象。但这会使后端的代码变得复杂。

那么,我该如何搜索这个嵌套对象?

更新

我弄平了我的数据:

@Transient
    @Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
    @org.apache.solr.client.solrj.beans.Field("attrs")
    private String attrs;
public String getAttrs() {
        return attrs;
    }

    public void setAttrs(Set<PAttribute> attributes) {
        StringBuffer attrs = new StringBuffer();
        if(attributes==null) {
            attributes = this.getAttributes();
        }
        for(PAttribute attr:attributes){
            attrs.append(attr.getName()+" " + attr.getValue()).append(" ");
        }
        this.attrs  =attrs.toString();
    }

问题已解决。

1 个答案:

答案 0 :(得分:1)

IIRC无法在solr中存储嵌套数据结构 - 这取决于您如何将数据展平以适应例如。多值字段 - 有点难以了解您的架构。 见:http://lucene.472066.n3.nabble.com/Possible-to-have-Solr-documents-with-deeply-nested-data-structures-i-e-hashes-within-hashes-td4004285.html

您的索引中的数据如何,您是否看过spring-data-solr发送的http请求?