本文整理汇总了Java中org.apache.lucene.search.TopFieldDocs类的典型用法代码示例。如果您正苦于以下问题:Java TopFieldDocs类的具体用法?Java TopFieldDocs怎么用?Java TopFieldDocs使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TopFieldDocs类属于org.apache.lucene.search包,在下文中一共展示了TopFieldDocs类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSortValues
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
public void testSortValues() throws Exception {
Directory dir = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
for (int i = 0; i < 10; i++) {
Document document = new Document();
String text = new String(new char[]{(char) (97 + i), (char) (97 + i)});
document.add(new TextField("str", text, Field.Store.YES));
document.add(new SortedDocValuesField("str", new BytesRef(text)));
indexWriter.addDocument(document);
}
IndexReader reader = SlowCompositeReaderWrapper.wrap(DirectoryReader.open(indexWriter));
IndexSearcher searcher = new IndexSearcher(reader);
TopFieldDocs docs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("str", SortField.Type.STRING)));
for (int i = 0; i < 10; i++) {
FieldDoc fieldDoc = (FieldDoc) docs.scoreDocs[i];
assertThat((BytesRef) fieldDoc.fields[0], equalTo(new BytesRef(new String(new char[]{(char) (97 + i), (char) (97 + i)}))));
}
}
示例2: shouldGetSearchResultsWhenExceptionThrownOnRelease
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
@Test
public void shouldGetSearchResultsWhenExceptionThrownOnRelease() throws Exception {
IndexSearcher mockTrackSearcher = mock(IndexSearcher.class);
when(mockTrackManager.acquire()).thenReturn(mockTrackSearcher);
ScoreDoc[] scoreDocs = { new ScoreDoc(1, 0), new ScoreDoc(2, 0) };
when(mockTrackSearcher.search(any(), anyInt(), any()))
.thenReturn(new TopFieldDocs(scoreDocs.length, scoreDocs, null, 0));
setTrackSearcherDocuments(mockTrackSearcher);
doThrow(new RuntimeException("SearchManagerTest.shouldGetSearchResultsWhenExceptionThrownOnRelease()"))
.when(mockTrackManager).release(any());
List<Track> result = spySearchManager.search(new TrackSearch("keywords"));
assertThat("Result should have 2 tracks", result, hasSize(2));
}
示例3: shouldGetAlbumById
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
@Test
public void shouldGetAlbumById() throws Exception {
IndexSearcher mockTrackSearcher = mock(IndexSearcher.class);
when(mockTrackManager.acquire()).thenReturn(mockTrackSearcher);
List<ScoreDoc> scoreDocsList = new ArrayList<>();
for (int i = 1; i < 10; i++) {
scoreDocsList.add(new ScoreDoc(i, 0));
}
ScoreDoc[] scoreDocs = scoreDocsList.toArray(new ScoreDoc[scoreDocsList.size()]);
when(mockTrackSearcher.search(any(), anyInt(), any()))
.thenReturn(new TopFieldDocs(scoreDocs.length, scoreDocs, null, 0));
setTrackSearcherDocuments(mockTrackSearcher);
List<Track> tracks = spySearchManager.getAlbumById("123");
assertThat("Tracks should have a size of 9", tracks, hasSize(9));
}
示例4: shouldGetAlbumByIdWhenExceptionThrownOnRelease
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
@Test
public void shouldGetAlbumByIdWhenExceptionThrownOnRelease() throws Exception {
IndexSearcher mockTrackSearcher = mock(IndexSearcher.class);
when(mockTrackManager.acquire()).thenReturn(mockTrackSearcher);
List<ScoreDoc> scoreDocsList = new ArrayList<>();
for (int i = 1; i < 10; i++) {
scoreDocsList.add(new ScoreDoc(i, 0));
}
ScoreDoc[] scoreDocs = scoreDocsList.toArray(new ScoreDoc[scoreDocsList.size()]);
when(mockTrackSearcher.search(any(), anyInt(), any()))
.thenReturn(new TopFieldDocs(scoreDocs.length, scoreDocs, null, 0));
setTrackSearcherDocuments(mockTrackSearcher);
doThrow(new RuntimeException("SearchManagerTest.shouldGetAlbumByIdWhenExceptionThrownOnRelease()"))
.when(mockTrackManager).release(any());
List<Track> tracks = spySearchManager.getAlbumById("123");
assertThat("Tracks should have a size of 9", tracks, hasSize(9));
}
示例5: testSortValues
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
/** tests the returned sort values are correct */
public void testSortValues() throws Exception {
Expression expr = JavascriptCompiler.compile("sqrt(_score)");
SimpleBindings bindings = new SimpleBindings();
bindings.add(new SortField("_score", SortField.Type.SCORE));
Sort sort = new Sort(expr.getSortField(bindings, true));
Query query = new TermQuery(new Term("body", "contents"));
TopFieldDocs td = searcher.search(query, null, 3, sort, true, true);
for (int i = 0; i < 3; i++) {
FieldDoc d = (FieldDoc) td.scoreDocs[i];
float expected = (float) Math.sqrt(d.score);
float actual = ((Double)d.fields[0]).floatValue();
assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, actual));
}
}
示例6: testTwoOfSameBinding
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
/** tests same binding used more than once in an expression */
public void testTwoOfSameBinding() throws Exception {
Expression expr = JavascriptCompiler.compile("_score + _score");
SimpleBindings bindings = new SimpleBindings();
bindings.add(new SortField("_score", SortField.Type.SCORE));
Sort sort = new Sort(expr.getSortField(bindings, true));
Query query = new TermQuery(new Term("body", "contents"));
TopFieldDocs td = searcher.search(query, null, 3, sort, true, true);
for (int i = 0; i < 3; i++) {
FieldDoc d = (FieldDoc) td.scoreDocs[i];
float expected = 2*d.score;
float actual = ((Double)d.fields[0]).floatValue();
assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, actual));
}
}
示例7: testDollarVariable
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
/** Uses variables with $ */
public void testDollarVariable() throws Exception {
Expression expr = JavascriptCompiler.compile("$0+$score");
SimpleBindings bindings = new SimpleBindings();
bindings.add(new SortField("$0", SortField.Type.SCORE));
bindings.add(new SortField("$score", SortField.Type.SCORE));
Sort sort = new Sort(expr.getSortField(bindings, true));
Query query = new TermQuery(new Term("body", "contents"));
TopFieldDocs td = searcher.search(query, null, 3, sort, true, true);
for (int i = 0; i < 3; i++) {
FieldDoc d = (FieldDoc) td.scoreDocs[i];
float expected = 2*d.score;
float actual = ((Double)d.fields[0]).floatValue();
assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, actual));
}
}
示例8: testExpressionRefersToExpression
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
/** tests expression referring to another expression */
public void testExpressionRefersToExpression() throws Exception {
Expression expr1 = JavascriptCompiler.compile("_score");
Expression expr2 = JavascriptCompiler.compile("2*expr1");
SimpleBindings bindings = new SimpleBindings();
bindings.add(new SortField("_score", SortField.Type.SCORE));
bindings.add("expr1", expr1);
Sort sort = new Sort(expr2.getSortField(bindings, true));
Query query = new TermQuery(new Term("body", "contents"));
TopFieldDocs td = searcher.search(query, null, 3, sort, true, true);
for (int i = 0; i < 3; i++) {
FieldDoc d = (FieldDoc) td.scoreDocs[i];
float expected = 2*d.score;
float actual = ((Double)d.fields[0]).floatValue();
assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, actual));
}
}
示例9: doTestLotsOfBindings
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
private void doTestLotsOfBindings(int n) throws Exception {
SimpleBindings bindings = new SimpleBindings();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i > 0) {
sb.append("+");
}
sb.append("x" + i);
bindings.add(new SortField("x" + i, SortField.Type.SCORE));
}
Expression expr = JavascriptCompiler.compile(sb.toString());
Sort sort = new Sort(expr.getSortField(bindings, true));
Query query = new TermQuery(new Term("body", "contents"));
TopFieldDocs td = searcher.search(query, null, 3, sort, true, true);
for (int i = 0; i < 3; i++) {
FieldDoc d = (FieldDoc) td.scoreDocs[i];
float expected = n*d.score;
float actual = ((Double)d.fields[0]).floatValue();
assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, actual));
}
}
示例10: testNeareastRestaurantToWork
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
public void testNeareastRestaurantToWork() throws Exception {
Sort sort = new Sort(new SortField("unused",
new DistanceComparatorSource(10, 10)));
TopFieldDocs docs = searcher.search(query, null, 3, sort); //
assertEquals(4, docs.totalHits); //
assertEquals(3, docs.scoreDocs.length); //
FieldDoc fieldDoc = (FieldDoc) docs.scoreDocs[0]; //
assertEquals("(10,10) -> (9,6) = sqrt(17)",
new Float(Math.sqrt(17)),
fieldDoc.fields[0]); //
Document document = searcher.doc(fieldDoc.doc); //
assertEquals("Los Betos", document.get("name"));
//dumpDocs(sort, docs);
}
示例11: testMultiValueAllSet
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
public void testMultiValueAllSet() throws Exception {
fillMultiValueAllSet();
// the segments are force merged to a single segment so that the sorted binary doc values can be asserted within a single segment.
// Previously we used the SlowCompositeReaderWrapper but this is an unideal solution so force merging is a better idea.
writer.forceMerge(1);
IndexFieldData indexFieldData = getForField("value");
List<LeafReaderContext> readerContexts = refreshReader();
for (LeafReaderContext readerContext : readerContexts) {
AtomicFieldData fieldData = indexFieldData.load(readerContext);
assertThat(fieldData.ramBytesUsed(), greaterThanOrEqualTo(minRamBytesUsed()));
SortedBinaryDocValues bytesValues = fieldData.getBytesValues();
assertValues(bytesValues, 0, two(), four());
assertValues(bytesValues, 1, one());
assertValues(bytesValues, 2, three());
IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer));
TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MIN, null))));
assertThat(topDocs.totalHits, equalTo(3));
assertThat(topDocs.scoreDocs.length, equalTo(3));
assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
assertThat(topDocs.scoreDocs[2].doc, equalTo(2));
topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MAX, null), true)));
assertThat(topDocs.totalHits, equalTo(3));
assertThat(topDocs.scoreDocs.length, equalTo(3));
assertThat(topDocs.scoreDocs[0].doc, equalTo(0));
assertThat(topDocs.scoreDocs[1].doc, equalTo(2));
assertThat(topDocs.scoreDocs[2].doc, equalTo(1));
}
}
示例12: shouldGetEmptyAlbumByIdIfNoSearchResults
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
@Test
public void shouldGetEmptyAlbumByIdIfNoSearchResults() throws Exception {
IndexSearcher mockTrackSearcher = mock(IndexSearcher.class);
when(mockTrackManager.acquire()).thenReturn(mockTrackSearcher);
when(mockTrackSearcher.search(any(), anyInt(), any()))
.thenReturn(new TopFieldDocs(0, new ScoreDoc[] {}, null, 0));
setArtistSearcherDocuments(mockTrackSearcher);
List<Track> tracks = spySearchManager.getAlbumById("123");
assertThat("Tracks should be empty", tracks.isEmpty(), equalTo(true));
}
示例13: search
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
/** Utility method, to search and also collect all hits
* into the provided {@link Collector}. */
public static TopFieldDocs search(IndexSearcher searcher, Query q, Filter filter, int n, Sort sort, Collector fc) throws IOException {
if (sort == null) {
throw new IllegalArgumentException("sort must not be null");
}
return (TopFieldDocs) doSearch(searcher, null, q, filter, n, sort, false, false, fc);
}
示例14: doRead
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
private List<Document> doRead(Set<String> fields) throws IOException {
IndexSearcher[] searchers = new IndexSearcher[indexes.size()];
try {
int realLimit = opts.start() + opts.limit();
if (Integer.MAX_VALUE - opts.limit() < opts.start()) {
realLimit = Integer.MAX_VALUE;
}
TopFieldDocs[] hits = new TopFieldDocs[indexes.size()];
for (int i = 0; i < indexes.size(); i++) {
searchers[i] = indexes.get(i).acquire();
hits[i] = searchers[i].search(query, realLimit, sort);
}
TopDocs docs = TopDocs.merge(sort, realLimit, hits);
List<Document> result = new ArrayList<>(docs.scoreDocs.length);
for (int i = opts.start(); i < docs.scoreDocs.length; i++) {
ScoreDoc sd = docs.scoreDocs[i];
result.add(searchers[sd.shardIndex].doc(sd.doc, fields));
}
return result;
} finally {
for (int i = 0; i < indexes.size(); i++) {
if (searchers[i] != null) {
try {
indexes.get(i).release(searchers[i]);
} catch (IOException e) {
log.warn("cannot release Lucene searcher", e);
}
}
}
}
}
示例15: retrieveCountResult
import org.apache.lucene.search.TopFieldDocs; //导入依赖的package包/类
public int retrieveCountResult(P pparam) throws Exception {
QueryParser parser = loadQueryParser();
Query query = parser.parse(composeQuery(pparam));
SortField[] sortfields = processQueryOrder(pparam);
IndexReader reader = null;
IndexSearcher searcher = null;
int numTotalHits = 0;
try {
reader = IndexReader.open(FSDirectory.open(loadLuceneIndexDirectory()));
searcher = new IndexSearcher(reader);
Sort sort = null;
if ( sortfields !=null && sortfields.length>0 ) {
sort = new Sort(sortfields);
} else {
sort = new Sort();
}
TopFieldDocs results = searcher.search(query, 5, sort);
numTotalHits = results.totalHits;
if ( query != null ) log.debug("query="+query.toString());
if ( sortfields != null ) {
for ( SortField sortField : sortfields ) {
log.debug("sort field="+sortField.getField() + " isReverse"+sortField.getReverse());
}
}
} catch (Exception e) {
log.error("",e);
} finally {
if ( searcher != null ) {
searcher.close();
}
if ( reader != null ) {
reader.close();
}
}
return numTotalHits;
}