【发布时间】:2013-08-14 16:41:00
【问题描述】:
我正在尝试在我的休眠查询中搜索特殊字符,我使用 QueryParser.escape(String searchTerm) 在所有特殊字符前面放置一个“\”字符以正确转义它们。
但是我发现用于标记化的标准分析器会从索引中删除这些特殊字符,因此即使您正确地转义了术语“abc-def”,如果您尝试搜索它,您也必须搜索“abc def” '。
那么我应该使用什么分析器/我应该如何指定分析器在索引时不删除特殊字符?
我的注释类和查询构建的 sn-p 如下:
@Entity
@Table(name="jobReq")
@Indexed
public class JobReq {
@Id
@DocumentId
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Field
@Column(name="jobId", nullable=false, unique=true)
private String jobId;
@Fields({@Field, @Field(name = "jobIdSort", analyze = Analyze.NO)})
@Column(name="jobIdSort", nullable=false, unique=true)
private String jobIdSort;
@Field
@Column(name="jobTitle", nullable=false)
private String jobTitle;
查询:
tempQ = (org.apache.lucene.search.Query) qb.keyword()
.wildcard()
.onField(allFields[i].getName().toString())
.matching(QueryParser.escape(termToFind) + "*")
.createQuery();
}
bq.add(new BooleanClause(tempQ, BooleanClause.Occur.SHOULD));
}
}
}
//wrap Lucene query in an org.hibernate.Query
hibQuery = fullTextSession.createFullTextQuery(bq, this.type).setSort(sort);
results = hibQuery.list();
System.out.println(bq);
fullTextSession.getTransaction().commit();
【问题讨论】:
-
你见过其他类似的问题吗,比如:stackoverflow.com/questions/3006524/…
-
我通过调用 QueryParser.escape() 来使用转义字符,它可以正确地转义查询,但是在数据库中标记数据的方式会从索引中删除特殊字符,所以它不是可搜索。我尝试使用 KeywordAnalyzer
@Analyzer(impl = KeywordAnalyzer.class) @Field() @Column(name="jobId", nullable=false, unique=true) private String jobId;但它在我的搜索中没有返回任何内容。此外,我没有使用字符串“手动”构建查询。
标签: hibernate lucene escaping character hibernate-search