List of usage examples for org.apache.lucene.search ScoreDoc ScoreDoc
public ScoreDoc(int doc, float score)
From source file:approxnn.DocIdSim.java
public ANNList(DocVector vec) { this.neighbors = new HashSet<>(); ScoreDoc sd = new ScoreDoc(vec.getId(), 0); this.neighbors.add(new DocIdSim(sd)); }
From source file:approxnn.DocIdSim.java
public ANNList(List<DocVector> vecs) { this.neighbors = new HashSet<>(); for (DocVector vec : vecs) { ScoreDoc sd = new ScoreDoc(vec.getId(), 0); this.neighbors.add(new DocIdSim(sd)); }//from w w w. j a v a2s. co m }
From source file:arena.lucene.LuceneIndexSearcherImpl.java
License:Open Source License
protected TopDocs executeSearch(IndexSearcher searcher, Query query, Filter filter, Sort sort, int collectorLimit) throws IOException { // Decide on how to search based on which elements of the lucene query model are available if (query != null) { // Full scoring search TopDocsCollector<? extends ScoreDoc> collector = null; if (sort == null) { collector = TopScoreDocCollector.create(collectorLimit, true); } else {/* w ww . j a v a 2 s.c o m*/ SortField sortFields[] = sort.getSort(); if (sortFields != null && sortFields.length > 0 && sortFields[0].getType() == SortField.SCORE && !sortFields[0].getReverse()) { collector = TopScoreDocCollector.create(collectorLimit, true); } else { collector = TopFieldCollector.create(sort, collectorLimit, false, true, true, true); } } searcher.search(query, filter, collector); return collector.topDocs(); } else if (filter != null) { // No query = no need for scoring, just dump the results into a hit collector that runs // off the results in the order we want DocIdSetIterator filterMatchesIterator = filter.getDocIdSet(searcher.getIndexReader()).iterator(); if (sort == null) { // no sort available, so the natural iteration order is fine // if we have an iterator that means sorting is already handled, so just pull off the first n rows into the output ScoreDoc[] scoreDocs = new ScoreDoc[collectorLimit]; int found = 0; int docId; while (found < collectorLimit && (docId = filterMatchesIterator.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) { scoreDocs[found++] = new ScoreDoc(docId, 1f); } return new TopDocs(found, found < collectorLimit ? Arrays.copyOf(scoreDocs, found) : scoreDocs, 1f); } else { TopDocsCollector<? extends ScoreDoc> collector = TopFieldCollector.create(sort, collectorLimit, false, true, true, true); int docId; while ((docId = filterMatchesIterator.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) { collector.collect(docId); } return collector.topDocs(); } } else if (sort != null) { // no query and no filter so no score but add every doc in the index for non-score sorting TopDocsCollector<? extends ScoreDoc> collector = TopFieldCollector.create(sort, collectorLimit, false, true, true, true); int numDocs = searcher.getIndexReader().numDocs(); for (int n = 0; n < numDocs; n++) { collector.collect(n); } return collector.topDocs(); } else { // no query filter or sort: return the top n docs ScoreDoc[] scoreDocs = new ScoreDoc[Math.min(collectorLimit, searcher.getIndexReader().numDocs())]; for (int n = 0; n < scoreDocs.length; n++) { scoreDocs[n] = new ScoreDoc(n, 1f); } return new TopDocs(scoreDocs.length, scoreDocs, 1f); } }
From source file:com.browseengine.local.service.geosearch.GeoSearchImpl.java
License:Open Source License
private GeoPriorityQueue innerSearch(LocalRequest request, Locatable knownCentroid) throws IOException, GeoSearchingException, TigerParseException, GeoCodingException { // TODO: desparately need caching to avoid re-running innerSearch for the standard // search(...) then fetch(...) API sequence Locatable point = knownCentroid;/* w w w . j a va 2 s . c om*/ if (null == point) { point = request.getPoint(); if (null == point) { // we need to figure out lon/lat Address address = request.getAddress(); if (address == null) { String query = request.getAddressStr(); if (query == null) { throw new GeoSearchingException( "you must specify at least one of point, address, or query on GeoSearch.search calls"); } // callback address = _geocoder.parseAddress(query); if (null == address) { throw new GeoSearchingException("unable to parse '" + query + "' into a valid address"); } } // callback point = _geocoder.lookupAddress(address); if (null == point) { throw new GeoSearchingException("unable to locate lon/lat for address '" + point + "'"); } } } float radius = request.getRangeInMiles(); if (radius <= 0f) { radius = 5f; } int[] bounds = HaversineWrapper.computeLonLatMinMaxAsInt(point, radius); int lowdocid = Arrays.binarySearch(_lons, bounds[HaversineWrapper.LON_MIN]); if (lowdocid < 0) { lowdocid = -1 - lowdocid; } // doc at lowdocid is the first candidate int highdocid = Arrays.binarySearch(_lons, bounds[HaversineWrapper.LON_MAX]); if (highdocid < 0) { highdocid = -1 - highdocid; // highdocid is actually too high a value highdocid--; } else { // highdocid is a valid match, as well as any following that are equal while (highdocid + 1 < _lons.length && _lons[highdocid] == _lons[highdocid + 1]) { highdocid++; } } // doc at highdocid is the last candidate if (highdocid < lowdocid) { return new GeoPriorityQueue(point, 20); } GeoPriorityQueue queue = new GeoPriorityQueue(point, MAX_DOC); // check every value in the region double lon1rad = point.getLongitudeRad(); double lat1rad = point.getLatitudeRad(); for (int i = lowdocid; i < highdocid; i++) { // check if a candidate if (_lats[i] >= bounds[HaversineWrapper.LAT_MIN] && _lats[i] <= bounds[HaversineWrapper.LAT_MAX]) { float distance = HaversineWrapper.computeHaversineDistanceMiles(lon1rad, lat1rad, _lons[i], _lats[i]); if (distance < radius) { // add to queue queue.insert(new ScoreDoc(i, distance)); } } } return queue; }
From source file:com.epam.wilma.message.search.lucene.search.LuceneSearchEngineTest.java
License:Open Source License
@BeforeMethod public void setUp() { underTest = spy(new LuceneSearchEngine()); MockitoAnnotations.initMocks(this); Whitebox.setInternalState(underTest, "topQueryHits", TOP_QUERY_HITS); scoreDocs = new ScoreDoc[1]; scoreDocs[0] = new ScoreDoc(12, 12); queryResult = new TopDocs(TOP_QUERY_HITS, scoreDocs, 10); document = new Document(); document.add(new StringField(FIELD_PATH, TEXT, null)); }
From source file:com.leavesfly.lia.commom.AllDocCollector.java
License:Apache License
public void collect(int doc) throws IOException { docs.add(new ScoreDoc(doc + docBase, // #A scorer.score())); // #B }
From source file:com.meltmedia.cadmium.search.SearchServiceTest.java
License:Apache License
@Before public void setupIndexReader() throws Exception { searcherProvider = mock(IndexSearcherProvider.class); index = mock(IndexSearcher.class); parser = mock(QueryParser.class); when(parser.parse("good_query")).thenReturn(new Query() { @Override//w w w.java 2 s . co m public String toString(String arg0) { return "good"; } }); when(parser.parse("bad_query")).thenReturn(new Query() { @Override public String toString(String arg0) { return "bad"; } }); TopDocs hasResults = new TopDocs(5, new ScoreDoc[] { new ScoreDoc(1, 1.1f), new ScoreDoc(2, 1.1f), new ScoreDoc(3, 1.1f), new ScoreDoc(4, 1.1f), new ScoreDoc(5, 1.1f) }, 5.5f); TopDocs noResults = new TopDocs(0, new ScoreDoc[] {}, 0.0f); when(index.search(parser.parse("good_query"), null, 100000)).thenReturn(hasResults); when(index.search(parser.parse("bad_query"), null, 100000)).thenReturn(noResults); Document one = new Document(); one.add(new TextField("path", "1", Field.Store.YES)); Document two = new Document(); two.add(new TextField("path", "2", Field.Store.YES)); Document three = new Document(); three.add(new TextField("path", "3", Field.Store.YES)); Document four = new Document(); four.add(new TextField("path", "4", Field.Store.YES)); Document five = new Document(); five.add(new TextField("path", "5", Field.Store.YES)); docs = new Document[] { one, two, three, four, five }; when(index.doc(1)).thenReturn(one); when(index.doc(2)).thenReturn(two); when(index.doc(3)).thenReturn(three); when(index.doc(4)).thenReturn(four); when(index.doc(5)).thenReturn(five); when(searcherProvider.startSearch()).thenReturn(index); service = new SearchService() { @Override QueryParser createParser(Analyzer analyzer) { return parser; } }; service.setIndexSearchProvider(searcherProvider); }
From source file:com.nearinfinity.blur.lucene.search.PagingCollector.java
License:Apache License
public PagingCollector(int numHits) { // creates an empty score doc so that i don't have to check for null // each time. this(numHits, new ScoreDoc(-1, Float.MAX_VALUE)); }
From source file:com.stratio.cassandra.lucene.service.SearchResultTest.java
License:Apache License
@Test public void testConstructor() { DecoratedKey decoratedKey = new BufferDecoratedKey(new LongToken((long) 10), ByteBufferUtil.EMPTY_BYTE_BUFFER); CellName clusteringKey = CellNames.simpleSparse(new ColumnIdentifier("aaaa", false)); ScoreDoc scoreDoc = new ScoreDoc(1, 2.0f); SearchResult searchResult = new SearchResult(decoratedKey, clusteringKey, scoreDoc); Assert.assertNotNull("SearchResult constructor must not return a null object", searchResult); }
From source file:com.stratio.cassandra.lucene.service.SearchResultTest.java
License:Apache License
@Test public void testGetPartitionKey() { DecoratedKey decoratedKey = new BufferDecoratedKey(new LongToken((long) 10), ByteBufferUtil.EMPTY_BYTE_BUFFER); CellName clusteringKey = CellNames.simpleSparse(new ColumnIdentifier("aaaa", false)); ScoreDoc scoreDoc = new ScoreDoc(1, 2.0f); SearchResult searchResult = new SearchResult(decoratedKey, clusteringKey, scoreDoc); Assert.assertEquals("SearchResult getPartitionKey must return the same object passed as parameter", decoratedKey, searchResult.getPartitionKey()); }