本文整理汇总了Java中org.apache.lucene.search.TopDocs类的典型用法代码示例。如果您正苦于以下问题:Java TopDocs类的具体用法?Java TopDocs怎么用?Java TopDocs使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TopDocs类属于org.apache.lucene.search包,在下文中一共展示了TopDocs类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testMMapDirectory
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
public void testMMapDirectory() throws IOException {
long start = System.currentTimeMillis();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(IndexWriterConfig
.OpenMode.CREATE);
FSDirectory open = FSDirectory.open(Paths.get("E:/testlucene"));
IndexWriter indexWriter = new IndexWriter(open, indexWriterConfig);
for (int i = 0; i < 10000000; i++) {
indexWriter.addDocument(addDocument(i));
}
indexWriter.commit();
indexWriter.close();
long end = System.currentTimeMillis();
log.error("MMapDirectory consumes {}s!", (end - start) / 1000);
start = System.currentTimeMillis();
IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(open));
int total = 0;
for (int i = 0; i < 10000000; i++) {
TermQuery key1 = new TermQuery(new Term("key1", "key" + i));
TopDocs search = indexSearcher.search(key1, 10);
total += search.totalHits;
}
System.out.println(total);
end = System.currentTimeMillis();
log.error("MMapDirectory search consumes {}ms!", (end - start));
}
示例2: duelRun
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
private void duelRun(PercolateQuery.QueryStore queryStore, MemoryIndex memoryIndex, IndexSearcher shardSearcher) throws IOException {
boolean requireScore = randomBoolean();
IndexSearcher percolateSearcher = memoryIndex.createSearcher();
Query percolateQuery = fieldType.percolateQuery("type", queryStore, new BytesArray("{}"), percolateSearcher);
Query query = requireScore ? percolateQuery : new ConstantScoreQuery(percolateQuery);
TopDocs topDocs = shardSearcher.search(query, 10);
Query controlQuery = new ControlQuery(memoryIndex, queryStore);
controlQuery = requireScore ? controlQuery : new ConstantScoreQuery(controlQuery);
TopDocs controlTopDocs = shardSearcher.search(controlQuery, 10);
assertThat(topDocs.totalHits, equalTo(controlTopDocs.totalHits));
assertThat(topDocs.scoreDocs.length, equalTo(controlTopDocs.scoreDocs.length));
for (int j = 0; j < topDocs.scoreDocs.length; j++) {
assertThat(topDocs.scoreDocs[j].doc, equalTo(controlTopDocs.scoreDocs[j].doc));
assertThat(topDocs.scoreDocs[j].score, equalTo(controlTopDocs.scoreDocs[j].score));
if (requireScore) {
Explanation explain1 = shardSearcher.explain(query, topDocs.scoreDocs[j].doc);
Explanation explain2 = shardSearcher.explain(controlQuery, controlTopDocs.scoreDocs[j].doc);
assertThat(explain1.isMatch(), equalTo(explain2.isMatch()));
assertThat(explain1.getValue(), equalTo(explain2.getValue()));
}
}
}
示例3: assertAvgScoreMode
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
@Override
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher) throws IOException {
MultiValueMode sortMode = MultiValueMode.AVG;
Query childFilter = Queries.not(parentFilter);
XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
TopDocs topDocs = searcher.search(query, 5, sort);
assertThat(topDocs.totalHits, equalTo(7));
assertThat(topDocs.scoreDocs.length, equalTo(5));
assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2));
assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
}
示例4: assertAvgScoreMode
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher, IndexFieldData.XFieldComparatorSource innerFieldComparator) throws IOException {
MultiValueMode sortMode = MultiValueMode.AVG;
Query childFilter = Queries.not(parentFilter);
XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
TopDocs topDocs = searcher.search(query, 5, sort);
assertThat(topDocs.totalHits, equalTo(7));
assertThat(topDocs.scoreDocs.length, equalTo(5));
assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2));
assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
}
示例5: assertAvgScoreMode
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher) throws IOException {
MultiValueMode sortMode = MultiValueMode.AVG;
Query childFilter = Queries.not(parentFilter);
XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
TopDocs topDocs = searcher.search(query, 5, sort);
assertThat(topDocs.totalHits, equalTo(7));
assertThat(topDocs.scoreDocs.length, equalTo(5));
assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
assertThat(topDocs.scoreDocs[1].doc, equalTo(3));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[2].doc, equalTo(7));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(4));
}
示例6: testSimpleNumericOps
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
public void testSimpleNumericOps() throws Exception {
Directory dir = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
Document document = new Document();
document.add(new TextField("_id", "1", Field.Store.YES));
document.add(new LegacyIntField("test", 2, LegacyIntField.TYPE_STORED));
indexWriter.addDocument(document);
IndexReader reader = DirectoryReader.open(indexWriter);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);
Document doc = searcher.doc(topDocs.scoreDocs[0].doc);
IndexableField f = doc.getField("test");
assertThat(f.stringValue(), equalTo("2"));
BytesRefBuilder bytes = new BytesRefBuilder();
LegacyNumericUtils.intToPrefixCoded(2, 0, bytes);
topDocs = searcher.search(new TermQuery(new Term("test", bytes.get())), 1);
doc = searcher.doc(topDocs.scoreDocs[0].doc);
f = doc.getField("test");
assertThat(f.stringValue(), equalTo("2"));
indexWriter.close();
}
示例7: getDocIdForId
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
/**
* Returns lucene's document id for the given id in the given {@link IIndexTypeConf}
* @param typeConf the 188.166.43.201 to fins.
* @param id the id to find.
* @return the id or 0 if document was not found.
*/
public int getDocIdForId(final IIndexTypeConf typeConf, final String id) {
final SearchOptions params = new SearchOptions();
params.setMaxResults(1);
final BooleanQuery query = new BooleanQuery();
QueryUtil.addTypeConf(query, typeConf);
QueryUtil.addId(query, id);
final TopDocs topDocs = IndexSearch.getInstance().getTopDocs(query, params);
if(topDocs.totalHits == 0) {
throw new IllegalStateException("Can't find news with id " + id + " in news index.");
}
else if(topDocs.totalHits > 1) {
LOGGER.warn("Found more than one result for news with id " + id + " in news index. "
+ "This is an invalid state. Using the first found document.");
}
return topDocs.scoreDocs[0].doc;
}
示例8: getAmountOfNewsInNewsGroups
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
/**
* Returns the amount of {@link News} which are assigned to news groups (news group id > 0)
* for the given {@link Query}.
*/
private int getAmountOfNewsInNewsGroups(final Query filterQuery) {
final BooleanQuery query = new BooleanQuery();
query.add(filterQuery, Occur.MUST);
// get only news that are in real groups (newsGroupId > 0)
final NumericRangeQuery<Long> newsGroupFilterQuery = NumericRangeQuery.newLongRange(
NewsIndexType.FIELD_NEWSGROUPID, 0l, null, false, true);
query.add(newsGroupFilterQuery, Occur.MUST);
final SearchOptions options = new SearchOptions();
options.setMaxResults(0); // we only want the totalHits, not the results.
final TopDocs topDocs = IndexSearch.getInstance().getTopDocs(query, options);
return topDocs.totalHits;
}
示例9: checkIndexContent
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
private void checkIndexContent(final String elementId,
final String fieldContent, final int expectedAmount) throws IOException {
final IndexReader reader = IndexManager.getInstance().getIndex().getIndexReader();
final IndexSearcher searcher = new IndexSearcher(reader);
final TopDocs topDocs = searcher.search(new TermQuery(new Term(FIELDNAME, fieldContent)), expectedAmount + 10);
assertNotNull(topDocs);
assertTrue(topDocs.totalHits == expectedAmount);
if(expectedAmount > 0) {
final ScoreDoc scoreDoc = topDocs.scoreDocs[0];
assertNotNull(scoreDoc);
final Document doc = reader.document(scoreDoc.doc);
assertNotNull(doc);
assertEquals(fieldContent, doc.get(FIELDNAME));
assertEquals(elementId, doc.get(IIndexElement.FIELD_ID));
assertEquals(INDEX_TYPE, doc.get(IIndexElement.FIELD_INDEX_TYPE));
}
}
示例10: prefixSearch
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
@Override
public ScoreDoc[] prefixSearch(String q) throws IOException {
if (StringUtils.isEmpty(q) || q.length() > appConfig.getKeywordMaxLength()) {
logger.error("empty keywords or over-length! {}", q);
return null;
}
final TopDocs[] rstTopDocs = new TopDocs[2];
final Query nameFldQuery = new PrefixQuery(new Term(NAME.getName(), q));
rstTopDocs[0] = indexSearcher.search(nameFldQuery, appConfig.getQuickTipsNum() * 2, sort);
final Query downLoadRankQuery = NumericRangeQuery.newIntRange(DOWNOLOAD_RANK.getName(), MIN_DOWNLOAD_RANK, Integer.MAX_VALUE, true, false);
//从下载量最高的1000条记录中,再过滤符合关键字的记录
rstTopDocs[1] = indexSearcher.search(downLoadRankQuery, MAX_TOP, sort);
TopDocs rst = TopDocsUtil.mergeDuplicateDocId(TopDocs.merge(sort, MAX_TOP + appConfig.getQuickTipsNum() * 2, rstTopDocs));
if(rst != null) {
return rst.scoreDocs;
}
return null;
}
示例11: combine
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
/** Modifies incoming TopDocs (in) by replacing the top hits with resorted's hits, and then resorting all hits. */
private TopDocs combine(TopDocs in, TopDocs resorted, QueryRescoreContext ctx) {
System.arraycopy(resorted.scoreDocs, 0, in.scoreDocs, 0, resorted.scoreDocs.length);
if (in.scoreDocs.length > resorted.scoreDocs.length) {
// These hits were not rescored (beyond the rescore window), so we treat them the same as a hit that did get rescored but did
// not match the 2nd pass query:
for(int i=resorted.scoreDocs.length;i<in.scoreDocs.length;i++) {
// TODO: shouldn't this be up to the ScoreMode? I.e., we should just invoke ScoreMode.combine, passing 0.0f for the
// secondary score?
in.scoreDocs[i].score *= ctx.queryWeight();
}
// TODO: this is wrong, i.e. we are comparing apples and oranges at this point. It would be better if we always rescored all
// incoming first pass hits, instead of allowing recoring of just the top subset:
Arrays.sort(in.scoreDocs, SCORE_DOC_COMPARATOR);
}
// update the max score after the resort
in.setMaxScore(in.scoreDocs[0].score);
return in;
}
示例12: testMultiPhrasePrefixQuery
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
public void testMultiPhrasePrefixQuery() throws Exception {
Analyzer analyzer = new StandardAnalyzer();
Directory dir = newDirectory();
String value = "The quick brown fox.";
IndexReader ir = indexOneDoc(dir, "text", value, analyzer);
MultiPhrasePrefixQuery query = new MultiPhrasePrefixQuery();
query.add(new Term("text", "quick"));
query.add(new Term("text", "brown"));
query.add(new Term("text", "fo"));
IndexSearcher searcher = newSearcher(ir);
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
assertThat(topDocs.totalHits, equalTo(1));
int docId = topDocs.scoreDocs[0].doc;
CustomPassageFormatter passageFormatter = new CustomPassageFormatter("<b>", "</b>", new DefaultEncoder());
CustomUnifiedHighlighter highlighter = new CustomUnifiedHighlighter(searcher, analyzer,
passageFormatter, null, value, false);
Snippet[] snippets = highlighter.highlightField("text", query, docId, 5);
assertThat(snippets.length, equalTo(1));
assertThat(snippets[0].getText(), equalTo("The <b>quick</b> <b>brown</b> <b>fox</b>."));
ir.close();
dir.close();
}
示例13: testAllTermQuery
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
public void testAllTermQuery() throws IOException {
Directory dir = newDirectory();
String value = "The quick brown fox.";
Analyzer analyzer = new StandardAnalyzer();
IndexReader ir = indexOneDoc(dir, "all", value, analyzer);
AllTermQuery query = new AllTermQuery(new Term("all", "fox"));
IndexSearcher searcher = newSearcher(ir);
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
assertThat(topDocs.totalHits, equalTo(1));
int docId = topDocs.scoreDocs[0].doc;
CustomPassageFormatter passageFormatter = new CustomPassageFormatter("<b>", "</b>", new DefaultEncoder());
CustomUnifiedHighlighter highlighter = new CustomUnifiedHighlighter(searcher, analyzer,
passageFormatter, null, value, false);
Snippet[] snippets = highlighter.highlightField("all", query, docId, 5);
assertThat(snippets.length, equalTo(1));
assertThat(snippets[0].getText(), equalTo("The quick brown <b>fox</b>."));
ir.close();
dir.close();
}
示例14: testCommonTermsQuery
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
public void testCommonTermsQuery() throws IOException {
Directory dir = newDirectory();
String value = "The quick brown fox.";
Analyzer analyzer = new StandardAnalyzer();
IndexReader ir = indexOneDoc(dir, "text", value, analyzer);
CommonTermsQuery query = new CommonTermsQuery(BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, 128);
query.add(new Term("text", "quick"));
query.add(new Term("text", "brown"));
query.add(new Term("text", "fox"));
IndexSearcher searcher = newSearcher(ir);
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
assertThat(topDocs.totalHits, equalTo(1));
int docId = topDocs.scoreDocs[0].doc;
CustomPassageFormatter passageFormatter = new CustomPassageFormatter("<b>", "</b>", new DefaultEncoder());
CustomUnifiedHighlighter highlighter = new CustomUnifiedHighlighter(searcher, analyzer,
passageFormatter, null, value, false);
Snippet[] snippets = highlighter.highlightField("text", query, docId, 5);
assertThat(snippets.length, equalTo(1));
assertThat(snippets[0].getText(), equalTo("The <b>quick</b> <b>brown</b> <b>fox</b>."));
ir.close();
dir.close();
}
示例15: search
import org.apache.lucene.search.TopDocs; //导入依赖的package包/类
public Map<String, Integer> search(String word, String field, int maxSearch) {
if (indexSearcher == null) {
initialize(index);
}
Map<String, Integer> verbFreqs = new HashMap<>();
QueryParser queryParser = new QueryParser(Version.LUCENE_36, field, analyzer);
try {
Query query = queryParser.parse(word);
TopDocs topDocs = indexSearcher.search(query, maxSearch);
ScoreDoc[] doc = topDocs.scoreDocs;
for (int i = 0; i < maxSearch && i < doc.length; ++i) {
int documentId = doc[i].doc;
Document document = indexSearcher.doc(documentId);
String verb = document.get(VERB);
String frequency = document.get(FREQ);
verbFreqs.put(verb, Integer.parseInt(frequency));
}
} catch (ParseException | IOException e) {
log.warn("Error searching Lucene index.", e);
}
return verbFreqs;
}