Sort convertToSort(JsonArray sortKeys) { if (sortKeys == null || sortKeys.size() == 0) return null; SortField[] sortFields = new SortField[sortKeys.size()]; for (int i = 0; i < sortKeys.size(); i++) { JsonObject sortKey = sortKeys.getJsonObject(i); String sortBy = sortKey.getString("sortBy"); boolean sortDescending = sortKey.getBoolean("sortDescending", false); String core = sortKey.getString("core", null); SortField field; if (sortBy.equals(SORT_ON_SCORE)) field = new SortField(null, SortField.Type.SCORE, !sortDescending); else { Type type = typeForSortField(sortKey.getString("type")); if (core == null || core.equals(coreName)) field = new SortField(sortBy, type, sortDescending); else field = new JoinSortField(sortBy, type, sortDescending, core); Object missingValue = missingSortValue(sortKey, type); if (missingValue != null) field.setMissingValue(missingValue); } sortFields[i] = field; } return new Sort(sortFields); }
private Object missingSortValue(JsonObject sortKey, Type type) { JsonValue missingValue = sortKey.getOrDefault("missingValue", null); if (missingValue == null) return null; if (missingValue instanceof JsonString) { if (((JsonString) missingValue).getString().equals("STRING_FIRST")) return SortField.STRING_FIRST; else if (((JsonString) missingValue).getString().equals("STRING_LAST")) return SortField.STRING_LAST; } else { switch (type) { case INT: return ((JsonNumber) missingValue).intValue(); case LONG: return ((JsonNumber) missingValue).longValue(); case DOUBLE: return ((JsonNumber) missingValue).doubleValue(); default: break; } } return null; }
/** * Finds the top n hits from whole Index where all results are after a * previous result (after) * * @param after * the last doc from previous search * @param nDocs * nb of elements to retrieve in total * @return */ public BrowseAllDocsResponse browseAllDocs(ScoreDoc after, int nDocs) { List<String> listOfDocReferences = new ArrayList<String>(); IndexSearcher searcher = null; try { searcher = iaviewSearcherManager.acquire(); TopDocs topDocs = searcher.searchAfter(after, new MatchAllDocsQuery(), nDocs, new Sort(new SortField(null, Type.DOC))); ScoreDoc scoreDoc = null; for (int i = 0; i < topDocs.scoreDocs.length; i++) { scoreDoc = topDocs.scoreDocs[i]; Document document = searcher.doc(scoreDoc.doc, new HashSet<String>(Arrays.asList(InformationAssetViewFields.DOCREFERENCE.toString()))); String docReferenceFromLuceneDocument = LuceneTaxonomyMapper .getDocReferenceFromLuceneDocument(document); listOfDocReferences.add(docReferenceFromLuceneDocument); } return new BrowseAllDocsResponse(listOfDocReferences, scoreDoc); } catch (IOException ioException) { throw new TaxonomyException(TaxonomyErrorType.LUCENE_IO_EXCEPTION, ioException); } finally { LuceneHelperTools.releaseSearcherManagerQuietly(iaviewSearcherManager, searcher); } }
@Override public SortField getSortField(boolean reverse) { if (reverse) { return new SortField(getFieldName(), Type.LONG, reverse); } return new SortField(getFieldName(), Type.LONG); }
@Override public SortField getSortField(boolean reverse) { if (reverse) { return new SortField(getFieldName(), Type.STRING, reverse); } return new SortField(getFieldName(), Type.STRING); }
@Override public SortField getSortField(boolean reverse) { if (reverse) { return new SortField(getFieldName(), Type.DOUBLE, reverse); } return new SortField(getFieldName(), Type.DOUBLE); }
@Override public SortField getSortField(boolean reverse) { if (reverse) { return new SortField(getFieldName(), Type.INT, reverse); } return new SortField(getFieldName(), Type.INT); }
@Override public SortField getSortField(boolean reverse) { if (reverse) { return new SortField(getFieldName(), Type.FLOAT, reverse); } return new SortField(getFieldName(), Type.FLOAT); }
@Test public void testSimpleSearchPagingWithSorting() throws Exception { IndexReader reader = getReaderFlatScore(length); IndexSearcherCloseable searcher = getSearcher(reader); TotalHitsRef totalHitsRef = new TotalHitsRef(); ProgressRef progressRef = new ProgressRef(); printHeapSize(); TermQuery query = new TermQuery(new Term("f1", "value")); Sort sort = new Sort(new SortField("index", Type.INT, true)); IterablePaging paging = new IterablePaging(new AtomicBoolean(true), searcher, query, 100, null, null, false, sort, new DeepPagingCache()); IterablePaging itPaging = paging.skipTo(90).gather(20).totalHits(totalHitsRef).progress(progressRef); BlurIterator<ScoreDoc, BlurException> iterator = itPaging.iterator(); int position = 90; int searches = 1; while (iterator.hasNext()) { ScoreDoc sd = iterator.next(); assertEquals(position, progressRef.currentHitPosition()); assertEquals(searches, progressRef.searchesPerformed()); System.out.println("time [" + progressRef.queryTime() + "] " + "total hits [" + totalHitsRef.totalHits() + "] " + "searches [" + progressRef.searchesPerformed() + "] " + "position [" + progressRef.currentHitPosition() + "] " + "doc id [" + sd.doc + "] " + "score [" + sd.score + "]"); position++; if (position == 100) { searches++; } } printHeapSize(); }
public FieldComparator<?> getComparator(String field, Type type, boolean reverse, final int numHits, final int sortPos, Object missingValue) { switch(type) { case STRING: return new JoinTermOrdValComparator(numHits, field, missingValue == SortField.STRING_LAST, this); case INT: return new JoinIntComparator(numHits, field, (Integer) missingValue, this); case DOUBLE: return new JoinDoubleComparator(numHits, field, (Double) missingValue, this); default: throw new IllegalStateException("Illegal join sort type: " + type); } }
private SortField.Type typeForSortField(String type) { switch (type) { case "String": return SortField.Type.STRING; case "Int": return SortField.Type.INT; case "Double": return SortField.Type.DOUBLE; case "Long": return SortField.Type.LONG; } return null; }
@Test public void testEmptyIndex() throws Exception { // LUCENE-5045: make sure DrillSideways works with an empty index Directory dir = newDirectory(); Directory taxoDir = newDirectory(); writer = new RandomIndexWriter(random(), dir); taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE); IndexSearcher searcher = newSearcher(writer.getReader()); writer.close(); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); taxoWriter.close(); // Count "Author" FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new CategoryPath("Author"), 10)); DrillSideways ds = new DrillSideways(searcher, taxoReader); DrillDownQuery ddq = new DrillDownQuery(fsp.indexingParams, new MatchAllDocsQuery()); ddq.add(new CategoryPath("Author", "Lisa")); DrillSidewaysResult r = ds.search(null, ddq, 10, fsp); // this used to fail on IllegalArgEx assertEquals(0, r.hits.totalHits); r = ds.search(ddq, null, null, 10, new Sort(new SortField("foo", Type.INT)), false, false, fsp); // this used to fail on IllegalArgEx assertEquals(0, r.hits.totalHits); IOUtils.close(searcher.getIndexReader(), taxoReader, dir, taxoDir); }
/** {@inheritDoc} */ @Override public SortField sortField(String field, boolean reverse) { return new SortField(field, Type.LONG, reverse); }
/** {@inheritDoc} */ @Override public SortField sortField(String field, boolean reverse) { return new SortField(field, Type.STRING, reverse); }
/** {@inheritDoc} */ @Override public SortField sortField(String field, boolean reverse) { return new SortField(field, Type.FLOAT, reverse); }
/** {@inheritDoc} */ @Override public SortField sortField(String field, boolean reverse) { return new SortField(field, Type.INT, reverse); }
/** {@inheritDoc} */ @Override public SortField sortField(String field, boolean reverse) { return new SortField(field, Type.DOUBLE, reverse); }
public void testRandomFieldNameSorts() throws Exception { SolrQueryRequest req = lrf.makeRequest("q", "*:*"); final int iters = atLeast(5000); // infinite loop abort when trying to generate a non-blank sort "name" final int nonBlankAttempts = 37; for (int i = 0; i < iters; i++) { final StringBuilder input = new StringBuilder(); final String[] names = new String[_TestUtil.nextInt(r,1,10)]; final boolean[] reverse = new boolean[names.length]; for (int j = 0; j < names.length; j++) { names[j] = null; for (int k = 0; k < nonBlankAttempts && null == names[j]; k++) { names[j] = _TestUtil.randomRealisticUnicodeString(r, 1, 100); // munge anything that might make this a function names[j] = names[j].replaceFirst("\\{","\\{\\{"); names[j] = names[j].replaceFirst("\\(","\\(\\("); names[j] = names[j].replaceFirst("(\\\"|\\')","$1$1z"); names[j] = names[j].replaceFirst("(\\d)","$1x"); // eliminate pesky problem chars names[j] = names[j].replaceAll("\\p{Cntrl}|\\p{javaWhitespace}",""); if (0 == names[j].length()) { names[j] = null; } } // with luck this bad, never go to vegas // alternatively: if (null == names[j]) names[j] = "never_go_to_vegas"; assertNotNull("Unable to generate a (non-blank) names["+j+"] after " + nonBlankAttempts + " attempts", names[j]); reverse[j] = r.nextBoolean(); input.append(r.nextBoolean() ? " " : ""); input.append(names[j]); input.append(" "); input.append(reverse[j] ? "desc," : "asc,"); } input.deleteCharAt(input.length()-1); SortField[] sorts = null; try { sorts = QueryParsing.parseSort(input.toString(), req).getSort(); } catch (RuntimeException e) { throw new RuntimeException("Failed to parse sort: " + input, e); } assertEquals("parsed sorts had unexpected size", names.length, sorts.length); for (int j = 0; j < names.length; j++) { assertEquals("sorts["+j+"] had unexpected reverse: " + input, reverse[j], sorts[j].getReverse()); final Type type = sorts[j].getType(); if (Type.SCORE.equals(type)) { assertEquals("sorts["+j+"] is (unexpectedly) type score : " + input, "score", names[j]); } else if (Type.DOC.equals(type)) { assertEquals("sorts["+j+"] is (unexpectedly) type doc : " + input, "_docid_", names[j]); } else if (Type.CUSTOM.equals(type) || Type.REWRITEABLE.equals(type)) { fail("sorts["+j+"] resulted in a '" + type.toString() + "', either sort parsing code is broken, or func/query " + "semantics have gotten broader and munging in this test " + "needs improved: " + input); } else { assertEquals("sorts["+j+"] ("+type.toString()+ ") had unexpected field in: " + input, names[j], sorts[j].getField()); } } } }
Lucene(Type fieldType) { this.fieldType = fieldType; }
/** * Creates a new instance with all properties. * @param id the id of the {@link ESortField}. * @param name the name of the {@link Field} in the {@link News} {@link Document} to which * will be sorted. * @param type the {@link Type} of the field set in {@link #name()}. * @param defaultOrder the default {@link ESortOrder} for this field. */ private ESortField(final int id, final String name, final Type type, final ESortOrder defaultOrder) { this.id = id; this.name = name; this.type = type; this.defaultOrder = defaultOrder; }