Hello! I just began to use Hibernate search and it worlk for me but weird )))
i have next domain object:
Code:
@Table(name="ADVERT")
@Entity(name="Advert")
@Indexed
@AnalyzerDef(name = "customanalyzer",
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
@Parameter(name = "language", value = "Russian"), @Parameter(name="protected", value="test.txt")
})
},
charFilters ={@CharFilterDef(factory = HTMLStripCharFilterFactory.class),})
public class JpaAdvert implements Advert{
@Id
@GeneratedValue
private long id;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="category_id", referencedColumnName="id")
private JpaCategory category = new JpaCategory();
@Field
@Analyzer(definition="customanalyzer")
private String title="";
@Field
@Analyzer(definition="customanalyzer")
private String description="";
and i want to make a search of adverts which are similar by advert title. I do like:
Code:
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getJpaTemplate().getEntityManagerFactory().createEntityManager());
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(JpaAdvert.class).get();
org.apache.lucene.search.Query query = qb.keyword().onFields("title")
.matching(((SimilarAdvertFilter)filter).getAdvert().getTitle())
.createQuery();
// wrap Lucene query in a javax.persistence.Query
// javax.persistence.Query persistenceQuery =
Query persistenceQuery =
fullTextEntityManager.createFullTextQuery(query, JpaAdvert.class);
setRange(persistenceQuery, 0, 8);
// execute search
return persistenceQuery.getResultList();
and i receved the list of adverts! but most of them are not relevant to what i need....
For Example the title of advert is "Sell the best phone" and i get the advert with title like "The best animal", so the word "the" exist in the both titles, but the titles are very far from each other!
May be exist way to sort by some wight or scores to get first advert which are very relative!
Please help!