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;
}
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;
}
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;
}
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));
}
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();
}
}
Aggregations