1

i am working on Spring and Hibernate project and i am using Hibernate Lucene Search. it's searching fine but while displaying result it's shows like this (i.e if i search for title)

 [id: 10 | title:easylib, id: 11 | title:IBM, id: 12 | title:Wipro]

but what i wanted is it should show only 'Wipro', not it's id or something else

this is my code(in POJO class for title) :

@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
@Column(name = "title", nullable = false, length = 150)
public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}

this is in my DAO Class

private void doIndex() throws InterruptedException {
            Session session = getSession();

            FullTextSession fullTextSession = Search.getFullTextSession(session);
            fullTextSession.createIndexer().startAndWait();

        }

       private List<CatalogueBase> searchTitle(String queryString) {
            Session session = getSession();
            FullTextSession fullTextSession = Search.getFullTextSession(session);       
            QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(CatalogueBase.class).get();
            org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("subTitle","publishedplace","title").matching(queryString).createQuery();   
            org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, CatalogueBase.class);           
            List<CatalogueBase> contactList = fullTextQuery.list();
            return contactList;
        }

    @Override
    public List<CatalogueBase> getSearchDao(String search) throws InterruptedException {
        doIndex();
        List<CatalogueBase> result = searchTitle(search); 
        return result;
    }

3 Answers 3

1

I got Answer, my mistake was

Before:

@Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder("id: ").append(this.getId()).append(" | title:").append(this.getTitle());
        return stringBuilder.toString();
    }

After:

@Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder().append(this.getTitle());
        return stringBuilder.toString();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, if that's what you are after you should really use the Projection as I suggested in the accepted answer.
0

The query code is not rendering this output you are "displaying". I think you might be simply invoking the toString() method on the resulting List<CatalogueBase>. You say you're getting the right results from the query, now just transform the CatalogueBase instances.

An alternative to loading CatalogueBase instances is to use projections:

org.hibernate.Query fullTextQuery = fts.createFullTextQuery(luceneQuery, CatalogueBase.class);
fullTextQuery.setProjection("title");
List contactList = fullTextQuery.list();

Comments

0

Make sure to add this annotation @Field(store = Store.YES) on your field and then use fullTextQuery.setProjection("title"); then only it will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.