本文整理匯總了Java中org.apache.lucene.search.SortField.getReverse方法的典型用法代碼示例。如果您正苦於以下問題:Java SortField.getReverse方法的具體用法?Java SortField.getReverse怎麽用?Java SortField.getReverse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.lucene.search.SortField
的用法示例。
在下文中一共展示了SortField.getReverse方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: buildSort
import org.apache.lucene.search.SortField; //導入方法依賴的package包/類
public static Optional<SortAndFormats> buildSort(List<SortBuilder<?>> sortBuilders, QueryShardContext context) throws IOException {
List<SortField> sortFields = new ArrayList<>(sortBuilders.size());
List<DocValueFormat> sortFormats = new ArrayList<>(sortBuilders.size());
for (SortBuilder<?> builder : sortBuilders) {
SortFieldAndFormat sf = builder.build(context);
sortFields.add(sf.field);
sortFormats.add(sf.format);
}
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) {
return Optional.of(new SortAndFormats(
new Sort(sortFields.toArray(new SortField[sortFields.size()])),
sortFormats.toArray(new DocValueFormat[sortFormats.size()])));
}
}
return Optional.empty();
}
示例2: parse
import org.apache.lucene.search.SortField; //導入方法依賴的package包/類
@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()])));
}
}
}
示例3: getSort
import org.apache.lucene.search.SortField; //導入方法依賴的package包/類
public static final Sort getSort(EntityManager entityManager, Class<?> entityClass, SortField... sortFields) {
if (sortFields == null || sortFields.length == 0) {
return null;
} else {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
.forEntity(entityClass).get();
SortContext context = queryBuilder.sort();
SortFieldContext fieldContext = null;
for (SortField sortField : sortFields) {
if (fieldContext == null) {
fieldContext = context.byField(sortField.getField());
} else {
fieldContext = fieldContext.andByField(sortField.getField());
}
if ( ! sortField.getReverse()) {
fieldContext.asc();
} else {
fieldContext.desc();
}
}
if (fieldContext != null) {
return fieldContext.createSort();
} else {
return null;
}
}
}
示例4: assertQuery
import org.apache.lucene.search.SortField; //導入方法依賴的package包/類
void assertQuery(Query query, Filter filter, Sort sort) throws Exception {
int size = TestUtil.nextInt(random(), 1, searcher.getIndexReader().maxDoc() / 5);
TopDocs expected = searcher.search(query, filter, size, sort, random().nextBoolean(), 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);
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, filter, size, mutatedSort, random().nextBoolean(), random().nextBoolean());
CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs);
if (size < actual.totalHits) {
expected = searcher.searchAfter(expected.scoreDocs[size-1], query, filter, size, sort);
actual = searcher.searchAfter(actual.scoreDocs[size-1], query, filter, size, mutatedSort);
CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs);
}
}
示例5: GroupComparator
import org.apache.lucene.search.SortField; //導入方法依賴的package包/類
public GroupComparator(Sort groupSort) throws IOException {
final SortField[] sortFields = groupSort.getSort();
comparators = new FieldComparator<?>[sortFields.length];
reversed = new int[sortFields.length];
for (int compIDX = 0; compIDX < sortFields.length; compIDX++) {
final SortField sortField = sortFields[compIDX];
comparators[compIDX] = sortField.getComparator(1, compIDX);
reversed[compIDX] = sortField.getReverse() ? -1 : 1;
}
}
示例6: getComparator
import org.apache.lucene.search.SortField; //導入方法依賴的package包/類
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;
}
};
}
示例7: getSortDoc
import org.apache.lucene.search.SortField; //導入方法依賴的package包/類
private SortDoc getSortDoc(SolrIndexSearcher searcher, SortField[] sortFields) throws IOException {
SortValue[] sortValues = new SortValue[sortFields.length];
IndexSchema schema = searcher.getSchema();
for(int i=0; i<sortFields.length; ++i) {
SortField sf = sortFields[i];
String field = sf.getField();
boolean reverse = sf.getReverse();
SchemaField schemaField = schema.getField(field);
FieldType ft = schemaField.getType();
if(!schemaField.hasDocValues()) {
throw new IOException(field+" must have DocValues to use this feature.");
}
if(ft instanceof TrieIntField) {
if(reverse) {
sortValues[i] = new IntValue(field, new IntDesc());
} else {
sortValues[i] = new IntValue(field, new IntAsc());
}
} else if(ft instanceof TrieFloatField) {
if(reverse) {
sortValues[i] = new FloatValue(field, new FloatDesc());
} else {
sortValues[i] = new FloatValue(field, new FloatAsc());
}
} else if(ft instanceof TrieDoubleField) {
if(reverse) {
sortValues[i] = new DoubleValue(field, new DoubleDesc());
} else {
sortValues[i] = new DoubleValue(field, new DoubleAsc());
}
} else if(ft instanceof TrieLongField) {
if(reverse) {
sortValues[i] = new LongValue(field, new LongDesc());
} else {
sortValues[i] = new LongValue(field, new LongAsc());
}
} else if(ft instanceof StrField) {
AtomicReader reader = searcher.getAtomicReader();
SortedDocValues vals = reader.getSortedDocValues(field);
if(reverse) {
sortValues[i] = new StringValue(vals, field, new IntDesc());
} else {
sortValues[i] = new StringValue(vals, field, new IntAsc());
}
} else {
throw new IOException("Sort fields must be one of the following types: int,float,long,double,string");
}
}
if(sortValues.length == 1) {
return new SingleValueSortDoc(sortValues[0]);
} else if(sortValues.length == 2) {
return new DoubleValueSortDoc(sortValues[0], sortValues[1]);
} else if(sortValues.length == 3) {
return new TripleValueSortDoc(sortValues[0], sortValues[1], sortValues[2]);
} else if(sortValues.length == 4) {
return new QuadValueSortDoc(sortValues[0], sortValues[1], sortValues[2], sortValues[3]);
} else {
throw new IOException("A max of 4 sorts can be specified");
}
}