当前位置: 首页>>代码示例>>Java>>正文


Java MoreLikeThis.setBoost方法代码示例

本文整理汇总了Java中org.apache.lucene.queries.mlt.MoreLikeThis.setBoost方法的典型用法代码示例。如果您正苦于以下问题:Java MoreLikeThis.setBoost方法的具体用法?Java MoreLikeThis.setBoost怎么用?Java MoreLikeThis.setBoost使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.lucene.queries.mlt.MoreLikeThis的用法示例。


在下文中一共展示了MoreLikeThis.setBoost方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getSimilar

import org.apache.lucene.queries.mlt.MoreLikeThis; //导入方法依赖的package包/类
/**
 * Searches for similar {@link News}.
 * @return a {@link List} with the similar {@link News}. Can be empty.
 * 		The more similar the more top is the {@link News} in the {@link List}.
 */
public List<News> getSimilar() {
	final int docId = IndexSearch.getInstance().getDocIdForId(
			NewsIndexType.getInstance(), String.valueOf(news.getId()));

	// configure "more like this"
	final MoreLikeThis moreLikeThis = IndexSearch.getInstance().newMoreLikeThis(news.getLocale());
	moreLikeThis.setMinWordLen(3);
	moreLikeThis.setBoost(true);
	moreLikeThis.setBoostFactor(10);
	moreLikeThis.setFieldNames(new String[] {
			NewsIndexType.FIELD_TITLE,
			NewsIndexType.FIELD_DESCRIPTION
	});

	try {
		final BooleanQuery query = new BooleanQuery();

		// it must have the same locale
		QueryUtil.addLocale(query, news.getLocale());

		// filter with publish date
		final NumericRangeQuery<Long> dateQuery = NumericRangeQuery.newLongRange(
				NewsIndexType.FIELD_PUBLISH_DATE, getDate(-PUBLISH_DATE_DELTA),
				getDate(PUBLISH_DATE_DELTA), true, true);
		query.add(dateQuery, Occur.MUST);

		// it must be in the same category.
		final NumericRangeQuery<Integer> categoryQuery = NumericRangeQuery.newIntRange(
				NewsIndexType.FIELD_CATEGORYID, news.getCategoryId(), news.getCategoryId(), true, true);
		query.add(categoryQuery, Occur.MUST);

		final Query moreQuery = moreLikeThis.like(docId);
		query.add(moreQuery, Occur.MUST);

		// not the same news
		query.add(new TermQuery(new Term(IIndexElement.FIELD_ID, String.valueOf(news.getId()))), Occur.MUST_NOT);

		// execute query
		final DocumentsSearchResult result = IndexSearch.getInstance().search(query, new SearchOptions());

		final List<Document> resultDocs = new ArrayList<>();
		for (final Document doc : result.getResults()) {
			final float score = result.getScore(doc);

			// use only news with a sufficient score.
			if(score >= MIN_SCORE) {
				resultDocs.add(doc);
				LOGGER.debug("News {} is similar to news {}.", doc.get(IIndexElement.FIELD_ID), news.getId());
			}
			else {
				LOGGER.debug("News {} has not a sufficient score to be similar to news {}.",
						doc.get(IIndexElement.FIELD_ID), news.getId());
			}
		}

		// convert the Documents to News.
		return NewsIndexType.docsToNews(resultDocs);
	}
	catch (final IOException e) {
		LOGGER.error("Can't build query for news with id '{}'.", news.getId());
	}

	return null;
}
 
开发者ID:XMBomb,项目名称:InComb,代码行数:70,代码来源:SimilarNewsFinder.java


注:本文中的org.apache.lucene.queries.mlt.MoreLikeThis.setBoost方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。