如何使用Spring Data Solr搜索嵌套对象?
我有两个这样的Java对象:如何使用Spring Data Solr搜索嵌套对象?,spring,solr,spring-data,spring-data-solr,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, anal
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;
.....
}
公共类PSObject
{
@纵队
@字段(index=index.YES,analyze=analyze.YES,store=store.NO)
@org.apache.solr.client.solrj.beans.Field(“名称”)
私有字符串名称;
@纵队
@字段(index=index.YES,analyze=analyze.YES,store=store.NO)
@org.apache.solr.client.solrj.beans.Field(“类型”)
私有字符串类型;
@纵队
@字段(index=index.YES,analyze=analyze.YES,store=store.NO)
@org.apache.solr.client.solrj.beans.Field(“uri”)
私有字符串uri;
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@指数化
@org.apache.solr.client.solrj.beans.Field(“属性”)
私有集属性=新HashSet();
.....
}
@实体
@索引
@表(name=“PAttribute”)
@继承(策略=继承类型。每个类的表)
公共类属性扩展了奔腾
{
私有静态最终长serialVersionUID=1L;
@纵队
@字段(index=index.YES,analyze=analyze.YES,store=store.YES)
@org.apache.solr.client.solrj.beans.Field(“attr\u name”)
私有字符串名称;
@纵队
@字段(index=index.YES,analyze=analyze.YES,store=store.YES)
@org.apache.solr.client.solrj.beans.Field(“attr_值”)
私有字符串值;
.....
}
和我的Spring数据解决方案查询界面:
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);
}
公共接口DerivedSubjectRepository扩展了SolrCrudeRepository{
页面findByName(字符串名称,可分页页面);
列出findByNameStartingWith(字符串名称);
页面查找方式(可分页页面);
@查询(“名称:*?0*或说明:*?0*或类型:*?0*或mac_地址:*?0*或uri:*?0*或属性:*?0*”)
页面查找(字符串关键字,可分页页面);
@查询(“名称:*?0*或说明:*?0*或类型:*?0*或mac_地址:*?0*或uri:*?0*或属性:*?0*”)
列表查找(字符串关键字);
}
我可以按名称、描述、类型和mac_地址搜索任何结果,但不能按属性搜索任何结果
更新:
例如,当用户搜索“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();
}
@Transient
@字段(index=index.YES,analyze=analyze.YES,store=store.NO)
@org.apache.solr.client.solrj.beans.Field(“attrs”)
私有字符串属性;
公共字符串getAttrs(){
返回属性;
}
公共void setAttrs(设置属性){
StringBuffer attrs=新的StringBuffer();
如果(属性==null){
attributes=this.getAttributes();
}
for(属性属性:属性){
attrs.append(attr.getName()+“”+attr.getValue()).append(“”);
}
this.attrs=attrs.toString();
}
问题已经解决。IIRC不可能在solr中存储嵌套的数据结构-这取决于您如何将数据展平以适应例如多值字段-不知道您的模式有点困难。 见:
索引中的数据是什么样子的?您是否查看了spring data solr发送的http请求?谢谢您的回复。我不知道如何将数据展平。