本文整理汇总了Java中org.apache.lucene.queries.mlt.MoreLikeThis.like方法的典型用法代码示例。如果您正苦于以下问题:Java MoreLikeThis.like方法的具体用法?Java MoreLikeThis.like怎么用?Java MoreLikeThis.like使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.queries.mlt.MoreLikeThis
的用法示例。
在下文中一共展示了MoreLikeThis.like方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: searchSimilar
import org.apache.lucene.queries.mlt.MoreLikeThis; //导入方法依赖的package包/类
public synchronized ListDataSet searchSimilar(Sample sample, int start, int count) throws Exception {
Term term = new Term(Sample.ID, sample.getId());
TermQuery tq = new TermQuery(term);
TopDocs td = getIndexSearcher().search(tq, count);
if (td == null || td.totalHits == 0) {
ListDataSet ds = ListDataSet.Factory.emptyDataSet();
return ds;
}
MoreLikeThis mlt = new MoreLikeThis(indexSearcher.getIndexReader());
mlt.setFieldNames(new String[] { Variable.LABEL, Variable.DESCRIPTION, Variable.TAGS });
mlt.setMaxWordLen(MAXWORDLENGTH);
Query query = mlt.like(td.scoreDocs[0].doc);
BooleanQuery bq = new BooleanQuery();
bq.add(query, Occur.MUST);
bq.add(new TermQuery(new Term("Id", sample.getId())), Occur.MUST_NOT);
return search(bq, start, count);
}
示例2: main
import org.apache.lucene.queries.mlt.MoreLikeThis; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
String indexDir = System.getProperty("index.dir");
FSDirectory directory = FSDirectory.open(new File(indexDir));
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
int numDocs = reader.maxDoc();
MoreLikeThis mlt = new MoreLikeThis(reader);
mlt.setFieldNames(new String[] { "title", "author" });
mlt.setMinTermFreq(1);
mlt.setMinDocFreq(1);
for (int docID = 0; docID < numDocs; docID++) {
LOGGER.info();
StoredDocument doc = reader.document(docID);
LOGGER.info(doc.get("title"));
Query query = mlt.like(docID);
LOGGER.info(" query=" + query);
TopDocs similarDocs = searcher.search(query, 10);
if (similarDocs.totalHits == 0)
LOGGER.info(" None like this");
for (int i = 0; i < similarDocs.scoreDocs.length; i++) {
if (similarDocs.scoreDocs[i].doc != docID) {
doc = reader.document(similarDocs.scoreDocs[i].doc);
LOGGER.info(" -> " + doc.getField("title").stringValue());
}
}
}
reader.close();
directory.close();
}
示例3: 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;
}