Java Code Examples for org.apache.lucene.search.SortField#getType()
The following examples show how to use
org.apache.lucene.search.SortField#getType() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestExpressionSorts.java From lucene-solr with Apache License 2.0 | 6 votes |
private DoubleValuesSource fromSortField(SortField field) { switch(field.getType()) { case INT: return DoubleValuesSource.fromIntField(field.getField()); case LONG: return DoubleValuesSource.fromLongField(field.getField()); case FLOAT: return DoubleValuesSource.fromFloatField(field.getField()); case DOUBLE: return DoubleValuesSource.fromDoubleField(field.getField()); case SCORE: return DoubleValuesSource.SCORES; default: throw new UnsupportedOperationException(); } }
Example 2
Source File: TestGrouping.java From lucene-solr with Apache License 2.0 | 6 votes |
@SuppressWarnings({"unchecked","rawtypes"}) private Comparable<?>[] fillFields(GroupDoc d, Sort sort) { final SortField[] sortFields = sort.getSort(); final Comparable<?>[] fields = new Comparable[sortFields.length]; for(int fieldIDX=0;fieldIDX<sortFields.length;fieldIDX++) { final Comparable<?> c; final SortField sf = sortFields[fieldIDX]; if (sf.getType() == SortField.Type.SCORE) { c = d.score; } else if (sf.getField().equals("sort1")) { c = d.sort1; } else if (sf.getField().equals("sort2")) { c = d.sort2; } else { assertEquals("id", sf.getField()); c = d.id; } fields[fieldIDX] = c; } return fields; }
Example 3
Source File: ShardFieldSortedHitQueue.java From lucene-solr with Apache License 2.0 | 6 votes |
Comparator<ShardDoc> getCachedComparator(SortField sortField, IndexSearcher searcher) { SortField.Type type = sortField.getType(); if (type == SortField.Type.SCORE) { return (o1, o2) -> { final float f1 = o1.score; final float f2 = o2.score; if (f1 < f2) return -1; if (f1 > f2) return 1; return 0; }; } else if (type == SortField.Type.REWRITEABLE) { try { sortField = sortField.rewrite(searcher); } catch (IOException e) { throw new SolrException(SERVER_ERROR, "Exception rewriting sort field " + sortField, e); } } return comparatorFieldComparator(sortField); }
Example 4
Source File: DocumentExpressionDictionaryFactory.java From lucene-solr with Apache License 2.0 | 6 votes |
private DoubleValuesSource fromSortField(SortField field) { switch(field.getType()) { case INT: return DoubleValuesSource.fromIntField(field.getField()); case LONG: return DoubleValuesSource.fromLongField(field.getField()); case FLOAT: return DoubleValuesSource.fromFloatField(field.getField()); case DOUBLE: return DoubleValuesSource.fromDoubleField(field.getField()); case SCORE: return DoubleValuesSource.SCORES; default: throw new UnsupportedOperationException(); } }
Example 5
Source File: WrappedIntPointField.java From lucene-solr with Apache License 2.0 | 6 votes |
private static DoubleValuesSource fromSortField(SortField field) { switch(field.getType()) { case INT: return DoubleValuesSource.fromIntField(field.getField()); case LONG: return DoubleValuesSource.fromLongField(field.getField()); case FLOAT: return DoubleValuesSource.fromFloatField(field.getField()); case DOUBLE: return DoubleValuesSource.fromDoubleField(field.getField()); case SCORE: return DoubleValuesSource.SCORES; default: throw new UnsupportedOperationException(); } }
Example 6
Source File: SortParseElement.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public void parse(XContentParser parser, SearchContext context) throws Exception { XContentParser.Token token = parser.currentToken(); List<SortField> sortFields = new ArrayList<>(2); if (token == XContentParser.Token.START_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (token == XContentParser.Token.START_OBJECT) { addCompoundSortField(parser, context, sortFields); } else if (token == XContentParser.Token.VALUE_STRING) { addSortField(context, sortFields, parser.text(), false, null, null, null, null); } else { throw new IllegalArgumentException("malformed sort format, within the sort array, an object, or an actual string are allowed"); } } } else if (token == XContentParser.Token.VALUE_STRING) { addSortField(context, sortFields, parser.text(), false, null, null, null, null); } else if (token == XContentParser.Token.START_OBJECT) { addCompoundSortField(parser, context, sortFields); } else { throw new IllegalArgumentException("malformed sort format, either start with array, object, or an actual string"); } if (!sortFields.isEmpty()) { // optimize if we just sort on score non reversed, we don't really need sorting boolean sort; if (sortFields.size() > 1) { sort = true; } else { SortField sortField = sortFields.get(0); if (sortField.getType() == SortField.Type.SCORE && !sortField.getReverse()) { sort = false; } else { sort = true; } } if (sort) { context.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()]))); } } }
Example 7
Source File: TestExpressionSorts.java From lucene-solr with Apache License 2.0 | 5 votes |
void assertQuery(Query query, Sort sort) throws Exception { int size = TestUtil.nextInt(random(), 1, searcher.getIndexReader().maxDoc() / 5); TopDocs expected = searcher.search(query, size, sort, random().nextBoolean()); // make our actual sort, mutating original by replacing some of the // sortfields with equivalent expressions SortField original[] = sort.getSort(); SortField mutated[] = new SortField[original.length]; for (int i = 0; i < mutated.length; i++) { if (random().nextInt(3) > 0) { SortField s = original[i]; Expression expr = JavascriptCompiler.compile(s.getField()); SimpleBindings simpleBindings = new SimpleBindings(); simpleBindings.add(s.getField(), fromSortField(s)); boolean reverse = s.getType() == SortField.Type.SCORE || s.getReverse(); mutated[i] = expr.getSortField(simpleBindings, reverse); } else { mutated[i] = original[i]; } } Sort mutatedSort = new Sort(mutated); TopDocs actual = searcher.search(query, size, mutatedSort, random().nextBoolean()); CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs); if (size < actual.totalHits.value) { expected = searcher.searchAfter(expected.scoreDocs[size-1], query, size, sort); actual = searcher.searchAfter(actual.scoreDocs[size-1], query, size, mutatedSort); CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs); } }
Example 8
Source File: TestGrouping.java From lucene-solr with Apache License 2.0 | 5 votes |
private Comparator<GroupDoc> getComparator(Sort sort) { final SortField[] sortFields = sort.getSort(); return new Comparator<GroupDoc>() { @Override public int compare(GroupDoc d1, GroupDoc d2) { for(SortField sf : sortFields) { final int cmp; if (sf.getType() == SortField.Type.SCORE) { if (d1.score > d2.score) { cmp = -1; } else if (d1.score < d2.score) { cmp = 1; } else { cmp = 0; } } else if (sf.getField().equals("sort1")) { cmp = d1.sort1.compareTo(d2.sort1); } else if (sf.getField().equals("sort2")) { cmp = d1.sort2.compareTo(d2.sort2); } else { assertEquals(sf.getField(), "id"); cmp = d1.id - d2.id; } if (cmp != 0) { return sf.getReverse() ? -cmp : cmp; } } // Our sort always fully tie breaks: fail(); return 0; } }; }
Example 9
Source File: AllGroupHeadsCollectorTest.java From lucene-solr with Apache License 2.0 | 5 votes |
private Comparator<GroupDoc> getComparator(Sort sort, final boolean sortByScoreOnly, final int[] fieldIdToDocID) { final SortField[] sortFields = sort.getSort(); return new Comparator<GroupDoc>() { @Override public int compare(GroupDoc d1, GroupDoc d2) { for (SortField sf : sortFields) { final int cmp; if (sf.getType() == SortField.Type.SCORE) { if (d1.score > d2.score) { cmp = -1; } else if (d1.score < d2.score) { cmp = 1; } else { cmp = sortByScoreOnly ? fieldIdToDocID[d1.id] - fieldIdToDocID[d2.id] : 0; } } else if (sf.getField().equals("sort1")) { cmp = d1.sort1.compareTo(d2.sort1); } else if (sf.getField().equals("sort2")) { cmp = d1.sort2.compareTo(d2.sort2); } else if (sf.getField().equals("sort3")) { cmp = d1.sort3.compareTo(d2.sort3); } else { assertEquals(sf.getField(), "id"); cmp = d1.id - d2.id; } if (cmp != 0) { return sf.getReverse() ? -cmp : cmp; } } // Our sort always fully tie breaks: fail(); return 0; } }; }
Example 10
Source File: SortSpec.java From lucene-solr with Apache License 2.0 | 5 votes |
public static boolean includesScore(Sort sort) { if (sort==null) return true; for (SortField sf : sort.getSort()) { if (sf.getType() == SortField.Type.SCORE) return true; } return false; }
Example 11
Source File: QueryElevationComponent.java From lucene-solr with Apache License 2.0 | 5 votes |
private SortSpec modifySortSpec(SortSpec current, boolean forceElevation, ElevationComparatorSource comparator) { boolean modify = false; SortField[] currentSorts = current.getSort().getSort(); List<SchemaField> currentFields = current.getSchemaFields(); ArrayList<SortField> sorts = new ArrayList<>(currentSorts.length + 1); List<SchemaField> fields = new ArrayList<>(currentFields.size() + 1); // Perhaps force it to always sort by score if (forceElevation && currentSorts[0].getType() != SortField.Type.SCORE) { sorts.add(new SortField("_elevate_", comparator, true)); fields.add(null); modify = true; } for (int i = 0; i < currentSorts.length; i++) { SortField sf = currentSorts[i]; if (sf.getType() == SortField.Type.SCORE) { sorts.add(new SortField("_elevate_", comparator, !sf.getReverse())); fields.add(null); modify = true; } sorts.add(sf); fields.add(currentFields.get(i)); } return modify ? new SortSpec(new Sort(sorts.toArray(new SortField[0])), fields, current.getCount(), current.getOffset()) : null; }
Example 12
Source File: ShardFieldSortedHitQueue.java From BioSolr with Apache License 2.0 | 5 votes |
Comparator<ShardDoc> getCachedComparator(SortField sortField, IndexSearcher searcher) { SortField.Type type = sortField.getType(); if (type == SortField.Type.SCORE) { return comparatorScore(); } else if (type == SortField.Type.REWRITEABLE) { try { sortField = sortField.rewrite(searcher); } catch (IOException e) { throw new SolrException(SERVER_ERROR, "Exception rewriting sort field " + sortField, e); } } return comparatorFieldComparator(sortField); }