2012-12-20 184 views
3

我有兩個這樣的Java對象:如何使用Spring Data Solr搜索嵌套對象?

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; 

    ..... 

} 

而且我的春節,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

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請求?

+0

感謝您的回覆。而且我不知道如何使數據變平。 – Tom

+0

我放平了我的數據。問題已解決。謝謝 – Tom