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.