そのような Java オブジェクトが 2 つあります。
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 クエリ インターフェイス:
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」と検索した場合、それはおそらく件名の種類または件名、または属性の名前または属性の値を意味します。そして、一致したすべての件名を 1 回のリクエストで取得したいと考えています。別のクエリで属性オブジェクトを検索できることはわかっています。しかし、それはバックエンドのコードを複雑にします。
では、このネストされたオブジェクトを検索するにはどうすればよいでしょうか?
更新:
データを平坦化しました:
@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();
}
問題は解決しました。