Java Code Examples for org.apache.lucene.queries.mlt.MoreLikeThis#setFieldNames()

The following examples show how to use org.apache.lucene.queries.mlt.MoreLikeThis#setFieldNames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SearchImpl.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Query mltQuery(int docid, MLTConfig mltConfig, Analyzer analyzer) {
  MoreLikeThis mlt = new MoreLikeThis(reader);

  mlt.setAnalyzer(analyzer);
  mlt.setFieldNames(mltConfig.getFieldNames());
  mlt.setMinDocFreq(mltConfig.getMinDocFreq());
  mlt.setMaxDocFreq(mltConfig.getMaxDocFreq());
  mlt.setMinTermFreq(mltConfig.getMinTermFreq());

  try {
    return mlt.like(docid);
  } catch (IOException e) {
    throw new LukeException("Failed to create MLT query for doc: " + docid);
  }
}
 
Example 2
Source File: LuceneIndexer.java    From MtgDesktopCompanion with GNU General Public License v3.0 4 votes vote down vote up
public Map<MagicCard,Float> similarity(MagicCard mc) throws IOException 
{
	Map<MagicCard,Float> ret = new LinkedHashMap<>();
	
	if(mc==null)
		return ret;
	
	if(dir==null)
		open();
	
	logger.debug("search similar cards for " + mc);
	
	try (IndexReader indexReader = DirectoryReader.open(dir))
	{
		
	 IndexSearcher searcher = new IndexSearcher(indexReader);
	 Query query = new QueryParser("text", analyzer).parse("name:\""+mc.getName()+"\"");
	 logger.trace(query);
	 TopDocs top = searcher.search(query, 1);
	 
	 if(top.totalHits.value>0)
	 {
		 MoreLikeThis mlt = new MoreLikeThis(indexReader);
		  mlt.setFieldNames(getArray(FIELDS));
		  mlt.setAnalyzer(analyzer);
		  mlt.setMinTermFreq(getInt(MIN_TERM_FREQ));
		  mlt.setBoost(getBoolean(BOOST));
		  
		  
		  
		 ScoreDoc d = top.scoreDocs[0];
		 logger.trace("found doc id="+d.doc);
		 Query like = mlt.like(d.doc);
		 
		 logger.trace("mlt="+Arrays.asList(mlt.retrieveInterestingTerms(d.doc)));
		 logger.trace("Like query="+like);
		 TopDocs likes = searcher.search(like,getInt(MAX_RESULTS));
		 
		 for(ScoreDoc l : likes.scoreDocs)
			 ret.put(serializer.fromJson(searcher.doc(l.doc).get("data"),MagicCard.class),l.score);

		 logger.debug("found " + likes.scoreDocs.length + " results");
		 close();
		
	 }
	 else
	 {
		 logger.error("can't found "+mc);
	 }
	 
	} catch (ParseException e) {
		logger.error(e);
	}
	return ret;
	
}
 
Example 3
Source File: ContextAnalyzerIndex.java    From modernmt with Apache License 2.0 4 votes vote down vote up
public ContextVector getContextVector(UUID user, LanguageDirection direction, Corpus queryDocument, int limit, Rescorer rescorer) throws IOException {
    String contentFieldName = DocumentBuilder.makeContentFieldName(direction);

    IndexSearcher searcher = this.getIndexSearcher();
    IndexReader reader = searcher.getIndexReader();

    // Get matching documents

    int rawLimit = limit < MIN_RESULT_BATCH ? MIN_RESULT_BATCH : limit;

    MoreLikeThis mlt = new MoreLikeThis(reader);
    mlt.setFieldNames(new String[]{contentFieldName});
    mlt.setMinDocFreq(0);
    mlt.setMinTermFreq(1);
    mlt.setMinWordLen(2);
    mlt.setBoost(true);
    mlt.setAnalyzer(analyzer);

    TopScoreDocCollector collector = TopScoreDocCollector.create(rawLimit, true);

    Reader queryDocumentReader = queryDocument.getRawContentReader();

    try {
        Query mltQuery = mlt.like(contentFieldName, queryDocumentReader);
        BooleanQuery ownerQuery = new BooleanQuery();

        if (user == null) {
            ownerQuery.add(DocumentBuilder.makePublicOwnerMatchingQuery(), BooleanClause.Occur.MUST);
        } else {
            ownerQuery.add(DocumentBuilder.makePublicOwnerMatchingQuery(), BooleanClause.Occur.SHOULD);
            ownerQuery.add(DocumentBuilder.makeOwnerMatchingQuery(user), BooleanClause.Occur.SHOULD);
            ownerQuery.setMinimumNumberShouldMatch(1);
        }

        FilteredQuery query = new FilteredQuery(mltQuery, new QueryWrapperFilter(ownerQuery));
        searcher.search(query, collector);
    } finally {
        IOUtils.closeQuietly(queryDocumentReader);
    }

    ScoreDoc[] topDocs = collector.topDocs().scoreDocs;

    // Rescore result

    if (rescorer != null) {
        Document referenceDocument = DocumentBuilder.newInstance(direction, queryDocument);
        rescorer.rescore(reader, this.analyzer, topDocs, referenceDocument, contentFieldName);
    }

    // Build result

    ContextVector.Builder resultBuilder = new ContextVector.Builder(topDocs.length);
    resultBuilder.setLimit(limit);

    for (ScoreDoc topDocRef : topDocs) {
        Document topDoc = searcher.doc(topDocRef.doc);

        long memory = DocumentBuilder.getMemory(topDoc);
        resultBuilder.add(memory, topDocRef.score);
    }

    return resultBuilder.build();
}