當前位置: 首頁>>代碼示例>>Java>>正文


Java Sort類代碼示例

本文整理匯總了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));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:22,代碼來源:DoubleNestedSortingTests.java

示例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));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:21,代碼來源:FloatNestedSortingTests.java

示例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));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:21,代碼來源:AbstractNumberNestedSortingTestCase.java

示例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());
}
 
開發者ID:XMBomb,項目名稱:InComb,代碼行數:25,代碼來源:NewsDao.java

示例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;
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:26,代碼來源:CollapseTopFieldDocs.java

示例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();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:23,代碼來源:CustomUnifiedHighlighterTests.java

示例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();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:20,代碼來源:CustomUnifiedHighlighterTests.java

示例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();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:23,代碼來源:CustomUnifiedHighlighterTests.java

示例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));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:26,代碼來源:QueryProfilerTests.java

示例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)}))));
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:19,代碼來源:SimpleLuceneTests.java

示例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;
}
 
開發者ID:snowdrop,項目名稱:spring-data-snowdrop,代碼行數:27,代碼來源:LuceneQueryHelper.java

示例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);
}
 
開發者ID:baidu,項目名稱:Elasticsearch,代碼行數:17,代碼來源:LuceneSortGenerator.java

示例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;
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:18,代碼來源:CoreSearchIterable.java

示例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;
    }
  }
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:27,代碼來源:TestLuceneIndexer.java

示例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;
}
 
開發者ID:zhaoxi1988,項目名稱:sjk,代碼行數:22,代碼來源:QuickTipsServiceImpl.java


注:本文中的org.apache.lucene.search.Sort類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。