我想使用hibernate搜索从数据库返回记录。当搜索字符串实际上是一个短语时,我遇到了困难,否则事情看起来很好。
我有几个字段在其中进行搜索,我不知道运行时我应该在哪个字段进行查询,所以我创建了一个BooleanJunction并使用should
DSL方法,如下所示:
final Query spatialLuceneQuery = queryBuilder.spatial().within(200, Unit.KM).ofLatitude(latitude)
.andLongitude(longitude).createQuery();
bool.must(spatialLuceneQuery);
if (tag != null) {
if (isPhrase(tag.getTag())) {
final Query phraseTagsLuceneQuery = queryBuilder.phrase().withSlop(2).onField("tags").sentence(tag.getTag()).createQuery();
final Query phraseNameLuceneQuery = queryBuilder.phrase().onField("name").sentence(tag.getTag()).createQuery();
final Query phraseDescriptionLuceneQuery = queryBuilder.phrase().withSlop(3).onField("description").sentence(tag.getTag()).createQuery();
final Query phraseActiveSkillsLuceneQuery = queryBuilder.phrase().withSlop(1).onField("activeSkills").sentence(tag.getTag()).createQuery();
bool.should(phraseTagsLuceneQuery).should(phraseActiveSkillsLuceneQuery).should(phraseNameLuceneQuery).should(phraseDescriptionLuceneQuery);
} else {
final Query fuzzyLuceneQuery = queryBuilder.keyword().fuzzy().withEditDistanceUpTo(2).withPrefixLength(1).onFields("tags", "activeSkills","name","description").matching(tag.getTag()).createQuery();
bool.must(fuzzyLuceneQuery);
}
}
当isPhrase
方法返回true时,将返回表中的所有记录。如何将结果限制在相关结果中?
映射看起来像这样:
@Entity
@Table(name = "USER_INDEXED_SEARCH")
@Indexed
@Spatial(spatialMode = SpatialMode.RANGE)
@AnalyzerDef(name = "customAnalyzer", tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class) , filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = ASCIIFoldingFilterFactory.class),
@TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
@Parameter(name = "language", value = "Romanian") }) })
public class UserIndexedSearch extends DefaultLoggable {
private static final long serialVersionUID = -8021827814963060426L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@DocumentId
@Column(name = "USER_INDEXED_SEARCH_ID")
private Integer userIndexSearchId;
@Latitude
@Column(name = "LATITUDE")
private Double lat;
@Longitude
@Column(name = "LONGITUDE")
private Double lon;
@Column(name = "USER_ID")
private Integer userId;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Analyzer(definition = "customAnalyzer")
@Column(name = "TAGS")
private String tags;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Analyzer(definition = "customAnalyzer")
@Column(name = "ACTIVE_AREAS")
private String activeAreas;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Analyzer(definition = "customAnalyzer")
@Column(name = "ACTIVE_SKILLS")
private String activeSkills;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Analyzer(definition = "customAnalyzer")
@Column(name = "NAME")
private String name;
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
@Analyzer(definition = "customAnalyzer")
@Column(name = "DESCRIPTION")
private String description;
谢谢!
答案 0 :(得分:1)
当您发现自己有时因查询中显示的结果或未显示的结果而感到困惑时 在查询中.Hibernate搜索 允许您访问给定结果的Lucene Explanation对象(在给定查询中)。这个 对于Lucene用户来说,类被认为是相当先进的,但是可以提供很好的理解 得分对象。您有两种方法可以访问给定结果的说明对象:
使用投影的示例:
FullTextQuery ftQuery = s.createFullTextQuery( luceneQuery, Dvd.class )
.setProjection( FullTextQuery.DOCUMENT_ID, FullTextQuery.EXPLANATION, FullTextQuery.THIS );
@SuppressWarnings("unchecked") List<Object[]> results = ftQuery.list();
for (Object[] result : results) {
Explanation e = (Explanation) result[1];
display( e.toString() );
}