本文整理汇总了Java中org.apache.lucene.search.SortField类的典型用法代码示例。如果您正苦于以下问题:Java SortField类的具体用法?Java SortField怎么用?Java SortField使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SortField类属于org.apache.lucene.search包,在下文中一共展示了SortField类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: assertAvgScoreMode
import org.apache.lucene.search.SortField; //导入依赖的package包/类
@Override
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher) throws IOException {
MultiValueMode sortMode = MultiValueMode.AVG;
Query childFilter = Queries.not(parentFilter);
XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
TopDocs topDocs = searcher.search(query, 5, sort);
assertThat(topDocs.totalHits, equalTo(7));
assertThat(topDocs.scoreDocs.length, equalTo(5));
assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2));
assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
}
示例2: assertAvgScoreMode
import org.apache.lucene.search.SortField; //导入依赖的package包/类
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher, IndexFieldData.XFieldComparatorSource innerFieldComparator) throws IOException {
MultiValueMode sortMode = MultiValueMode.AVG;
Query childFilter = Queries.not(parentFilter);
XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
TopDocs topDocs = searcher.search(query, 5, sort);
assertThat(topDocs.totalHits, equalTo(7));
assertThat(topDocs.scoreDocs.length, equalTo(5));
assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2));
assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
}
示例3: assertAvgScoreMode
import org.apache.lucene.search.SortField; //导入依赖的package包/类
protected void assertAvgScoreMode(Query parentFilter, IndexSearcher searcher) throws IOException {
MultiValueMode sortMode = MultiValueMode.AVG;
Query childFilter = Queries.not(parentFilter);
XFieldComparatorSource nestedComparatorSource = createFieldComparator("field2", sortMode, -127, createNested(searcher, parentFilter, childFilter));
Query query = new ToParentBlockJoinQuery(new ConstantScoreQuery(childFilter), new QueryBitSetProducer(parentFilter), ScoreMode.None);
Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
TopDocs topDocs = searcher.search(query, 5, sort);
assertThat(topDocs.totalHits, equalTo(7));
assertThat(topDocs.scoreDocs.length, equalTo(5));
assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
assertThat(topDocs.scoreDocs[1].doc, equalTo(3));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[2].doc, equalTo(7));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(4));
}
示例4: randomOfType
import org.apache.lucene.search.SortField; //导入依赖的package包/类
private Object randomOfType(SortField.Type type) {
switch (type) {
case CUSTOM:
throw new UnsupportedOperationException();
case DOC:
return between(0, IndexWriter.MAX_DOCS);
case DOUBLE:
return randomDouble();
case FLOAT:
return randomFloat();
case INT:
return randomInt();
case LONG:
return randomLong();
case REWRITEABLE:
throw new UnsupportedOperationException();
case SCORE:
return randomFloat();
case STRING:
return new BytesRef(randomAsciiOfLength(5));
case STRING_VAL:
return new BytesRef(randomAsciiOfLength(5));
default:
throw new UnsupportedOperationException("Unkown SortField.Type: " + type);
}
}
示例5: testSortValues
import org.apache.lucene.search.SortField; //导入依赖的package包/类
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)}))));
}
}
示例6: missingValue
import org.apache.lucene.search.SortField; //导入依赖的package包/类
/** Calculates the missing Values as in {@link org.elasticsearch.index.fielddata.IndexFieldData}
* The results in the {@link org.apache.lucene.search.ScoreDoc} contains this missingValues instead of nulls. Because we
* need nulls in the result, it's necessary to check if a value is a missingValue.
*/
public static Object missingValue(boolean reverseFlag, Boolean nullFirst, SortField.Type type) {
boolean min = reverseFlag ^ (nullFirst != null ? nullFirst : reverseFlag);
switch (type) {
case INT:
case LONG:
return min ? Long.MIN_VALUE : Long.MAX_VALUE;
case FLOAT:
return min ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
case DOUBLE:
return min ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
case STRING:
case STRING_VAL:
return min ? null : MAX_TERM;
default:
throw new UnsupportedOperationException("Unsupported reduced type: " + type);
}
}
示例7: generateLuceneSort
import org.apache.lucene.search.SortField; //导入依赖的package包/类
@Nullable
public static Sort generateLuceneSort(CollectorContext context,
OrderBy orderBy,
CollectInputSymbolVisitor<?> inputSymbolVisitor) {
if (orderBy.orderBySymbols().isEmpty()) {
return null;
}
SortSymbolVisitor sortSymbolVisitor = new SortSymbolVisitor(inputSymbolVisitor);
SortField[] sortFields = sortSymbolVisitor.generateSortFields(
orderBy.orderBySymbols(),
context,
orderBy.reverseFlags(),
orderBy.nullsFirst()
);
return new Sort(sortFields);
}
示例8: run
import org.apache.lucene.search.SortField; //导入依赖的package包/类
@Override
public void run() {
int i = 0;
while (i < 10000) {
try {
if (data.size() <= i) {
sleep(1);
continue;
}
final String key = "key" + i;
final String val = "value" + i;
final List<Document> documents = index.searchForDocuments(new TermQuery(new Term(key, val)), 10, new Sort(new SortField(key, SortField.Type.STRING)));
if (documents.size() != 1) {
throw new RuntimeException("Invalid number of matching documents for " + key + ", found " + documents);
}
++i;
} catch (IOException ioe) {
error = ioe;
break;
} catch (InterruptedException e) {
} catch (AlreadyClosedException ace) {
error = ace;
break;
}
}
}
示例9: prefixSearch
import org.apache.lucene.search.SortField; //导入依赖的package包/类
@Override
public ScoreDoc[] prefixSearch(String keywords) throws IOException {
if (StringUtils.isEmpty(keywords) || keywords.length() > appConfig.getKeywordMaxLength()) {
logger.error("empty keywords or over-length! {}", keywords);
return null;
}
Sort sort = new Sort(new SortField("downloadRank", SortField.INT, true));
Term nameFldTerm = new Term(fieldName, keywords);
PrefixQuery nameFldQuery = new PrefixQuery(nameFldTerm);
NumericRangeQuery<Integer> catalogQuery = NumericRangeQuery.newIntRange("catalog",
(int) EnumCatalog.SOFT.getCatalog(), (int) EnumCatalog.GAME.getCatalog(), true, true);
BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(catalogQuery, Occur.MUST);
booleanQuery.add(nameFldQuery, Occur.MUST);
TopDocs topDocs = quickTipsSearcher.search(booleanQuery, appConfig.getQuickTipsNum() * 2, sort);
ScoreDoc[] docs = topDocs.scoreDocs;
return docs;
}
示例10: getInstance
import org.apache.lucene.search.SortField; //导入依赖的package包/类
@Override
public MergePolicy getInstance(Map<String, String> params) throws IOException {
String field = params.get(SORT_FIELD);
SortField.Type sortFieldType = SortField.Type.DOC;
if (params.containsKey(SORT_FIELD_TYPE)) {
sortFieldType = SortField.Type.valueOf(params.get(SORT_FIELD_TYPE).toUpperCase());
}
if (sortFieldType == SortField.Type.DOC) {
throw new IOException(
"Relying on internal lucene DocIDs is not guaranteed to work, this is only an implementation detail.");
}
boolean desc = true;
if (params.containsKey(SORT_DESC)) {
try {
desc = Boolean.valueOf(params.get(SORT_DESC));
} catch (Exception e) {
desc = true;
}
}
SortField sortField = new SortField(field, sortFieldType, desc);
Sort sort = new Sort(sortField);
return new SortingMergePolicyDecorator(new TieredMergePolicy(), sort);
}
示例11: sortField
import org.apache.lucene.search.SortField; //导入依赖的package包/类
/**
* Returns a Lucene {@link SortField} for sorting documents/rows according
* to the partition key.
*
* @return a sort field for sorting by partition key
*/
public SortField sortField() {
return new SortField(FIELD_NAME, new FieldComparatorSource() {
@Override
public FieldComparator<?> newComparator(String field, int hits, int sort, boolean reversed)
throws IOException {
return new FieldComparator.TermValComparator(hits, field, false) {
@Override
public int compareValues(BytesRef val1, BytesRef val2) {
ByteBuffer bb1 = ByteBufferUtils.byteBuffer(val1);
ByteBuffer bb2 = ByteBufferUtils.byteBuffer(val2);
return ByteBufferUtil.compareUnsigned(bb1, bb2);
}
};
}
});
}
示例12: searchByTag
import org.apache.lucene.search.SortField; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public List<PostPO> searchByTag(Paging paigng, String tag) {
FullTextSession fullTextSession = Search.getFullTextSession(super.session());
SearchFactory sf = fullTextSession.getSearchFactory();
QueryBuilder qb = sf.buildQueryBuilder().forEntity(PostPO.class).get();
org.apache.lucene.search.Query luceneQuery = qb.phrase().onField("tags").sentence(tag).createQuery();
FullTextQuery query = fullTextSession.createFullTextQuery(luceneQuery);
query.setFirstResult(paigng.getFirstResult());
query.setMaxResults(paigng.getMaxResults());
Sort sort = new Sort(new SortField("id", SortField.Type.LONG, true));
query.setSort(sort);
paigng.setTotalCount(query.getResultSize());
return query.list();
}
示例13: getGroupByField
import org.apache.lucene.search.SortField; //导入依赖的package包/类
private String getGroupByField(List<VcfFile> files, String groupBy) throws IOException {
IndexSortField sortField = IndexSortField.getByName(groupBy);
if (sortField == null) {
VcfFilterInfo info = vcfManager.getFiltersInfo(
files.stream().map(BaseEntity::getId).collect(Collectors.toList()));
InfoItem infoItem = info.getInfoItemMap().get(groupBy);
Assert.notNull(infoItem, "Unknown sort field: " + groupBy);
if (infoItem.getType() == VCFHeaderLineType.Integer || infoItem.getType() == VCFHeaderLineType.Float) {
return FeatureIndexFields.getGroupName(infoItem.getName().toLowerCase());
} else {
return infoItem.getName().toLowerCase();
}
} else {
if (sortField.getType() == SortField.Type.INT || sortField.getType() == SortField.Type.FLOAT) {
return sortField.getField().getGroupName();
} else {
return sortField.getField().fieldName;
}
}
}
示例14: getStringSortField
import org.apache.lucene.search.SortField; //导入依赖的package包/类
/**
* Comes from Sorting of Solr
*
* Returns a {@link SortField} for a string field.
* If nullLast and nullFirst are both false, then default lucene string sorting is used where
* null strings sort first in an ascending sort, and last in a descending sort.
*
* @param fieldName the name of the field to sort on
* @param reverse true for a reverse (desc) sort
* @param nullLast true if null should come last, regardless of sort order
* @param nullFirst true if null should come first, regardless of sort order
* @return SortField
*/
public static SortField getStringSortField(String fieldName, boolean reverse, boolean nullLast, boolean nullFirst) {
if (nullFirst && nullLast) {
throw new IllegalArgumentException("Cannot specify missing values as both first and last");
}
SortField sortField = new SortField(fieldName, SortField.Type.STRING, reverse);
// 4 cases:
// missingFirst / forward: default lucene behavior
// missingFirst / reverse: set sortMissingLast
// missingLast / forward: set sortMissingLast
// missingLast / reverse: default lucene behavior
if (nullFirst && reverse) {
sortField.setMissingValue(SortField.STRING_LAST);
} else if (nullLast && !reverse) {
sortField.setMissingValue(SortField.STRING_LAST);
}
return sortField;
}
示例15: getLongSortField
import org.apache.lucene.search.SortField; //导入依赖的package包/类
public static SortField getLongSortField(String fieldName, boolean reverse, boolean nullLast, boolean nullFirst) {
if (nullFirst && nullLast) {
throw new IllegalArgumentException("Cannot specify missing values as both first and last");
}
SortField sortField = new SortField(fieldName, SortField.Type.LONG, reverse);
// 4 cases:
// missingFirst / forward: default lucene behavior
// missingFirst / reverse: set sortMissingLast
// missingLast / forward: set sortMissingLast
// missingLast / reverse: default lucene behavior
if (nullFirst && reverse) {
sortField.setMissingValue(Long.MAX_VALUE);
} else if (nullLast && !reverse) {
sortField.setMissingValue(Long.MAX_VALUE);
}
return sortField;
}