简体   繁体   中英

Tag Cloud with Hibernate Search/Lucene?

Our web application has a database of product categories, within which are n number of products. Each product can have 0 or more tags associated with it. All of these objects are indexed using Hibernate Search (JPA). We're trying to build a tag cloud using these tags, to make it easier to see what are the most popular. Our code looks something like this...

FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(this.entityManager);

        SearchFactory searchFactory = fullTextEntityManager.getSearchFactory();
        IndexReader indexReader = searchFactory.getIndexReaderAccessor().open(ProductBean.class);
        TermStats[] termStats = null;
        int numResults = 500;
        String field = "tags.name";

        try {
            termStats = org.apache.lucene.misc.HighFreqTerms.getHighFreqTerms(indexReader, numResults, field,
                    new org.apache.lucene.misc.HighFreqTerms.DocFreqComparator());
        } finally {
            searchFactory.getIndexReaderAccessor().close(indexReader);
        }

This code works as we would expect, and provides us an array of TermStats objects that give us the scoring info necessary to build the tag cloud. However, now we would like to build tag clouds on a per-category basis. That is, we'd like to be able to filter our the results based on the category that a particular product is in. We haven't been able to figure out a way to do this, however, since IndexReader doesn't seem to allow us to attach any sort of filter.

Has anyone accomplished anything similar to this? Is there a better way to build a tag cloud using Hibernate Search?

I think you can try using the lucene facets ( https://lucene.apache.org/core/4_0_0/facet/org/apache/lucene/facet/doc-files/userguide.html )

They allow you to add some sort of "group by" and count on the search result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM