gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:25:05 25 4
gpt4 key购买 nike

我有两个这样的 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查询接口(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);
}

我可以按名称、描述、类型和 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();
}

问题已解决。

最佳答案

IIRC 不可能在 solr 中存储嵌套数据结构——这取决于您如何展平数据以适应 eg。多值字段 - 不知道您的架构有点困难。参见: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请求?

关于spring - 如何使用 Spring Data Solr 搜索嵌套对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13964595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com