本文整理汇总了Java中org.apache.lucene.search.Sort类的典型用法代码示例。如果您正苦于以下问题:Java Sort类的具体用法?Java Sort怎么用?Java Sort使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Sort类属于org.apache.lucene.search包,在下文中一共展示了Sort类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: assertAvgScoreMode
import org.apache.lucene.search.Sort; //导入依赖的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.Sort; //导入依赖的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.Sort; //导入依赖的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: getNewsOfNewsGroup
import org.apache.lucene.search.Sort; //导入依赖的package包/类
/**
* Returns all {@link News} of the given news group except the {@link News} with the given id.
* @param newsGroupId the news group id
* @param exceptId the news which should not be returned
* @return a {@link List} with all {@link News} of the requested news group except the {@link News} with the exceptId.
*/
private List<News> getNewsOfNewsGroup(final long newsGroupId, final long exceptId) {
final BooleanQuery query = new BooleanQuery();
QueryUtil.addTypeConf(query, NewsIndexType.getInstance());
final NumericRangeQuery<Long> groupQuery = NumericRangeQuery.newLongRange(
NewsIndexType.FIELD_NEWSGROUPID, newsGroupId, newsGroupId, true, true);
query.add(groupQuery, Occur.MUST);
// exclude news
query.add(new TermQuery(new Term(IIndexElement.FIELD_ID, String.valueOf(exceptId))), Occur.MUST_NOT);
final SearchOptions options = new SearchOptions();
options.setSort(new Sort(ESortField.PUBLISH_DATE.getSortField(ESortOrder.DESC)));
final DocumentsSearchResult result = IndexSearch.getInstance().search(query, options);
return NewsIndexType.docsToNews(result.getResults());
}
示例5: MergeSortQueue
import org.apache.lucene.search.Sort; //导入依赖的package包/类
MergeSortQueue(Sort sort, CollapseTopFieldDocs[] shardHits) throws IOException {
super(shardHits.length);
this.shardHits = new ScoreDoc[shardHits.length][];
for (int shardIDX = 0; shardIDX < shardHits.length; shardIDX++) {
final ScoreDoc[] shard = shardHits[shardIDX].scoreDocs;
if (shard != null) {
this.shardHits[shardIDX] = shard;
// Fail gracefully if API is misused:
for (int hitIDX = 0; hitIDX < shard.length; hitIDX++) {
final ScoreDoc sd = shard[hitIDX];
final FieldDoc gd = (FieldDoc) sd;
assert gd.fields != null;
}
}
}
final SortField[] sortFields = sort.getSort();
comparators = new FieldComparator[sortFields.length];
reverseMul = new int[sortFields.length];
for (int compIDX = 0; compIDX < sortFields.length; compIDX++) {
final SortField sortField = sortFields[compIDX];
comparators[compIDX] = sortField.getComparator(1, compIDX);
reverseMul[compIDX] = sortField.getReverse() ? -1 : 1;
}
}
示例6: testMultiPhrasePrefixQuery
import org.apache.lucene.search.Sort; //导入依赖的package包/类
public void testMultiPhrasePrefixQuery() throws Exception {
Analyzer analyzer = new StandardAnalyzer();
Directory dir = newDirectory();
String value = "The quick brown fox.";
IndexReader ir = indexOneDoc(dir, "text", value, analyzer);
MultiPhrasePrefixQuery query = new MultiPhrasePrefixQuery();
query.add(new Term("text", "quick"));
query.add(new Term("text", "brown"));
query.add(new Term("text", "fo"));
IndexSearcher searcher = newSearcher(ir);
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
assertThat(topDocs.totalHits, equalTo(1));
int docId = topDocs.scoreDocs[0].doc;
CustomPassageFormatter passageFormatter = new CustomPassageFormatter("<b>", "</b>", new DefaultEncoder());
CustomUnifiedHighlighter highlighter = new CustomUnifiedHighlighter(searcher, analyzer,
passageFormatter, null, value, false);
Snippet[] snippets = highlighter.highlightField("text", query, docId, 5);
assertThat(snippets.length, equalTo(1));
assertThat(snippets[0].getText(), equalTo("The <b>quick</b> <b>brown</b> <b>fox</b>."));
ir.close();
dir.close();
}
示例7: testAllTermQuery
import org.apache.lucene.search.Sort; //导入依赖的package包/类
public void testAllTermQuery() throws IOException {
Directory dir = newDirectory();
String value = "The quick brown fox.";
Analyzer analyzer = new StandardAnalyzer();
IndexReader ir = indexOneDoc(dir, "all", value, analyzer);
AllTermQuery query = new AllTermQuery(new Term("all", "fox"));
IndexSearcher searcher = newSearcher(ir);
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
assertThat(topDocs.totalHits, equalTo(1));
int docId = topDocs.scoreDocs[0].doc;
CustomPassageFormatter passageFormatter = new CustomPassageFormatter("<b>", "</b>", new DefaultEncoder());
CustomUnifiedHighlighter highlighter = new CustomUnifiedHighlighter(searcher, analyzer,
passageFormatter, null, value, false);
Snippet[] snippets = highlighter.highlightField("all", query, docId, 5);
assertThat(snippets.length, equalTo(1));
assertThat(snippets[0].getText(), equalTo("The quick brown <b>fox</b>."));
ir.close();
dir.close();
}
示例8: testCommonTermsQuery
import org.apache.lucene.search.Sort; //导入依赖的package包/类
public void testCommonTermsQuery() throws IOException {
Directory dir = newDirectory();
String value = "The quick brown fox.";
Analyzer analyzer = new StandardAnalyzer();
IndexReader ir = indexOneDoc(dir, "text", value, analyzer);
CommonTermsQuery query = new CommonTermsQuery(BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, 128);
query.add(new Term("text", "quick"));
query.add(new Term("text", "brown"));
query.add(new Term("text", "fox"));
IndexSearcher searcher = newSearcher(ir);
TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
assertThat(topDocs.totalHits, equalTo(1));
int docId = topDocs.scoreDocs[0].doc;
CustomPassageFormatter passageFormatter = new CustomPassageFormatter("<b>", "</b>", new DefaultEncoder());
CustomUnifiedHighlighter highlighter = new CustomUnifiedHighlighter(searcher, analyzer,
passageFormatter, null, value, false);
Snippet[] snippets = highlighter.highlightField("text", query, docId, 5);
assertThat(snippets.length, equalTo(1));
assertThat(snippets[0].getText(), equalTo("The <b>quick</b> <b>brown</b> <b>fox</b>."));
ir.close();
dir.close();
}
示例9: testNoScoring
import org.apache.lucene.search.Sort; //导入依赖的package包/类
public void testNoScoring() throws IOException {
QueryProfiler profiler = new QueryProfiler();
searcher.setProfiler(profiler);
Query query = new TermQuery(new Term("foo", "bar"));
searcher.search(query, 1, Sort.INDEXORDER); // scores are not needed
List<ProfileResult> results = profiler.getTree();
assertEquals(1, results.size());
Map<String, Long> breakdown = results.get(0).getTimeBreakdown();
assertThat(breakdown.get(QueryTimingType.CREATE_WEIGHT.toString()).longValue(), greaterThan(0L));
assertThat(breakdown.get(QueryTimingType.BUILD_SCORER.toString()).longValue(), greaterThan(0L));
assertThat(breakdown.get(QueryTimingType.NEXT_DOC.toString()).longValue(), greaterThan(0L));
assertThat(breakdown.get(QueryTimingType.ADVANCE.toString()).longValue(), equalTo(0L));
assertThat(breakdown.get(QueryTimingType.SCORE.toString()).longValue(), equalTo(0L));
assertThat(breakdown.get(QueryTimingType.MATCH.toString()).longValue(), equalTo(0L));
assertThat(breakdown.get(QueryTimingType.CREATE_WEIGHT.toString() + "_count").longValue(), greaterThan(0L));
assertThat(breakdown.get(QueryTimingType.BUILD_SCORER.toString() + "_count").longValue(), greaterThan(0L));
assertThat(breakdown.get(QueryTimingType.NEXT_DOC.toString() + "_count").longValue(), greaterThan(0L));
assertThat(breakdown.get(QueryTimingType.ADVANCE.toString() + "_count").longValue(), equalTo(0L));
assertThat(breakdown.get(QueryTimingType.SCORE.toString() + "_count").longValue(), equalTo(0L));
assertThat(breakdown.get(QueryTimingType.MATCH.toString() + "_count").longValue(), equalTo(0L));
long rewriteTime = profiler.getRewriteTime();
assertThat(rewriteTime, greaterThan(0L));
}
示例10: testSortValues
import org.apache.lucene.search.Sort; //导入依赖的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)}))));
}
}
示例11: convert
import org.apache.lucene.search.Sort; //导入依赖的package包/类
@Override
public Sort convert(org.springframework.data.domain.Sort sort) {
SortContext context = queryBuilder.sort();
SortFieldContext currentContext = null;
for (org.springframework.data.domain.Sort.Order order : sort) {
String fieldName = entityMetadataContext.getFieldName(order.getProperty());
if (currentContext == null) {
currentContext = context.byField(fieldName);
} else {
currentContext = currentContext.andByField(fieldName);
}
org.springframework.data.domain.Sort.NullHandling nullHandling = order.getNullHandling();
if (nullHandling == org.springframework.data.domain.Sort.NullHandling.NULLS_FIRST) {
currentContext = currentContext.onMissingValue().sortFirst();
} else if (nullHandling == org.springframework.data.domain.Sort.NullHandling.NULLS_LAST) {
currentContext = currentContext.onMissingValue().sortLast();
}
boolean desc = (order.getDirection() == org.springframework.data.domain.Sort.Direction.DESC);
if (desc) {
currentContext = currentContext.desc();
} else {
currentContext = currentContext.asc();
}
}
return (currentContext != null) ? currentContext.createSort() : null;
}
示例12: generateLuceneSort
import org.apache.lucene.search.Sort; //导入依赖的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);
}
示例13: CoreSearchIterable
import org.apache.lucene.search.Sort; //导入依赖的package包/类
public CoreSearchIterable(
CoreKVStore<K, V> store,
LuceneSearchIndex index,
Query searchQuery,
Sort sort,
int pageSize,
int offset,
int limit
) {
this.store = store;
this.index = index;
this.searchQuery = searchQuery;
this.sort = sort;
this.pageSize = pageSize;
this.offset = offset;
this.limit = limit;
}
示例14: run
import org.apache.lucene.search.Sort; //导入依赖的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;
}
}
}
示例15: prefixSearch
import org.apache.lucene.search.Sort; //导入依赖的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;
}