當前位置: 首頁>>代碼示例>>Java>>正文


Java TopScoreDocCollector.topDocs方法代碼示例

本文整理匯總了Java中org.apache.lucene.search.TopScoreDocCollector.topDocs方法的典型用法代碼示例。如果您正苦於以下問題:Java TopScoreDocCollector.topDocs方法的具體用法?Java TopScoreDocCollector.topDocs怎麽用?Java TopScoreDocCollector.topDocs使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.lucene.search.TopScoreDocCollector的用法示例。


在下文中一共展示了TopScoreDocCollector.topDocs方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: search

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
/**
 * Given a search field to search,the name of the field to return results in
 * and a query string, return search results up to the limit.
 * 
 * @param searchfieldName
 * @param returnFieldName
 * @param queryStr
 * @param limit
 * @return search results (with confidences)
 * @throws ParseException
 * @throws IOException
 */
public HashMap<String[], Float> search(String searchfieldName,
		String[] returnFieldName, String queryStr, int limit)
		throws ParseException, IOException {
	if (queryStr == null || queryStr.length() == 0)
		return new HashMap<String[], Float>();
	final String clean = QueryParser.escape(queryStr);
	final Query q = new QueryParser(Version.LUCENE_40, searchfieldName,
			analyser).parse(clean);
	final TopScoreDocCollector collector = TopScoreDocCollector.create(
			limit, true);

	searcher.search(q, collector);
	final ScoreDoc[] hits = collector.topDocs().scoreDocs;
	final HashMap<String[], Float> results = new HashMap<String[], Float>();
	for (int i = 0; i < hits.length; ++i) {
		final int docId = hits[i].doc;
		final Document d = searcher.doc(docId);
		String[] rvalues = new String[returnFieldName.length];
		for(int j=0;j<rvalues.length;j++){
			rvalues[j]=d.get(returnFieldName[j]);
		}
		results.put(rvalues, hits[i].score);
	}
	return results;
}
 
開發者ID:openimaj,項目名稱:openimaj,代碼行數:38,代碼來源:QuickSearcher.java

示例2: search

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
public SearchResult search (String queryString, int hitsPerPage) throws IOException  {
	SearchResult result = new SearchResult();
	result.setQuery(queryString);
    TopScoreDocCollector collector;
    ScoreDoc[] docs;
    Query query;
    try { // new Query parser, because it is not thread safe
        query = new QueryParser("terms", new StandardAnalyzer()).parse(QueryParser.escape(queryString));
    } catch (ParseException e) {
        throw new IOException(e);
    }
    collector = TopScoreDocCollector.create(hitsPerPage, true);
	if (hitsSearcher == null) openReader(); // reopen index to see updates.
    hitsSearcher.search(query, collector);
    docs = collector.topDocs().scoreDocs;
    for(ScoreDoc doc: docs) {
	    int docId = doc.doc;
        Document d = hitsSearcher.doc(docId);
        Hit hit = new Hit(d.get("result"));
        hit.put("score", doc.score);
        hit.remove("query"); // remove for privacy reasons
        result.addHit(hit);
    }
    return result;
}
 
開發者ID:searsia,項目名稱:searsiaserver,代碼行數:26,代碼來源:SearchResultIndex.java

示例3: getHit

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
/**
 * Get Hit by Lucene id. Used for tests only
 * @param hitId
 * @return hit
 * @throws IOException
 */
protected Hit getHit(String hitId) throws IOException {
	Term term = new Term("id", hitId);
	Query query = new TermQuery(term);
	TopScoreDocCollector collector = TopScoreDocCollector.create(1, true);
	if (hitsSearcher == null) openReader();
	hitsSearcher.search(query, collector);
	if (collector.getTotalHits() > 0) {
    	ScoreDoc[] docs = collector.topDocs().scoreDocs;
    	Document doc = hitsSearcher.doc(docs[0].doc);
    	Hit hit = new Hit(doc.get("result"));
    	return hit;
	} else {
		return null;
	}
}
 
開發者ID:searsia,項目名稱:searsiaserver,代碼行數:22,代碼來源:SearchResultIndex.java

示例4: getVector

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
public Object2DoubleMap<String> getVector(Language lang, String value) {
    Object2DoubleMap<String> uriWeightMap = new Object2DoubleOpenHashMap<>();
    
    try {
        Search luceneSearch = search();
        final String field = getContentFieldName(lang);
        
        TopScoreDocCollector docsCollector = luceneSearch.search(value, field);
        
        ScoreDoc[] scoreDocs = docsCollector.topDocs().scoreDocs;
        
        double score = 0.0;
        for(int i=0;i<scoreDocs.length;++i) {
            int docID = scoreDocs[i].doc;
            score = scoreDocs[i].score;
            Document document = luceneSearch.getDocumentWithDocID(docID);
            String uri = document.get(fieldNameURI);
            uriWeightMap.put(uri, score);
        }
        return uriWeightMap;
    } catch(IOException x) {
        throw new RuntimeException(x);
    }
}
 
開發者ID:jmccrae,項目名稱:naisc,代碼行數:25,代碼來源:Corpus.java

示例5: search

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
private List<KObject> search(final Query query,
                             final int totalNumHitsEstimate,
                             final IOSearchService.Filter filter,
                             final ClusterSegment... clusterSegments) {
    final TopScoreDocCollector collector = TopScoreDocCollector.create(totalNumHitsEstimate);
    final IndexSearcher index = indexManager.getIndexSearcher(clusterSegments);
    final List<KObject> result = new ArrayList<KObject>();
    try {
        index.search(query,
                     collector);
        final ScoreDoc[] hits = collector.topDocs(0).scoreDocs;
        for (int i = 0; i < hits.length; i++) {
            final KObject kObject = toKObject(index.doc(hits[i].doc));
            if (filter.accept(kObject)) {
                result.add(kObject);
            }
        }
    } catch (final Exception ex) {
        throw new RuntimeException("Error during Query!",
                                   ex);
    } finally {
        indexManager.release(index);
    }

    return result;
}
 
開發者ID:kiegroup,項目名稱:appformer,代碼行數:27,代碼來源:LuceneSearchIndex.java

示例6: getNeighbours

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
public Map<Movie, Float> getNeighbours(final Movie movie,
		final int numberOfHits) throws ParseException, IOException {
	final StandardAnalyzer analyzer = new StandardAnalyzer(
			Version.LUCENE_46);
	final QueryParser parser = new QueryParser(Version.LUCENE_46,
			FIELD_SUMMARY, analyzer);
	final Query query = parser.parse(movie.toString());
	final TopScoreDocCollector collector = TopScoreDocCollector.create(
			numberOfHits, true);

	final IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(
			writer, false));
	searcher.search(query, collector);

	final Map<Movie, Float> result = new LinkedHashMap<Movie, Float>();
	final ScoreDoc[] scoreDocs = collector.topDocs().scoreDocs;
	for (final ScoreDoc doc : scoreDocs) {
		result.put(indexedMovies.get(doc.doc), 1 - doc.score);
	}
	return result;
}
 
開發者ID:asenovm,項目名稱:FuzzyClassifier,代碼行數:22,代碼來源:MovieIndex.java

示例7: compareHits

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
private void compareHits(SpanQueryParser p, String s, IndexSearcher searcher, int ... docids) throws Exception{
  Query q = p.parse(s);
  TopScoreDocCollector results = TopScoreDocCollector.create(1000);
  searcher.search(q, results);
  ScoreDoc[] scoreDocs = results.topDocs().scoreDocs;
  Set<Integer> hits = new HashSet<>();

  for (int i = 0; i < scoreDocs.length; i++) {
    hits.add(scoreDocs[i].doc);
  }
  assertEquals(docids.length, hits.size());

  for (int i = 0; i < docids.length; i++) {
    assertTrue("couldn't find " + Integer.toString(docids[i]) + " among the hits", hits.contains(docids[i]));
  }
}
 
開發者ID:tballison,項目名稱:lucene-addons,代碼行數:17,代碼來源:TestOverallSpanQueryParser.java

示例8: returnMusics

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
private List<String> returnMusics(Query query) throws IOException {
	int hitsPerPage = 10;
	IndexReader reader = DirectoryReader.open(LuceneUtil.INSTANCE.getDirectory());
	IndexSearcher searcher = new IndexSearcher(reader);
	TopScoreDocCollector collector = TopScoreDocCollector.create(
			hitsPerPage, true);
	searcher.search(query, collector);
	ScoreDoc[] hits = collector.topDocs().scoreDocs;
	
	   
	   List<String> musics = new LinkedList<>();
	    for(int i=0;i<hits.length;++i) {
	      int docId = hits[i].doc;
	      Document d = searcher.doc(docId);
	      musics.add(d.get(COLUMN_NAME));
	    }
	return musics;
}
 
開發者ID:otaviojava,項目名稱:Easy-Cassandra-samples,代碼行數:19,代碼來源:MusicSearch.java

示例9: returnResume

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
private List<String> returnResume(Query query) throws IOException {
	int hitsPerPage = 10;
	IndexReader reader = DirectoryReader.open(LuceneUtil.INSTANCE.getDirectory());
	IndexSearcher searcher = new IndexSearcher(reader);
	TopScoreDocCollector collector = TopScoreDocCollector.create(
			hitsPerPage, true);
	searcher.search(query, collector);
	ScoreDoc[] hits = collector.topDocs().scoreDocs;
	
	   
	   List<String> resumeIDs = new LinkedList<>();
	    for(int i=0;i<hits.length;++i) {
	      int docId = hits[i].doc;
	      Document d = searcher.doc(docId);
	      resumeIDs.add(d.get(COLUMN_NICk_NAME));
	    }
	return resumeIDs;
}
 
開發者ID:otaviojava,項目名稱:Easy-Cassandra-samples,代碼行數:19,代碼來源:ResumeSearch.java

示例10: searchUri

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
private String searchUri(String title, Language language) {
	BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST,
			BooleanClause.Occur.MUST};	
	String uri = null;
	String[] queryStrings = new String[2];
	String[] fields = new String[2];
	queryStrings[0] = title;
	queryStrings[1] = language.getIso639_1();
	fields[0] = TitleURILucDocCreator.Fields.Title.toString();
	fields[1] = TitleURILucDocCreator.Fields.LanguageISOCode.toString();		
	TopScoreDocCollector docCollector = searcher.multiFieldTermSearch(queryStrings, fields, flags, 10);
	ScoreDoc[] scoreDocs = docCollector.topDocs().scoreDocs;
	if(scoreDocs.length>0) {
		ScoreDoc scoreDoc = scoreDocs[0];
		Document document = searcher.getDocumentWithDocID(scoreDoc.doc);
		uri = document.get(TitleURILucDocCreator.Fields.URI_EN.toString());
	}
	return uri;
}
 
開發者ID:kasooja,項目名稱:cl-esa,代碼行數:20,代碼來源:ArticlesOTDFProcessor.java

示例11: makeVector

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
public void makeVector() {
	TIntDoubleHashMap vecMapT = new TIntDoubleHashMap();	
	String fieldName = pair.getSecond().getIso639_1() + "TopicContent";
	TopScoreDocCollector docsCollector = searcher.search(pair.getFirst(), lucHits, fieldName, AnalyzerFactory.getAnalyzer(pair.getSecond()));			
	if(docsCollector == null) {
		vectorMapT.put(threadName, null); 
		return;				
	}	
	ScoreDoc[] scoreDocs = docsCollector.topDocs().scoreDocs;
	double score = 0.0;
	for(int i=0;i<scoreDocs.length;++i) {
		int docID = scoreDocs[i].doc;
		score = scoreDocs[i].score;
		vecMapT.put(docID, score);
	}		
	vectorMapT.put(threadName, vecMapT);
}
 
開發者ID:kasooja,項目名稱:cl-esa,代碼行數:18,代碼來源:CLESA.java

示例12: getFromIndex

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
private List<Triple> getFromIndex(int maxNumberOfResults, BooleanQuery bq) throws IOException {
	log.debug("\t start asking index...");
	TopScoreDocCollector collector = TopScoreDocCollector.create(maxNumberOfResults, true);
	// Similarity BM25Similarity = new BM25Similarity();
	// isearcher.setSimilarity(BM25Similarity);
	isearcher.search(bq, collector);
	ScoreDoc[] hits = collector.topDocs().scoreDocs;

	List<Triple> triples = new ArrayList<Triple>();
	String s, p, o;
	for (int i = 0; i < hits.length; i++) {
		Document hitDoc = isearcher.doc(hits[i].doc);
		s = hitDoc.get(FIELD_NAME_SUBJECT);
		p = hitDoc.get(FIELD_NAME_PREDICATE);
		o = hitDoc.get(FIELD_NAME_OBJECT_URI);
		if (o == null) {
			o = hitDoc.get(FIELD_NAME_OBJECT_LITERAL);
		}
		Triple triple = new Triple(s, p, o);
		triples.add(triple);
	}
	log.debug("\t finished asking index...");
	return triples;
}
 
開發者ID:dice-group,項目名稱:AGDISTIS,代碼行數:25,代碼來源:TripleIndex.java

示例13: getFromIndex

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
private List<Triple> getFromIndex(final int maxNumberOfResults, final BooleanQuery bq)
    throws IOException {
  log.debug("\t start asking index...");
  final TopScoreDocCollector collector = TopScoreDocCollector.create(maxNumberOfResults, true);
  // Similarity BM25Similarity = new BM25Similarity();
  // isearcher.setSimilarity(BM25Similarity);
  isearcher.search(bq, collector);
  final ScoreDoc[] hits = collector.topDocs().scoreDocs;

  final List<Triple> triples = new ArrayList<Triple>();
  String s, p, o;
  for (int i = 0; i < hits.length; i++) {
    final Document hitDoc = isearcher.doc(hits[i].doc);
    s = hitDoc.get(FIELD_NAME_SUBJECT);
    p = hitDoc.get(FIELD_NAME_PREDICATE);
    o = hitDoc.get(FIELD_NAME_OBJECT_URI);
    if (o == null) {
      o = hitDoc.get(FIELD_NAME_OBJECT_LITERAL);
    }
    final Triple triple = new Triple(s, p, o);
    triples.add(triple);
  }
  log.debug("\t finished asking index...");
  return triples;
}
 
開發者ID:dice-group,項目名稱:FOX,代碼行數:26,代碼來源:TripleIndex.java

示例14: search

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
public Set<String> search(String q) throws IOException {
    Set<String> recipes = new LinkedHashSet<>();
    String fields[] = new String[]{"description", "steps"};
    logger.debug(String.format("searching for %s", q));

    try (IndexReader reader = DirectoryReader.open(index)) {
        int hitsPerPage = 1000;
        IndexSearcher searcher = new IndexSearcher(reader);
        Query query = new MultiFieldQueryParser(fields, analyzer).parse(q);
        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, null);
        searcher.search(query, collector);

        ScoreDoc[] hits = collector.topDocs().scoreDocs;
        for (ScoreDoc hit : hits) {
            Document d = searcher.doc(hit.doc);
            recipes.add(d.get("title"));
        }

    } catch (CorruptIndexException | ParseException e) {
        logger.error(e);
    }

    logger.debug(String.format("found %d results", recipes.size()));
    return recipes;
}
 
開發者ID:anycook,項目名稱:anycook-api,代碼行數:26,代碼來源:FulltextIndex.java

示例15: search

import org.apache.lucene.search.TopScoreDocCollector; //導入方法依賴的package包/類
static void search(String className, Indexer indexer, Collection<IndexingContext> contexts, List<? super ClassUsage> results) throws IOException {
    String searchString = crc32base64(className.replace('.', '/'));
    Query refClassQuery = indexer.constructQuery(ClassDependencyIndexCreator.FLD_NB_DEPENDENCY_CLASS.getOntology(), new StringSearchExpression(searchString));
    TopScoreDocCollector collector = TopScoreDocCollector.create(NexusRepositoryIndexerImpl.MAX_RESULT_COUNT, null);
    for (IndexingContext context : contexts) {
        IndexSearcher searcher = context.acquireIndexSearcher();
        try {
    searcher.search(refClassQuery, collector);
    ScoreDoc[] hits = collector.topDocs().scoreDocs;
    LOG.log(Level.FINER, "for {0} ~ {1} found {2} hits", new Object[] {className, searchString, hits.length});
    for (ScoreDoc hit : hits) {
        int docId = hit.doc;
        Document d = searcher.doc(docId);
        String fldValue = d.get(ClassDependencyIndexCreator.NB_DEPENDENCY_CLASSES);
        LOG.log(Level.FINER, "{0} uses: {1}", new Object[] {className, fldValue});
        Set<String> refClasses = parseField(searchString, fldValue, d.get(ArtifactInfo.NAMES));
        if (!refClasses.isEmpty()) {
            ArtifactInfo ai = IndexUtils.constructArtifactInfo(d, context);
            if (ai != null) {
                ai.setRepository(context.getRepositoryId());
                List<NBVersionInfo> version = NexusRepositoryIndexerImpl.convertToNBVersionInfo(Collections.singleton(ai));
                if (!version.isEmpty()) {
                    results.add(new ClassUsage(version.get(0), refClasses));
                }
            }
        }
    }
    } finally {
        context.releaseIndexSearcher(searcher);
    }
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:33,代碼來源:ClassDependencyIndexCreator.java


注:本文中的org.apache.lucene.search.TopScoreDocCollector.topDocs方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。