Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search nested object by using Spring Data Solr?

I have two such Java object:

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;

    .....

}

And my Spring Data Solr query interface:

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);
}

I can search any by name, description, type and mac_address, but can't search any result by attribute.

Update:

For example,when user search "ipod", it's probably means the type of subject or name of subject, or the name of attribute or the value of attribute. And I want get all the matched subject in one request. I know I can search the attribute object in a separate query. But that makes the code in the backend complex.

So, how can I search this nested object?

Update:

I flattened my data:

@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();
    }

The issue is resolved.

like image 525
Tom Avatar asked Mar 27 '26 08:03

Tom


1 Answers

IIRC it is not possible to store nested data structures in solr - it depends how you flatten your data to fit into an eg. multivalue field - a little hard not knowing your schema. see: http://lucene.472066.n3.nabble.com/Possible-to-have-Solr-documents-with-deeply-nested-data-structures-i-e-hashes-within-hashes-td4004285.html

How does the data look like in you index, and did you have a look at the http request sent by spring-data-solr?

like image 59
Christoph Strobl Avatar answered Mar 30 '26 01:03

Christoph Strobl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!