本文整理汇总了Java中org.apache.lucene.queries.mlt.MoreLikeThis.setFieldNames方法的典型用法代码示例。如果您正苦于以下问题:Java MoreLikeThis.setFieldNames方法的具体用法?Java MoreLikeThis.setFieldNames怎么用?Java MoreLikeThis.setFieldNames使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.queries.mlt.MoreLikeThis
的用法示例。
在下文中一共展示了MoreLikeThis.setFieldNames方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSample
import org.apache.lucene.queries.mlt.MoreLikeThis; //导入方法依赖的package包/类
public synchronized Sample getSample(String id) throws Exception {
Term term = new Term(Sample.ID, id);
TermQuery tq = new TermQuery(term);
TopDocs td = getIndexSearcher().search(tq, 1);
if (td.totalHits > 0) {
Document doc = indexSearcher.doc(td.scoreDocs[0].doc);
Sample s = (Sample) SerializationUtil.deserialize(doc.getBinaryValue("RawData").bytes);
MoreLikeThis mlt = new MoreLikeThis(indexSearcher.getIndexReader());
mlt.setFieldNames(new String[] { Variable.LABEL, Variable.DESCRIPTION, Variable.TAGS });
mlt.setMaxWordLen(MAXWORDLENGTH);
String[] terms = mlt.retrieveInterestingTerms(td.scoreDocs[0].doc);
for (int i = 0; i < 10 && i < terms.length; i++) {
s.put(Variable.SUGGESTEDTAGS, terms[i]);
}
return s;
} else {
return null;
}
}
示例2: 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);
}
示例3: train
import org.apache.lucene.queries.mlt.MoreLikeThis; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void train(AtomicReader atomicReader, String[] textFieldNames, String classFieldName, Analyzer analyzer, Query query) throws IOException {
this.textFieldNames = textFieldNames;
this.classFieldName = classFieldName;
mlt = new MoreLikeThis(atomicReader);
mlt.setAnalyzer(analyzer);
mlt.setFieldNames(textFieldNames);
indexSearcher = new IndexSearcher(atomicReader);
if (minDocsFreq > 0) {
mlt.setMinDocFreq(minDocsFreq);
}
if (minTermFreq > 0) {
mlt.setMinTermFreq(minTermFreq);
}
this.query = query;
}
示例4: train
import org.apache.lucene.queries.mlt.MoreLikeThis; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void train(AtomicReader atomicReader, String textFieldName, String classFieldName, Analyzer analyzer) throws IOException {
this.textFieldName = textFieldName;
this.classFieldName = classFieldName;
mlt = new MoreLikeThis(atomicReader);
mlt.setAnalyzer(analyzer);
mlt.setFieldNames(new String[]{textFieldName});
indexSearcher = new IndexSearcher(atomicReader);
}
示例5: train
import org.apache.lucene.queries.mlt.MoreLikeThis; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void train(AtomicReader atomicReader, String textFieldName, String classFieldName, Analyzer analyzer, Query query) throws IOException {
this.textFieldNames = new String[]{textFieldName};
this.classFieldName = classFieldName;
mlt = new MoreLikeThis(atomicReader);
mlt.setAnalyzer(analyzer);
mlt.setFieldNames(new String[]{textFieldName});
indexSearcher = new IndexSearcher(atomicReader);
this.query = query;
}
示例6: 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();
}
示例7: 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;
}