Search in sources :

Example 1 with Tag

use of com.github.chipolaris.bootforum.domain.Tag in project BootForum by chipolaris.

the class IndexService method createDiscussion.

private Discussion createDiscussion(Document doc) {
    Discussion discussion = new Discussion();
    discussion.setId(Long.valueOf(doc.getField("id").stringValue()));
    discussion.setCreateBy(doc.getField("createBy").stringValue());
    discussion.setCreateDate(new Date(doc.getField("createDate").numericValue().longValue()));
    discussion.setTitle(doc.getField("title").stringValue());
    discussion.setClosed(Boolean.valueOf(doc.getField("closed").stringValue()));
    /* construct tag list */
    List<Tag> tags = new ArrayList<>();
    discussion.setTags(tags);
    IndexableField[] tagFields = doc.getFields("tag");
    if (tagFields.length > 0) {
        for (IndexableField tagField : tagFields) {
            Tag tag = new Tag();
            tag.setLabel(tagField.stringValue());
            tags.add(tag);
        }
    }
    return discussion;
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) ArrayList(java.util.ArrayList) Tag(com.github.chipolaris.bootforum.domain.Tag) Discussion(com.github.chipolaris.bootforum.domain.Discussion) Date(java.util.Date)

Example 2 with Tag

use of com.github.chipolaris.bootforum.domain.Tag in project BootForum by chipolaris.

the class IndexService method searchSimilarDiscussions.

public ServiceResponse<SearchDiscussionResult> searchSimilarDiscussions(Discussion discussion, int first, int pageSize) {
    ServiceResponse<SearchDiscussionResult> response = new ServiceResponse<>();
    IndexSearcher indexSearcher = null;
    try {
        indexSearcher = discussionSearcherManager.acquire();
        BoostQuery boostTagQuery = null;
        String tagSearchString = "";
        for (Tag tag : discussion.getTags()) {
            tagSearchString += tag.getLabel() + " ";
        }
        if (!"".equals(tagSearchString)) {
            Query tagQuery = new QueryParser("tag", discussionAnalyzer).parse(tagSearchString);
            // tag match will get scoring boost of 0.35
            boostTagQuery = new BoostQuery(tagQuery, 0.35f);
        }
        Query titleQuery = new QueryParser("title", discussionAnalyzer).parse(QueryParser.escape(discussion.getTitle()));
        Query notClosedQuery = new QueryParser("closed", discussionAnalyzer).parse(String.valueOf(Boolean.FALSE));
        // title match will get scoring boost of 0.50 if tag is available, otherwise, it would get .85
        BoostQuery boostTitleQuery = new BoostQuery(titleQuery, boostTagQuery != null ? 0.50f : 0.85f);
        // not closed match will get scoring boost of 0.15
        BoostQuery boostNotClosedQuery = new BoostQuery(notClosedQuery, 0.15f);
        /*
			 * Give a small boost (0.001f) to the "MoreRecent" feature based on "Id" attribute of the comment record.
			 * That is: greater id (more recent) has a small boost in scoring
			 */
        BoostQuery boostedQuery = new BoostQuery(FeatureField.newSaturationQuery("features", "MoreRecent"), 0.001f);
        /*
			 * First, create a query that the searchString must match
			 * Note from Lucene Javadoc for {@link org.apache.lucene.search.BooleanClause Occur}
			 * Use this operator for clauses that should appear in the matching documents. 
			 * For a BooleanQuery with no MUST clauses one or more SHOULD clauses must match 
			 * a document for the BooleanQuery to match.
			 */
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.add(boostTitleQuery, Occur.MUST).add(boostNotClosedQuery, Occur.SHOULD);
        if (boostTagQuery != null) {
            builder.add(boostTagQuery, Occur.SHOULD);
        }
        Query matchedQuery = builder.build();
        /*
			 * Combine the matchedQuery with the boostedQuery
			 */
        Query query = new BooleanQuery.Builder().add(matchedQuery, Occur.MUST).add(boostedQuery, Occur.MUST).build();
        TopScoreDocCollector collector = TopScoreDocCollector.create(maxDiscussionSearchResults, maxDiscussionSearchResults);
        indexSearcher.search(query, collector);
        TopDocs topDocs = collector.topDocs(first, pageSize);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        long totalHits = topDocs.totalHits.value;
        List<Discussion> results = new ArrayList<Discussion>();
        for (ScoreDoc scoreDoc : scoreDocs) {
            int docNumber = scoreDoc.doc;
            Document document = indexSearcher.doc(docNumber);
            results.add(createDiscussion(document));
        }
        SearchDiscussionResult searchResult = new SearchDiscussionResult();
        searchResult.setTotalHits(totalHits);
        searchResult.setDiscussions(results);
        response.setDataObject(searchResult);
    } catch (IOException e) {
        logger.error("Unable to search similar to Discussion with id " + discussion.getId(), e);
        response.setAckCode(AckCodeType.FAILURE);
    } catch (ParseException e) {
        logger.error("Unable to search similar to Discussion with id " + discussion.getId(), e);
        response.setAckCode(AckCodeType.FAILURE);
    } finally {
        try {
            commentSearcherManager.release(indexSearcher);
            // make sure not to use this indexSearcher again:
            // ref: http://blog.mikemccandless.com/2011/09/lucenes-searchermanager-simplifies.html
            indexSearcher = null;
        } catch (IOException e) {
            logger.error("Error trying to release indexSearcher", e);
        }
    }
    return response;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TopScoreDocCollector(org.apache.lucene.search.TopScoreDocCollector) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) BoostQuery(org.apache.lucene.search.BoostQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Tag(com.github.chipolaris.bootforum.domain.Tag) ParseException(org.apache.lucene.queryparser.classic.ParseException) Discussion(com.github.chipolaris.bootforum.domain.Discussion)

Example 3 with Tag

use of com.github.chipolaris.bootforum.domain.Tag in project BootForum by chipolaris.

the class TagService method getActiveTags.

@Transactional(readOnly = true)
@Cacheable(value = CachingConfig.ACTIVE_TAGS, key = "'tagService.getActiveTags'")
public ServiceResponse<List<Tag>> getActiveTags() {
    ServiceResponse<List<Tag>> response = new ServiceResponse<>();
    List<Tag> tags = genericDAO.getEntities(Tag.class, Collections.singletonMap("disabled", Boolean.FALSE), new SortSpec("sortOrder", SortSpec.Direction.DESC));
    response.setDataObject(tags);
    return response;
}
Also used : List(java.util.List) Tag(com.github.chipolaris.bootforum.domain.Tag) SortSpec(com.github.chipolaris.bootforum.dao.SortSpec) Cacheable(org.springframework.cache.annotation.Cacheable) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Tag

use of com.github.chipolaris.bootforum.domain.Tag in project BootForum by chipolaris.

the class DataInitializer method createBulletinTag.

private void createBulletinTag(Discussion discussion) {
    Tag tag = new Tag();
    tag.setLabel("Bulletin");
    // DodgerBlue
    tag.setColor("1e90ff");
    tag.setIcon("pi pi-book");
    genericService.saveEntity(tag);
    discussion.setTags(List.of(tag));
    genericService.updateEntity(discussion);
    applicationEventPublisher.publishEvent(new DiscussionUpdateEvent(this, discussion));
}
Also used : DiscussionUpdateEvent(com.github.chipolaris.bootforum.event.DiscussionUpdateEvent) Tag(com.github.chipolaris.bootforum.domain.Tag)

Example 5 with Tag

use of com.github.chipolaris.bootforum.domain.Tag in project BootForum by chipolaris.

the class HomePage method onLoad.

public void onLoad() {
    this.allTags = tagService.getActiveTags().getDataObject();
    this.displayOption = systemConfigService.getDisplayOption().getDataObject();
    if (displayOption.isShowDiscussionsForTag()) {
        this.displayTags = displayOption.getDisplayTags();
        for (Tag tag : displayTags) {
            int numDiscussions = displayOption.getNumDiscussionsPerTag();
            // get Discussions for tag, if numDiscussions is 0 or less, default to 5
            tag.setDiscussions(tagService.getDiscussionsForTag(tag, numDiscussions > 0 ? numDiscussions : 5).getDataObject());
        }
    }
    if (displayOption.isShowMostViewsDiscussions()) {
        this.mostViewsDiscussions = discussionService.getMostViewsDiscussions(DAY_BACK, displayOption.getNumMostViewsDiscussions()).getDataObject();
    }
    if (displayOption.isShowMostCommentsDiscussions()) {
        this.mostCommentsDiscussions = discussionService.getMostCommentsDiscussions(DAY_BACK, displayOption.getNumMostCommentsDiscussions()).getDataObject();
    }
    if (displayOption.isShowMostRecentDiscussions()) {
        this.latestDiscussions = discussionService.getLatestDiscussions(displayOption.getNumMostRecentDiscussions()).getDataObject();
    }
}
Also used : Tag(com.github.chipolaris.bootforum.domain.Tag)

Aggregations

Tag (com.github.chipolaris.bootforum.domain.Tag)10 Discussion (com.github.chipolaris.bootforum.domain.Discussion)3 DiscussionUpdateEvent (com.github.chipolaris.bootforum.event.DiscussionUpdateEvent)2 ArrayList (java.util.ArrayList)2 Document (org.apache.lucene.document.Document)2 SortSpec (com.github.chipolaris.bootforum.dao.SortSpec)1 IOException (java.io.IOException)1 Date (java.util.Date)1 List (java.util.List)1 PostConstruct (javax.annotation.PostConstruct)1 FeatureField (org.apache.lucene.document.FeatureField)1 StoredField (org.apache.lucene.document.StoredField)1 StringField (org.apache.lucene.document.StringField)1 TextField (org.apache.lucene.document.TextField)1 IndexableField (org.apache.lucene.index.IndexableField)1 ParseException (org.apache.lucene.queryparser.classic.ParseException)1 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1