Hibernate search : single search term with space - java

I am a newbie to hibernate search.
I am trying to implement a engine in which the results are exact matches.
My persistence class changes
#Field(index=Index.UN_TOKENIZED, store=Store.NO)
private String offerTitle;
Implementation changes
String[] offerFields = new String[] { "offerTitle"};
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_31,offerFields,new KeywordAnalyzer());
org.apache.lucene.search.Query query = parser.parse(queryString);
org.hibernate.Query offerHibQuery = fullTextSession.createFullTextQuery(query, Offer.class);
List<?> offerResults = offerHibQuery.list();
It works fine until the search term has space in it. When search term contains space, it gets no results.
For example: For the search term: "Comcast offer name" I get no results, and the query is broken into offerTitle:Comcast offerTitle:offer offerTitle:name
Is there any way to search for exact match with spaces?
I tried this:
QueryBuilder queryBuilder_1 = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Offer.class).get();
org.apache.lucene.search.Query offerCode_1 = queryBuilder_1.phrase().onField("offerTitle").sentence(queryString).createQuery();
org.hibernate.Query offerCodeHibQuery = fullTextSession.createFullTextQuery(offerCode_1);
List<?> offerCodeResults = offerCodeHibQuery.list();
It is still not working.

You are not tokenizing the field, so you need to search with a single, unanalyzed term. The simplest way to do that, I find, is to go straight to the Lucene APIs and just construct a TermQuery, like:
Query query = new TermQuery(new Term("offerTitle", queryString));
I think this should also work:
queryBuilder_1.keyword().onField("offerTitle").matching(queryString).createQuery();

queryBuilder_1.phrase().withSlop(0)
.onField("offerTitle").sentence(queryString).createQuery()

Related

How can I perform grouping on Solr search results in Spring?

The official Spring documentation provides the following example (slightly simplified here) for grouping results from a Solr query:
Field field = new SimpleField("popularity");
Query query = new SimpleQuery("inStock:true");
SimpleQuery groupQuery = new SimpleQuery(new SimpleStringCriteria("*:*"));
GroupOptions groupOptions = new GroupOptions()
.addGroupByField(field)
.addGroupByQuery(query);
groupQuery.setGroupOptions(groupOptions);
GroupPage<Product> page = solrTemplate.queryForGroupPage("collection-1", query, Product.class);
However, when I try this, it ignores the search conditions (inStock:true) and just performs the grouping on all results ("*:*")
There are several things about this example I had to modify to get it working. First of all, as you may have noticed, the argument query should actually be groupQuery in the queryForGroupPage call. I also removed the Query and put the actual search conditions in groupQuery. The working version is as follows, where groupByField is a SimpleField:
SimpleQuery groupQuery = new SimpleQuery(conditions);
GroupOptions groupOptions = new GroupOptions()
.addGroupByField(groupByField);
groupQuery.setGroupOptions(groupOptions);
return solrTemplate.queryForGroupPage(groupQuery, YourObjectHere.class);

Returning certain amount of documents from elasticsearch query in java

I am trying to limit the document size returned by my query.i want lets say 10 documents back only,any my query normally displays 22,how would i go buy setting a limit for the returned output. i am aware i can just limit the list size by creating a list and adding to that list however i want to do it on the query level.
My Query: Thanks in advance :)
ueryBuilder raceGenderQuery = QueryBuilders.boolQuery()
.must(termQuery("lep_etg_desc", "indian"))
.must(termQuery("lep_gen_desc", "male"));
Set<String> suburbanLocationSet = new HashSet<String>();
suburbanLocationSet.add("queensburgh");
suburbanLocationSet.add("umhlanga");
suburbanLocationSet.add("tongaat");
suburbanLocationSet.add("phoenix");
suburbanLocationSet.add("shallcross");
suburbanLocationSet.add("balito");
//Build the necessary location query.
QueryBuilder locationQuery = QueryBuilders.boolQuery().must(termsQuery("lep_suburb_home", suburbanLocationSet));
//Combine all Queries so that its filtered to get exact results.
FilteredQueryBuilder finalSearchQuery = QueryBuilders.filteredQuery(QueryBuilders.boolQuery().must(raceGenderQuery).must(locationQuery), FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter("lep_age").gte(25).lte(45)).must(FilterBuilders.rangeFilter("lep_max_income").gte(25000).lte(45000)));
//Run Query through elasticsearch iterating through documents in the traceps index for query matches.
List<Leads> finalLeadsList = new ArrayList<Leads>();
for (Leads leads : this.leadsRepository.search(finalSearchQuery)) {
finalLeadsList.add(leads);
}
I think this is what you want:
SearchResponse response = client.prepareSearch().setSearchType(SearchType.QUERY_THEN_FETCH).setSize(10).setQuery(finalSearchQuery).execute().get
You have to use QUERY_THEN_FETCH for it to return exactly size results because otherwise it gets size results from each shard.

How to query lucene for 2 index fields?

I'd like to execute queries with lucene. But the lookup should not only be based on the input, but also on a 2nd parameter.
Example: imagine the lucene index should contain citynames and countrycodes.
Now, during lookup I already know which country the desired cityname should be in.
SO I want to query the lucene index by cityname, but tell lucene to only look on the citynames where the countrycode matches.
It it possibel? If yes, how?
For a single attribute I would just set up the following:
QueryParser q = QueryParser(Version matchVersion, String f, Analyzer a)
Query q = queryParser.parse(input);
But how for 2 attributes?
Something like this should work. Untested but you should get the idea:
String countryCode = ....; // known in advance
QueryParser queryParser = new QueryParser(matchVersion, f, a);
Query cityNameQuery = queryParser.parse(inputWithCityName);
Query countryCodeQuery = queryParser.parse("+countrycode:" + countryCode);
BooleanQuery result = new BooleanQuery();
result.add(new BooleanClause(cityNameQuery, MUST));
result.add(new BooleanClause(countryCodeQuery, MUST));

Search keywords in database

I'm using Eclipselink and have a tricky problem regarding JPA NamedQueries.
My database table contains a column which is from type VARCHAR and stores a comma separated list of keywords as one String.
How can I create a NamedQuery in JPA to search theese keywords?
I'd like to give a list of Strings as a parameter and as a result I'd like to have a list of objects which keyword list contain one of the Strings from the parameter list.
Maybe like the following:
List<String> keywordList = new ArrayList<String>();
keywordList.add("test");
keywordList.add("car");
List<Object> result = em.createNamedQuery("findObjectByKeywords", Object.class)
.setParameter("keywords", keywordList)
.getResultList();
Unfortunately I'm not such a big database/SQL expert. Maybe someone of you can help me?
I hope you understand my problem.
Edit:
I am developing on Weblogic 10.3.6, which means I am not able to use JPA 2.0 features.
Edit2:
I managed to activate JPA 2.0 in my Weblogic Server with the help of Oracle Enterprise Pack for Eclipse. Problem solved, I think.
VALID FOR JPA2.0
As Bhesh commented a simple JPQL won't make it. The resulting SQL has to contain a where clause similar to following:
where keywords like '%keyword1%' or keywords like '%keyword2%' or ... or keywords like '%keywordN%'
This means: We need a loop here!
You could try to build a JPQL by yourself like Bhesh suggested in his first comment, though as he also stated it is not a brilliant idea. But don't worry - JPA provides also a Criteria API which comes handy in such situations. So, although you're not going to have a named query, you can still make it with JPA this way:
public List<YourEntity> findAllByKeywords(List<String> keywords){
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<YourEntity> query = builder.createQuery(YourEntity.class);
Root<YourEntity> root = query.from(YourEntity.class);
List<Predicate> predicates = new LinkedList<>();
for (String keyword : keywords) {
predicates.add(builder.like(root.<String>get("keywords"), "%" + keyword + "%"));
}
return entityManager.createQuery(
query.select(root).where(
builder.or(
predicates.toArray(new Predicate[predicates.size()])
)
))
.getResultList();
}
or (always slightly better with Guava)
public List<YourEntity> findAllByKeywords(List<String> keywords){
final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<YourEntity> query = builder.createQuery(YourEntity.class);
final Root<YourEntity> root = query.from(YourEntity.class);
return entityManager.createQuery(
query.select(root).where(
builder.or(
transform(keywords, toPredicateFunction(builder, root)).toArray(new Predicate[]{})
)
))
.getResultList();
}
private Function<String, Predicate> toPredicateFunction(final CriteriaBuilder builder, final Root<YourEntity> root) {
return new Function<String, Predicate>() {
#Override
public Predicate apply(String input) {
return builder.like(root.<String>get("keywords"), "%" + input + "%");
}
};
}

QueryDSL & Hibernate-Search with Lucene Analyzers

I configured Hibernate-Search to use my custom analyzer when indexing my entities. However when I try and search with QueryDSL's Hibernate-Search integration, it doesn't find entities, but if I use straight hibernate-search it finds something.
#AnalyzerDef(name = "customanalyzer",
tokenizer = #TokenizerDef(factory = StandardTokenizerFactory.class),
filters = {
#TokenFilterDef(factory = LowerCaseFilterFactory.class),
#TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
#Parameter(name = "language", value = "English")
})
})
#Analyzer(definition = "customanalyzer")
public abstract class Post extends BaseEntity {}
I indexed an entity with a title of "the quick brown fox jumped over the lazy dog".
These work…
List articlePosts = fullTextEntityManager.createFullTextQuery(queryBuilder.keyword().onFields("title").matching("jumped").createQuery(), ArticlePost.class).getResultList(); // list of 2
List articlePosts = fullTextSession.createFullTextQuery(queryBuilder.keyword().onFields("title").matching("jumped").createQuery(), ArticlePost.class).getResultList(); // list of 2
This does not…
SearchQuery<ArticlePost> query = new SearchQuery<ArticlePost>(this.entityManagerFactory.createEntityManager().unwrap(HibernateEntityManager.class).getSession(), post);
List articlePosts = query.where(post.title.contains("jumped")).list() // empty list
But a search with how it is likely stored in Lucene (probable result of SnowballPorter), then it works…
SearchQuery<ArticlePost> query = new SearchQuery<ArticlePost>(this.entityManagerFactory.createEntityManager().unwrap(HibernateEntityManager.class).getSession(), post);
List articlePosts = query.where(post.title.contains("jump")).list() // list of 2
So it seems like when using QueryDSL, that the analyzer isn't being run before it does the query. Can anyone confirm this is the problem, and is there anyway to have them automatically run before QueryDSL runs the query?
Regarding your question, the analyzer is applied per default when using the query DSL. In most cases it makes sense to use the same analyzer for indexing and searching. For this reason the analyzer is applied per default unless 'ignoreAnalyzer' is used.
Why your second example does not work I cannot tell you. SearchQuery is not part of the Hibernate Search or ORM API. It must be an internal class of your application. What's happening in this class? Which type of query is it using?

Categories

Resources