首先,我们使用的技术是 Neo4j、Spring 和 Spring Data Neo4j(都是最新的稳定版本)。
我们要求用户应该能够单独搜索我们所有的实体,并为所有实体提供所有全局搜索。我希望收集有关如何实施全局搜索的建议。下面是一些(简化的!)代码,显示了我如何查询我的实体。每个实体都使用自己的 Lucene 索引。
实体结构:
@NodeEntity
public abstract class BaseEntity {
@GraphId
private Long id;
}
@NodeEntity
public class A extends BaseEntity {
private static final String INDEX = "A_Index";
public static final String SEARCH_QUERY = "START a=node:" + INDEX + "({name}) RETURN a";
@Indexed(indexType = IndexType.FULLTEXT, indexName = INDEX)
@NotBlank
private String name;
}
@NodeEntity
public class B extends BaseEntity {
private static final String INDEX = "B_Index";
public static final String SEARCH_QUERY = "START b=node:" + INDEX + "({name}) RETURN b";
@Indexed(indexType = IndexType.FULLTEXT, indexName = INDEX)
@NotBlank
private String name;
}
存储库类:
@Repository
public interface ARepository extends GraphRepository<A> {
@Query(A.SEARCH_QUERY)
List<A> find(@Param("name") String name, Pageable pageable);
}
@Repository
public interface BRepository extends GraphRepository<B> {
@Query(B.SEARCH_QUERY)
List<B> find(@Param("name") String name, Pageable pageable);
}
我如何访问存储库类(再次,非常简化):
@Service
public class Service {
@Autowired
private ARepository repository;
public List<A> search(final String name) {
return repository.find("name:*" + name + "*", null);
}
}
因此,当您搜索单个实体类型时,这一切都很好。有人可以建议最好的方法是实现搜索每种实体类型的全局搜索吗?
我一直在思考的想法:
使用单个 Lucene 索引,而不是每个实体的索引。在 @Indexed 中提供 fieldName,如“a.name”或“b.name”。然后在单个查询中使用每个字段名称,例如“globalIndex:(a.name: foo OR b.name: foo )。(实际上不确定这是否可能)
为每种实体类型启动单独的搜索调用并组合结果。但是,很难根据索引分数来实现分页和排序。
我不担心性能,因为我们将使用相对较小的数据集。
最后一个问题:从 Cypher Lucene 查询返回的结果是否总是按其索引分数排序?如果没有,我应该如何在 SDN 中执行此操作?