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)})))); } }
@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)); }
@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)); }
@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)); }
/** 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)); } }
/** 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)); } }
/** 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)); } }
/** 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)); } }
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)); } }
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); }
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)); } }
@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)); }
/** 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); }
/** 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, boolean doDocScores, boolean doMaxScore, Collector fc) throws IOException { if (sort == null) { throw new IllegalArgumentException("sort must not be null"); } return (TopFieldDocs) doSearch(searcher, null, q, filter, n, sort, doDocScores, doMaxScore, fc); }
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); } } } } }
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; }
public TopDocs topDocs(int start) throws IOException { TopDocs[] topdocs = this.sort == null ? new TopDocs[this.subs.size()] : new TopFieldDocs[this.subs.size()]; for (int i = 0; i < topdocs.length; i++) topdocs[i] = this.subs.get(i).topdocs; if (this.sort == null) return TopDocs.merge(start, numHits - start, topdocs, true); return TopDocs.merge(this.sort, start, this.numHits - start, (TopFieldDocs[]) topdocs, true); }
public Set<String> search(MultimodalItem item, int k) throws IOException { Set<String> set = new HashSet<String>(); IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); Point pt = ctx.makePoint(item.longitude, item.latitude); ValueSource valueSource = strategy.makeDistanceValueSource(pt); Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(indexSearcher); TopFieldDocs docs = indexSearcher.search(new MatchAllDocsQuery(), k, distSort); for(ScoreDoc scoreDoc : docs.scoreDocs) { Document doc = indexSearcher.doc(scoreDoc.doc); String otherId = doc.getField("id").stringValue(); if(item.id.equals(otherId)) continue; set.add(otherId); // try { // Number longitude = doc.getField("lng").numericValue(); // Number latitude = doc.getField("lat").numericValue(); // Double dist = GeodesicDistanceCalculator.vincentyDistance(latitude.doubleValue(), longitude.doubleValue(), item.latitude, item.longitude); // } // catch(Exception e){ // e.printStackTrace(); // } } indexReader.close(); return set; }
private void dumpDocs(Sort sort, TopFieldDocs docs) throws IOException { LOGGER.info("Sorted by: " + sort); ScoreDoc[] scoreDocs = docs.scoreDocs; for (int i = 0; i < scoreDocs.length; i++) { FieldDoc fieldDoc = (FieldDoc) scoreDocs[i]; Float distance = (Float) fieldDoc.fields[0]; StoredDocument doc = searcher.doc(fieldDoc.doc); LOGGER.info(" " + doc.get("name") + " @ (" + doc.get("x") + "," + doc.get("y") + ") -> " + distance); } }
public void testSingleValueAllSet() throws Exception { fillSingleValueAllSet(); 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(); bytesValues.setDocument(0); assertThat(bytesValues.count(), equalTo(1)); assertThat(bytesValues.valueAt(0), equalTo(new BytesRef(two()))); bytesValues.setDocument(1); assertThat(bytesValues.count(), equalTo(1)); assertThat(bytesValues.valueAt(0), equalTo(new BytesRef(one()))); bytesValues.setDocument(2); assertThat(bytesValues.count(), equalTo(1)); assertThat(bytesValues.valueAt(0), equalTo(new BytesRef(three()))); assertValues(bytesValues, 0, two()); assertValues(bytesValues, 1, one()); assertValues(bytesValues, 2, three()); IndexSearcher searcher = new IndexSearcher(readerContext.reader()); TopFieldDocs topDocs; 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[0].doc, equalTo(1)); assertThat(toString(((FieldDoc) topDocs.scoreDocs[0]).fields[0]), equalTo(one())); assertThat(topDocs.scoreDocs[1].doc, equalTo(0)); assertThat(toString(((FieldDoc) topDocs.scoreDocs[1]).fields[0]), equalTo(two())); assertThat(topDocs.scoreDocs[2].doc, equalTo(2)); assertThat(toString(((FieldDoc) topDocs.scoreDocs[2]).fields[0]), equalTo(three())); 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[0].doc, equalTo(2)); assertThat(topDocs.scoreDocs[1].doc, equalTo(0)); assertThat(topDocs.scoreDocs[2].doc, equalTo(1)); } }
public void testSortMultiValuesFields() throws Exception { fillExtendedMvSet(); IndexFieldData indexFieldData = getForField("value"); 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(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(7)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("!08")); assertThat(topDocs.scoreDocs[1].doc, equalTo(0)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("02")); assertThat(topDocs.scoreDocs[2].doc, equalTo(2)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("03")); assertThat(topDocs.scoreDocs[3].doc, equalTo(3)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("04")); assertThat(topDocs.scoreDocs[4].doc, equalTo(4)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("06")); assertThat(topDocs.scoreDocs[5].doc, equalTo(6)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("08")); assertThat(topDocs.scoreDocs[6].doc, equalTo(1)); assertThat((BytesRef) ((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null)); assertThat(topDocs.scoreDocs[7].doc, equalTo(5)); assertThat((BytesRef) ((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField("value", indexFieldData.comparatorSource(null, MultiValueMode.MAX, null), true))); assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(6)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("10")); assertThat(topDocs.scoreDocs[1].doc, equalTo(4)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("08")); assertThat(topDocs.scoreDocs[2].doc, equalTo(3)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("06")); assertThat(topDocs.scoreDocs[3].doc, equalTo(0)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("04")); assertThat(topDocs.scoreDocs[4].doc, equalTo(2)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("03")); assertThat(topDocs.scoreDocs[5].doc, equalTo(7)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("!10")); assertThat(topDocs.scoreDocs[6].doc, equalTo(1)); assertThat(((FieldDoc) topDocs.scoreDocs[6]).fields[0], equalTo(null)); assertThat(topDocs.scoreDocs[7].doc, equalTo(5)); assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], equalTo(null)); }
public void testSorting() throws Exception { IndexFieldData indexFieldData = getForField(parentType); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer)); IndexFieldData.XFieldComparatorSource comparator = indexFieldData.comparatorSource("_last", MultiValueMode.MIN, null); TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField(ParentFieldMapper.joinField(parentType), comparator, false))); assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(0)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[1].doc, equalTo(1)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[2].doc, equalTo(2)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[3].doc, equalTo(5)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[4].doc, equalTo(3)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[5].doc, equalTo(4)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[6].doc, equalTo(6)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[6]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[7].doc, equalTo(7)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[7]).fields[0]), equalTo(null)); topDocs = searcher.search(new MatchAllDocsQuery(), 10, new Sort(new SortField(ParentFieldMapper.joinField(parentType), comparator, true))); assertThat(topDocs.totalHits, equalTo(8)); assertThat(topDocs.scoreDocs.length, equalTo(8)); assertThat(topDocs.scoreDocs[0].doc, equalTo(3)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[1].doc, equalTo(4)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[2].doc, equalTo(6)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("2")); assertThat(topDocs.scoreDocs[3].doc, equalTo(0)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[4].doc, equalTo(1)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[5].doc, equalTo(2)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[5]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[6].doc, equalTo(5)); assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[6]).fields[0]).utf8ToString(), equalTo("1")); assertThat(topDocs.scoreDocs[7].doc, equalTo(7)); assertThat(((FieldDoc) topDocs.scoreDocs[7]).fields[0], nullValue()); }
@Test public void shouldGetSearchResults() 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); List<Track> result = spySearchManager.search(new TrackSearch("keywords")); assertThat("Result should have 2 tracks", result, hasSize(2)); Track track1 = result.get(0); assertThat("Track 1 artist ID should be 1231", track1.getArtistId(), equalTo("1231")); assertThat("Track 1 artist name should be 'Artist Name 1", track1.getArtistName(), equalTo("Artist Name 1")); assertThat("Track 1 artist image should be 'Artist Image 1", track1.getArtistImage(), equalTo("Artist Image 1")); assertThat("Track 1 album ID should be 4561", track1.getAlbumId(), equalTo("4561")); assertThat("Track 1 album name should be 'Album Name 1", track1.getAlbumName(), equalTo("Album Name 1")); assertThat("Track 1 album image should be 'Album Image 1", track1.getAlbumImage(), equalTo("Album Image 1")); assertThat("Track 1 year should be 2001", track1.getYear(), equalTo(2001)); assertThat("Track 1 track ID should be 7891", track1.getTrackId(), equalTo("7891")); assertThat("Track 1 track name should be 'Track Name 1", track1.getTrackName(), equalTo("Track Name 1")); assertThat("Track 1 number should be 1", track1.getNumber(), equalTo(1)); assertThat("Track 1 location should be 'Location 1'", track1.getLocation(), equalTo("Location 1")); assertThat("Track 1 is preferred should be true", track1.isPreferred(), equalTo(true)); assertThat("Track 1 should have 2 genres", track1.getGenres(), hasSize(2)); assertThat("Track 1 genre 0 should be 'Genre 1 1", track1.getGenres().get(0), equalTo("Genre 1 1")); assertThat("Track 1 genre 1 should be 'Genre 2 1", track1.getGenres().get(1), equalTo("Genre 2 1")); Track track2 = result.get(1); assertThat("Track 2 artist ID should be 1232", track2.getArtistId(), equalTo("1232")); assertThat("Track 2 artist name should be 'Artist Name 2", track2.getArtistName(), equalTo("Artist Name 2")); assertThat("Track 2 artist image should be 'Artist Image 2", track2.getArtistImage(), equalTo("Artist Image 2")); assertThat("Track 2 album ID should be 4562", track2.getAlbumId(), equalTo("4562")); assertThat("Track 2 album name should be 'Album Name 2", track2.getAlbumName(), equalTo("Album Name 2")); assertThat("Track 2 album image should be 'Album Image 2", track2.getAlbumImage(), equalTo("Album Image 2")); assertThat("Track 2 year should be 2002", track2.getYear(), equalTo(2002)); assertThat("Track 2 track ID should be 7892", track2.getTrackId(), equalTo("7892")); assertThat("Track 2 track name should be 'Track Name 2", track2.getTrackName(), equalTo("Track Name 2")); assertThat("Track 2 number should be 2", track2.getNumber(), equalTo(2)); assertThat("Track 2 location should be 'Location 2'", track2.getLocation(), equalTo("Location 2")); assertThat("Track 2 is preferred should be true", track2.isPreferred(), equalTo(false)); assertThat("Track 2 should have 2 genres", track2.getGenres(), hasSize(2)); assertThat("Track 2 genre 0 should be 'Genre 1 2", track2.getGenres().get(0), equalTo("Genre 1 2")); assertThat("Track 2 genre 1 should be 'Genre 2 2", track2.getGenres().get(1), equalTo("Genre 2 2")); }
@Override protected List<Lookup.LookupResult> createResults(IndexSearcher searcher, TopFieldDocs hits, int num, CharSequence key, boolean doHighlight, Set<String> matchedTokens, String prefixToken) throws IOException { BinaryDocValues textDV = MultiDocValues.getBinaryValues(searcher.getIndexReader(), TEXT_FIELD_NAME); assert textDV != null; // This will just be null if app didn't pass payloads to build(): // TODO: maybe just stored fields? they compress... BinaryDocValues payloadsDV = MultiDocValues.getBinaryValues(searcher.getIndexReader(), "payloads"); TreeSet<Lookup.LookupResult> results = new TreeSet<>(LOOKUP_COMP); // we reduce the num to the one initially requested int actualNum = num / numFactor; for (int i = 0; i < hits.scoreDocs.length; i++) { FieldDoc fd = (FieldDoc) hits.scoreDocs[i]; final String text = textDV.get(fd.doc).utf8ToString(); long weight = (Long) fd.fields[0]; BytesRef payload; if (payloadsDV != null) { payload = BytesRef.deepCopyOf(payloadsDV.get(fd.doc)); } else { payload = null; } double coefficient; if (text.startsWith(key.toString())) { // if hit starts with the key, we don't change the score coefficient = 1; } else { coefficient = createCoefficient(searcher, fd.doc, matchedTokens, prefixToken); } long score = (long) (weight * coefficient); LookupResult result; if (doHighlight) { result = new LookupResult(text, highlight(text, matchedTokens, prefixToken), score, payload); } else { result = new LookupResult(text, score, payload); } boundedTreeAdd(results, result, actualNum); } return new ArrayList<>(results.descendingSet()); }
public List<Document> retrievePageResult(P pparam) throws Exception { List<Document> documents = new ArrayList<Document>(); 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 ( results.totalHits > 0 ) { ScoreDoc[] hits = searcher.search(query, numTotalHits, sort).scoreDocs; int readpos = 0; int NO_OF_RESULT_PER_QUERY = 25; if ( !StringUtils.isEmpty(pparam.getResultIndex()) ) { readpos = Integer.parseInt(pparam.getResultIndex()); } if ( !StringUtils.isEmpty(pparam.getRecordPerPage()) ) { NO_OF_RESULT_PER_QUERY=Integer.parseInt(pparam.getRecordPerPage()); } for ( int i = readpos; i < readpos+NO_OF_RESULT_PER_QUERY && i < numTotalHits; i++ ) { Document tmpdoc = searcher.doc(hits[i].doc); documents.add(tmpdoc); } } 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 documents; }
public List<Map<String, Object>> search( final String queryString, // search value final Set<AttributeType> queryAttributes, // attribute(s) to search in final Set<AttributeType> responseAttributes, // attribute(s) to return final Set<ObjectType> objectTypes) // filter by object type(s) throws IOException { // TODO: wrap IOException, return something sensible return fullTextIndex.search( (final IndexReader indexReader, final TaxonomyReader taxonomyReader, final IndexSearcher indexSearcher) -> { final List<Map<String, Object>> results = Lists.newArrayList(); final Query query; try { if (objectTypes != null && !objectTypes.isEmpty()) { query = combine(constructQuery(queryAttributes, queryString), constructQuery(objectTypes)); } else { query = constructQuery(queryAttributes, queryString); } } catch (ParseException e) { // TODO: [ES] fix parsing of asterisk (wildcard) as first character LOGGER.info("Caught {} on {}", e.getMessage(), queryString); return Collections.<Map<String, Object>>emptyList(); } final TopFieldDocs topDocs = indexSearcher.search(query, MAX_SEARCH_RESULTS, SORT_BY_LOOKUP_KEY); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { final Document doc = indexSearcher.doc(scoreDoc.doc); final Map<String, Object> result = Maps.newLinkedHashMap(); result.put("key", doc.get(FullTextIndex.LOOKUP_KEY_FIELD_NAME)); result.put("type", doc.get(FullTextIndex.OBJECT_TYPE_FIELD_NAME)); for (final AttributeType attribute : responseAttributes) { final ObjectTemplate template = ObjectTemplate.getTemplate( ObjectType.getByName(doc.get(FullTextIndex.OBJECT_TYPE_FIELD_NAME))); if (template.getMultipleAttributes().contains(attribute)) { result.put(attribute.getName(), Lists.newArrayList(doc.getValues(attribute.getName()))); } else { result.put(attribute.getName(), doc.get(attribute.getName())); } } results.add(result); } return results; }); }