本文整理汇总了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;
}
示例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;
}
示例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;
}
}
示例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);
}
}
示例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;
}
示例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;
}
示例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]));
}
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
}
}