List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:org.apache.solr.search.SpatialQParser.java
License:Apache License
@Override public Query parse() throws ParseException { // this will combine the results that are found for each // administrative level BooleanQuery allQuery = new BooleanQuery(); // attempt to create a query on city (low level administrative division) String city = localParams.get(CITY); if (city != null) { city = city.toLowerCase();//from w w w .j a va 2 s . c om SchemaField nameField = req.getSchema().getField(NAME_FIELD); FieldType nameFieldType = nameField.getType(); Query cityQuery = nameFieldType.getFieldQuery(this, nameField, city); allQuery.add(cityQuery, Occur.MUST); } // attempt to create a query on state (mid level administrative division) String state = localParams.get(STATE); if (state != null) { state = state.toLowerCase(); SchemaField stateField = req.getSchema().getField(STATE_FIELD); FieldType stateFieldType = stateField.getType(); Query stateQuery = stateFieldType.getFieldQuery(this, stateField, state); allQuery.add(stateQuery, Occur.MUST); } // attempt to create a query on city (high level administrative division) String country = localParams.get(COUNTRY); if (country != null) { country = country.toLowerCase(); SchemaField countryField = req.getSchema().getField(COUNTRY_FIELD); FieldType countryFieldType = countryField.getType(); Query countryQuery = countryFieldType.getFieldQuery(this, countryField, country); allQuery.add(countryQuery, Occur.MUST); } String latitude = null; String longitude = null; // no location provided, computer user's location via reverse-ip lookup if (allQuery.getClauses().length == 0) { HttpServletRequest httpreq = req.getHttpServletRequest(); String ip = httpreq.getRemoteAddr(); LatLng currLoc = geoTargeter.getCurrentLocation(ip); if (currLoc != null) { latitude = Double.toString(currLoc.getLat()); longitude = Double.toString(currLoc.getLng()); } } else { SolrIndexSearcher searcher = req.getSearcher(); Document geocodeDoc = null; try { Sort s = new Sort(new SortField(POPULATION_FIELD, SortField.LONG, true)); DocList docs = searcher.getDocList(allQuery, new ArrayList<Query>(), s, 0, 1, 0); if (docs == null) return query; DocIterator iter = docs.iterator(); int geocodeDocId = iter.nextDoc(); geocodeDoc = searcher.doc(geocodeDocId); } catch (Exception e) { e.printStackTrace(); return query; } latitude = geocodeDoc.get("latitude"); longitude = geocodeDoc.get("longitude"); } // combine the spatial and free-text queries BooleanQuery finalQuery = new BooleanQuery(); // if no location is provided and user's location cannot be determined, // do not search location if (latitude != null && longitude != null) { String distance = localParams.get(DISTANCE); try { Double.parseDouble(distance); } catch (Exception e) { distance = SEARCH_RADIUS; } SpatialFilterQParserPlugin spatialFilter = new SpatialFilterQParserPlugin(); ModifiableSolrParams spatialParams = new ModifiableSolrParams(); spatialParams.add(SpatialParams.POINT, latitude + "," + longitude); spatialParams.add(SpatialParams.DISTANCE, distance); spatialParams.add(CommonParams.FL, LOCATION_FIELD); Query spatialQuery = spatialFilter.createParser(qstr, spatialParams, spatialParams, req).parse(); finalQuery.add(spatialQuery, Occur.MUST); } // get results from default LuceneQParser Query defQuery = new LuceneQParserPlugin().createParser(qstr, localParams, params, req).parse(); finalQuery.add(defQuery, Occur.MUST); return finalQuery; }
From source file:org.apache.solr.search.TestSort.java
License:Apache License
public void testSort() throws Exception { Directory dir = new RAMDirectory(); Field f = new StringField("f", "0", Field.Store.NO); Field f2 = new StringField("f2", "0", Field.Store.NO); for (int iterCnt = 0; iterCnt < iter; iterCnt++) { IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new SimpleAnalyzer(TEST_VERSION_CURRENT)) .setOpenMode(IndexWriterConfig.OpenMode.CREATE)); final MyDoc[] mydocs = new MyDoc[ndocs]; int v1EmptyPercent = 50; int v2EmptyPercent = 50; int commitCountdown = commitCount; for (int i = 0; i < ndocs; i++) { MyDoc mydoc = new MyDoc(); mydoc.doc = i;/*from w ww .ja v a2 s .com*/ mydocs[i] = mydoc; Document document = new Document(); if (r.nextInt(100) < v1EmptyPercent) { mydoc.val = Integer.toString(r.nextInt(maxval)); f.setStringValue(mydoc.val); document.add(f); } if (r.nextInt(100) < v2EmptyPercent) { mydoc.val2 = Integer.toString(r.nextInt(maxval)); f2.setStringValue(mydoc.val2); document.add(f2); } iw.addDocument(document); if (--commitCountdown <= 0) { commitCountdown = commitCount; iw.commit(); } } iw.close(); DirectoryReader reader = DirectoryReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); // System.out.println("segments="+searcher.getIndexReader().getSequentialSubReaders().length); assertTrue(reader.leaves().size() > 1); for (int i = 0; i < qiter; i++) { Filter filt = new Filter() { @Override public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) { return BitsFilteredDocIdSet.wrap(randSet(context.reader().maxDoc()), acceptDocs); } }; int top = r.nextInt((ndocs >> 3) + 1) + 1; final boolean luceneSort = r.nextBoolean(); final boolean sortMissingLast = !luceneSort && r.nextBoolean(); final boolean sortMissingFirst = !luceneSort && !sortMissingLast; final boolean reverse = r.nextBoolean(); List<SortField> sfields = new ArrayList<SortField>(); final boolean secondary = r.nextBoolean(); final boolean luceneSort2 = r.nextBoolean(); final boolean sortMissingLast2 = !luceneSort2 && r.nextBoolean(); final boolean sortMissingFirst2 = !luceneSort2 && !sortMissingLast2; final boolean reverse2 = r.nextBoolean(); if (r.nextBoolean()) sfields.add(new SortField(null, SortField.Type.SCORE)); // hit both use-cases of sort-missing-last sfields.add(Sorting.getStringSortField("f", reverse, sortMissingLast, sortMissingFirst)); if (secondary) { sfields.add(Sorting.getStringSortField("f2", reverse2, sortMissingLast2, sortMissingFirst2)); } if (r.nextBoolean()) sfields.add(new SortField(null, SortField.Type.SCORE)); Sort sort = new Sort(sfields.toArray(new SortField[sfields.size()])); final String nullRep = luceneSort || sortMissingFirst && !reverse || sortMissingLast && reverse ? "" : "zzz"; final String nullRep2 = luceneSort2 || sortMissingFirst2 && !reverse2 || sortMissingLast2 && reverse2 ? "" : "zzz"; boolean trackScores = r.nextBoolean(); boolean trackMaxScores = r.nextBoolean(); boolean scoreInOrder = r.nextBoolean(); final TopFieldCollector topCollector = TopFieldCollector.create(sort, top, true, trackScores, trackMaxScores, scoreInOrder); final List<MyDoc> collectedDocs = new ArrayList<MyDoc>(); // delegate and collect docs ourselves Collector myCollector = new Collector() { int docBase; @Override public void setScorer(Scorer scorer) throws IOException { topCollector.setScorer(scorer); } @Override public void collect(int doc) throws IOException { topCollector.collect(doc); collectedDocs.add(mydocs[doc + docBase]); } @Override public void setNextReader(AtomicReaderContext context) throws IOException { topCollector.setNextReader(context); docBase = context.docBase; } @Override public boolean acceptsDocsOutOfOrder() { return topCollector.acceptsDocsOutOfOrder(); } }; searcher.search(new MatchAllDocsQuery(), filt, myCollector); Collections.sort(collectedDocs, new Comparator<MyDoc>() { @Override public int compare(MyDoc o1, MyDoc o2) { String v1 = o1.val == null ? nullRep : o1.val; String v2 = o2.val == null ? nullRep : o2.val; int cmp = v1.compareTo(v2); if (reverse) cmp = -cmp; if (cmp != 0) return cmp; if (secondary) { v1 = o1.val2 == null ? nullRep2 : o1.val2; v2 = o2.val2 == null ? nullRep2 : o2.val2; cmp = v1.compareTo(v2); if (reverse2) cmp = -cmp; } cmp = cmp == 0 ? o1.doc - o2.doc : cmp; return cmp; } }); TopDocs topDocs = topCollector.topDocs(); ScoreDoc[] sdocs = topDocs.scoreDocs; for (int j = 0; j < sdocs.length; j++) { int id = sdocs[j].doc; if (id != collectedDocs.get(j).doc) { log.error("Error at pos " + j + "\n\tsortMissingFirst=" + sortMissingFirst + " sortMissingLast=" + sortMissingLast + " reverse=" + reverse + "\n\tEXPECTED=" + collectedDocs); } assertEquals(id, collectedDocs.get(j).doc); } } reader.close(); } dir.close(); }
From source file:org.apache.solr.uninverting.TestFieldCacheSort.java
License:Apache License
/** Tests sorting on type string */ private void testString(SortField.Type sortType) throws IOException { Directory dir = newDirectory();/*from w w w. j a v a2 s . c o m*/ RandomIndexWriter writer = new RandomIndexWriter(random(), dir); Document doc = new Document(); doc.add(newStringField("value", "foo", Field.Store.YES)); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "bar", Field.Store.YES)); writer.addDocument(doc); Type type = sortType == SortField.Type.STRING ? Type.SORTED : Type.BINARY; IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", type)); writer.close(); IndexSearcher searcher = newSearcher(ir); Sort sort = new Sort(new SortField("value", sortType)); TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); assertEquals(2, td.totalHits); // 'bar' comes before 'foo' assertEquals("bar", searcher.doc(td.scoreDocs[0].doc).get("value")); assertEquals("foo", searcher.doc(td.scoreDocs[1].doc).get("value")); TestUtil.checkReader(ir); ir.close(); dir.close(); }
From source file:org.apache.solr.uninverting.TestFieldCacheSort.java
License:Apache License
/** Tests sorting on type string with a missing value */ private void testStringMissing(SortField.Type sortType) throws IOException { Directory dir = newDirectory();/* w w w. j a va2s. com*/ RandomIndexWriter writer = new RandomIndexWriter(random(), dir); Document doc = new Document(); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "foo", Field.Store.YES)); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "bar", Field.Store.YES)); writer.addDocument(doc); Type type = sortType == SortField.Type.STRING ? Type.SORTED : Type.BINARY; IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", type)); writer.close(); IndexSearcher searcher = newSearcher(ir); Sort sort = new Sort(new SortField("value", sortType)); TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); assertEquals(3, td.totalHits); // null comes first assertNull(searcher.doc(td.scoreDocs[0].doc).get("value")); assertEquals("bar", searcher.doc(td.scoreDocs[1].doc).get("value")); assertEquals("foo", searcher.doc(td.scoreDocs[2].doc).get("value")); TestUtil.checkReader(ir); ir.close(); dir.close(); }
From source file:org.apache.solr.uninverting.TestFieldCacheSort.java
License:Apache License
/** Tests reverse sorting on type string */ private void testStringReverse(SortField.Type sortType) throws IOException { Directory dir = newDirectory();/*from w w w. j av a 2 s . c o m*/ RandomIndexWriter writer = new RandomIndexWriter(random(), dir); Document doc = new Document(); doc.add(newStringField("value", "bar", Field.Store.YES)); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "foo", Field.Store.YES)); writer.addDocument(doc); Type type = sortType == SortField.Type.STRING ? Type.SORTED : Type.BINARY; IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", type)); writer.close(); IndexSearcher searcher = newSearcher(ir); Sort sort = new Sort(new SortField("value", sortType, true)); TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); assertEquals(2, td.totalHits); // 'foo' comes after 'bar' in reverse order assertEquals("foo", searcher.doc(td.scoreDocs[0].doc).get("value")); assertEquals("bar", searcher.doc(td.scoreDocs[1].doc).get("value")); TestUtil.checkReader(ir); ir.close(); dir.close(); }
From source file:org.apache.solr.uninverting.TestFieldCacheSort.java
License:Apache License
/** Tests sorting on type string with a missing * value sorted first *///from w w w . java 2 s. c o m private void testStringMissingSortedFirst(SortField.Type sortType) throws IOException { Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir); Document doc = new Document(); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "foo", Field.Store.YES)); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "bar", Field.Store.YES)); writer.addDocument(doc); Type type = sortType == SortField.Type.STRING ? Type.SORTED : Type.BINARY; IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", type)); writer.close(); IndexSearcher searcher = newSearcher(ir); SortField sf = new SortField("value", sortType); Sort sort = new Sort(sf); TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); assertEquals(3, td.totalHits); // null comes first assertNull(searcher.doc(td.scoreDocs[0].doc).get("value")); assertEquals("bar", searcher.doc(td.scoreDocs[1].doc).get("value")); assertEquals("foo", searcher.doc(td.scoreDocs[2].doc).get("value")); TestUtil.checkReader(ir); ir.close(); dir.close(); }
From source file:org.apache.solr.uninverting.TestFieldCacheSort.java
License:Apache License
/** Tests reverse sorting on type string with a missing * value sorted first */// w ww. j a v a 2 s .c om private void testStringMissingSortedFirstReverse(SortField.Type sortType) throws IOException { Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir); Document doc = new Document(); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "foo", Field.Store.YES)); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "bar", Field.Store.YES)); writer.addDocument(doc); Type type = sortType == SortField.Type.STRING ? Type.SORTED : Type.BINARY; IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", type)); writer.close(); IndexSearcher searcher = newSearcher(ir); SortField sf = new SortField("value", sortType, true); Sort sort = new Sort(sf); TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); assertEquals(3, td.totalHits); assertEquals("foo", searcher.doc(td.scoreDocs[0].doc).get("value")); assertEquals("bar", searcher.doc(td.scoreDocs[1].doc).get("value")); // null comes last assertNull(searcher.doc(td.scoreDocs[2].doc).get("value")); TestUtil.checkReader(ir); ir.close(); dir.close(); }
From source file:org.apache.solr.uninverting.TestFieldCacheSort.java
License:Apache License
/** Tests sorting on type string with a missing * value sorted last *//*from w w w .ja v a 2 s. com*/ private void testStringMissingSortedLast(SortField.Type sortType) throws IOException { Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir); Document doc = new Document(); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "foo", Field.Store.YES)); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "bar", Field.Store.YES)); writer.addDocument(doc); Type type = sortType == SortField.Type.STRING ? Type.SORTED : Type.BINARY; IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", type)); writer.close(); IndexSearcher searcher = newSearcher(ir); SortField sf = new SortField("value", sortType); sf.setMissingValue(SortField.STRING_LAST); Sort sort = new Sort(sf); TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); assertEquals(3, td.totalHits); assertEquals("bar", searcher.doc(td.scoreDocs[0].doc).get("value")); assertEquals("foo", searcher.doc(td.scoreDocs[1].doc).get("value")); // null comes last assertNull(searcher.doc(td.scoreDocs[2].doc).get("value")); TestUtil.checkReader(ir); ir.close(); dir.close(); }
From source file:org.apache.solr.uninverting.TestFieldCacheSort.java
License:Apache License
/** Tests reverse sorting on type string with a missing * value sorted last *//*from w w w.j a v a 2s. c o m*/ private void testStringMissingSortedLastReverse(SortField.Type sortType) throws IOException { Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir); Document doc = new Document(); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "foo", Field.Store.YES)); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "bar", Field.Store.YES)); writer.addDocument(doc); Type type = sortType == SortField.Type.STRING ? Type.SORTED : Type.BINARY; IndexReader ir = UninvertingReader.wrap(writer.getReader(), Collections.singletonMap("value", type)); writer.close(); IndexSearcher searcher = newSearcher(ir); SortField sf = new SortField("value", sortType, true); sf.setMissingValue(SortField.STRING_LAST); Sort sort = new Sort(sf); TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); assertEquals(3, td.totalHits); // null comes first assertNull(searcher.doc(td.scoreDocs[0].doc).get("value")); assertEquals("foo", searcher.doc(td.scoreDocs[1].doc).get("value")); assertEquals("bar", searcher.doc(td.scoreDocs[2].doc).get("value")); TestUtil.checkReader(ir); ir.close(); dir.close(); }
From source file:org.apache.solr.uninverting.TestFieldCacheSort.java
License:Apache License
/** Tests sorting on internal docid order */ public void testFieldDoc() throws Exception { Directory dir = newDirectory();//w w w . j a va 2 s. c om RandomIndexWriter writer = new RandomIndexWriter(random(), dir); Document doc = new Document(); doc.add(newStringField("value", "foo", Field.Store.NO)); writer.addDocument(doc); doc = new Document(); doc.add(newStringField("value", "bar", Field.Store.NO)); writer.addDocument(doc); IndexReader ir = writer.getReader(); writer.close(); IndexSearcher searcher = newSearcher(ir); Sort sort = new Sort(SortField.FIELD_DOC); TopDocs td = searcher.search(new MatchAllDocsQuery(), 10, sort); assertEquals(2, td.totalHits); // docid 0, then docid 1 assertEquals(0, td.scoreDocs[0].doc); assertEquals(1, td.scoreDocs[1].doc); TestUtil.checkReader(ir); ir.close(); dir.close(); }