org.apache.lucene.search.Sort类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(15.5k)|赞(0)|评价(0)|浏览(96)

本文整理了Java中org.apache.lucene.search.Sort类的一些代码示例,展示了Sort类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Sort类的具体详情如下:
包路径:org.apache.lucene.search.Sort
类名称:Sort

Sort介绍

[英]Encapsulates sort criteria for returned hits.

The fields used to determine sort order must be carefully chosen. Documents must contain a single term in such a field, and the value of the term should indicate the document's relative position in a given sort order. The field must be indexed, but should not be tokenized, and does not need to be stored (unless you happen to want it back with the rest of your document data). In other words:

document.add (new Field ("byNumber", Integer.toString(x), Field.Store.NO, Field.Index.NOT_ANALYZED));

Valid Types of Values

There are four possible kinds of term values which may be put into sorting fields: Integers, Longs, Floats, or Strings. Unless SortField objects are specified, the type of value in the field is determined by parsing the first term in the field.

Integer term values should contain only digits and an optional preceding negative sign. Values must be base 10 and in the range Integer.MIN_VALUE and Integer.MAX_VALUE inclusive. Documents which should appear first in the sort should have low value integers, later documents high values (i.e. the documents should be numbered 1..n where 1 is the first and n the last).

Long term values should contain only digits and an optional preceding negative sign. Values must be base 10 and in the range Long.MIN_VALUE and Long.MAX_VALUE inclusive. Documents which should appear first in the sort should have low value integers, later documents high values.

Float term values should conform to values accepted by Float (except that NaN and Infinity are not supported). Documents which should appear first in the sort should have low values, later documents high values.

String term values can contain any valid String, but should not be tokenized. The values are sorted according to their Comparable. Note that using this type of term value has higher memory requirements than the other two types.

Object Reuse

One of these objects can be used multiple times and the sort order changed between usages.

This class is thread safe.

Memory Usage

Sorting uses of caches of term values maintained by the internal HitQueue(s). The cache is static and contains an integer or float array of length IndexReader.maxDoc() for each field name for which a sort is performed. In other words, the size of the cache in bytes is:

4 * IndexReader.maxDoc() * (# of different fields actually used to sort)

For String fields, the cache is larger: in addition to the above array, the value of every term in the field is kept in memory. If there are many unique terms in the field, this could be quite large.

Note that the size of the cache is not affected by how many fields are in the index and might be used to sort - only by the ones actually used to sort a result set.

Created: Feb 12, 2004 10:53:57 AM
[中]封装返回点击的排序标准。
必须仔细选择用于确定排序顺序的字段。文档必须在这样的字段中包含一个术语,该术语的值应指示文档在给定排序顺序中的相对位置。该字段必须编制索引,但不应标记化,也不需要存储(除非您碰巧想将其与文档的其余数据一起返回)。换句话说:
document.add (new Field ("byNumber", Integer.toString(x), Field.Store.NO, Field.Index.NOT_ANALYZED));
####有效的值类型
有四种可能的术语值可以放入排序字段:整数、长、浮点数或字符串。除非指定了SortField对象,否则字段中的值类型是通过解析字段中的第一个项来确定的。
整型项值应仅包含数字和可选的前置负号。值必须以10为基数,并且在Integer.MIN_VALUEInteger.MAX_VALUE范围内。排序中应首先出现的文档应具有低值整数,之后的文档应具有高值(即,文档应编号为1..n,其中1为第一个,n为最后一个)。
长期值应仅包含数字和可选的前置负号。值必须以10为基数,并且在Long.MIN_VALUELong.MAX_VALUE范围内。排序中首先出现的文档应具有低值整数,随后出现的文档应具有高值整数。
浮动期限值应符合浮动接受的值(不支持NaNInfinity)。排序中首先出现的文档应具有较低的值,之后出现的文档应具有较高的值。
字符串术语值可以包含任何有效字符串,但不应标记化。这些值根据其可比性进行排序。请注意,与其他两种类型相比,使用这种类型的术语值具有更高的内存需求。
####对象重用
其中一个对象可以多次使用,并且在不同的使用中排序顺序会发生变化。
这个类是线程安全的。
####内存使用
对内部HitQueue维护的术语值缓存进行排序。缓存是静态的,对于每个执行排序的字段名,它都包含一个长度为IndexReader.maxDoc()的整数或浮点数组。换句话说,缓存的大小(以字节为单位):
4 * IndexReader.maxDoc() * (# of different fields actually used to sort)
对于字符串字段,缓存更大:除了上面的数组,字段中每个项的值都保存在内存中。如果在这个领域有很多独特的术语,这可能是相当大的。
请注意,缓存的大小不受索引中有多少字段的影响,并且可能用于排序——仅受实际用于对结果集排序的字段的影响。
创建时间:2004年2月12日上午10:53:57

代码示例

代码示例来源:origin: oracle/opengrok

SortField sfield = new SortField(QueryBuilder.DATE, SortField.Type.STRING, true);
Sort sort = new Sort(sfield);
QueryParser qparser = new QueryParser(QueryBuilder.PATH, new CompatibleAnalyser());
Query query;
ScoreDoc[] hits = null;
try {
  query = qparser.parse(path);

代码示例来源:origin: org.apache.lucene/lucene-core

public MergeSortQueue(Sort sort, TopDocs[] shardHits) {
 super(shardHits.length);
 this.shardHits = new ScoreDoc[shardHits.length][];
 for(int shardIDX=0;shardIDX<shardHits.length;shardIDX++) {
  final ScoreDoc[] shard = shardHits[shardIDX].scoreDocs;
  //System.out.println("  init shardIdx=" + shardIDX + " hits=" + shard);
  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];
    if (!(sd instanceof FieldDoc)) {
     throw new IllegalArgumentException("shard " + shardIDX + " was not sorted by the provided Sort (expected FieldDoc but got ScoreDoc)");
    }
    final FieldDoc fd = (FieldDoc) sd;
    if (fd.fields == null) {
     throw new IllegalArgumentException("shard " + shardIDX + " did not set sort field values (FieldDoc.fields is null); you must pass fillFields=true to IndexSearcher.search on each shard");
    }
   }
  }
 }
 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;
 }
}

代码示例来源:origin: querydsl/querydsl

public Sort toSort(List<? extends OrderSpecifier<?>> orderBys) {
    List<SortField> sorts = new ArrayList<SortField>(orderBys.size());
    for (OrderSpecifier<?> order : orderBys) {
      if (!(order.getTarget() instanceof Path<?>)) {
        throw new IllegalArgumentException("argument was not of type Path.");
      }
      Class<?> type = order.getTarget().getType();
      boolean reverse = !order.isAscending();
      Path<?> path = getPath(order.getTarget());
      if (Number.class.isAssignableFrom(type)) {
        sorts.add(new SortField(toField(path), sortFields.get(type), reverse));
      } else {
        sorts.add(new SortField(toField(path), sortLocale, reverse));
      }
    }
    Sort sort = new Sort();
    sort.setSort(sorts.toArray(new SortField[sorts.size()]));
    return sort;
  }
}

代码示例来源:origin: org.apache.lucene/lucene-core

@Override
 public Explanation explain(IndexSearcher searcher, Explanation firstPassExplanation, int docID) throws IOException {
  TopDocs oneHit = new TopDocs(1, new ScoreDoc[] {new ScoreDoc(docID, firstPassExplanation.getValue())});
  TopDocs hits = rescore(searcher, oneHit, 1);
  assert hits.totalHits == 1;

  List<Explanation> subs = new ArrayList<>();

  // Add first pass:
  Explanation first = Explanation.match(firstPassExplanation.getValue(), "first pass score", firstPassExplanation);
  subs.add(first);

  FieldDoc fieldDoc = (FieldDoc) hits.scoreDocs[0];

  // Add sort values:
  SortField[] sortFields = sort.getSort();
  for(int i=0;i<sortFields.length;i++) {
   subs.add(Explanation.match(0.0f, "sort field " + sortFields[i].toString() + " value=" + fieldDoc.fields[i]));
  }

  // TODO: if we could ask the Sort to explain itself then
  // we wouldn't need the separate ExpressionRescorer...
  return Explanation.match(0.0f, "sort field values for sort=" + sort.toString(), subs);
 }
}

代码示例来源:origin: hibernate/hibernate-search

@SuppressWarnings("unchecked")
@Test
public void testResultOrderedByIdAsString() throws Exception {
  Transaction tx = fullTextSession.beginTransaction();
  Query query = queryParser.parse( "summary:lucene" );
  FullTextQuery hibQuery = fullTextSession.createFullTextQuery( query, Book.class );
  Sort sort = new Sort( new SortField( "id", SortField.Type.STRING, false ) );
  hibQuery.setSort( sort );
  List<Book> result = hibQuery.list();
  assertNotNull( result );
  assertThat( result ).extracting( "id" ).containsExactly( 1, 10, 2, 3 );
  tx.commit();
}

代码示例来源:origin: hibernate/hibernate-search

@Test
public void testResultTransformToDelimString() throws Exception {
  FullTextSession s = Search.getFullTextSession( openSession() );
  prepEmployeeIndex( s );
  Transaction tx;
  s.clear();
  tx = s.beginTransaction();
  QueryParser parser = new QueryParser( "dept", TestConstants.standardAnalyzer );
  Query query = parser.parse( "dept:ITech" );
  org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class );
  hibQuery.setProjection( "id", "lastname", "dept", FullTextQuery.THIS, FullTextQuery.SCORE, FullTextQuery.ID );
  hibQuery.setResultTransformer( new ProjectionToDelimStringResultTransformer() );
  hibQuery.setSort( new Sort( new SortField( "id", SortField.Type.STRING ), SortField.FIELD_SCORE ) );
  @SuppressWarnings("unchecked")
  List<String> result = hibQuery.list();
  assertTrue( "incorrect transformation", result.get( 0 ).startsWith( "1000, Griffin, ITech" ) );
  assertTrue( "incorrect transformation", result.get( 1 ).startsWith( "1002, Jimenez, ITech" ) );
  //cleanup
  for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) {
    s.delete( element );
  }
  tx.commit();
  s.close();
}

代码示例来源:origin: hibernate/hibernate-search

@Test
public void testFetchSizeLargerThanHits() throws Exception {
  FullTextSession fullTextSession = Search.getFullTextSession( openSession() );
  Transaction tx = fullTextSession.beginTransaction();
  QueryParser parser = new QueryParser( "dept", TestConstants.standardAnalyzer );
  Query query = parser.parse( "dept:ITech" );
  org.hibernate.search.FullTextQuery hibQuery = fullTextSession.createFullTextQuery( query, Employee.class );
  hibQuery.setSort( new Sort( new SortField( "id", SortField.Type.STRING ) ) );
  hibQuery.setProjection( "id", "lastname", "dept" );
  hibQuery.setFetchSize( 6 );
  ScrollableResults results = hibQuery.scroll();
  results.beforeFirst();
  results.next();
  Object[] result = results.get();
  assertEquals( "incorrect entityInfo returned", 1000, result[0] );
  tx.commit();
  fullTextSession.close();
}

代码示例来源:origin: hibernate/hibernate-search

@Test
public void testSortableFieldConfiguredThroughCustomFieldLevelBridge() throws Exception {
  FullTextSession fullTextSession = Search.getFullTextSession( openSession() );
  Transaction tx = fullTextSession.beginTransaction();
  @SuppressWarnings("unchecked")
  List<Book> result = fullTextSession.createFullTextQuery( new MatchAllDocsQuery(), Explorer.class )
    .setSort( new Sort( new SortField( "nameParts_lastName", SortField.Type.STRING ) ) )
    .list();
  assertNotNull( result );
  assertThat( result ).extracting( "id" ).containsExactly( 3, 1, 2 );
  tx.commit();
  fullTextSession.close();
}

代码示例来源:origin: hibernate/hibernate-search

private List<?> getResultsFiltered(FullTextSession session, Query query, Class<? extends BaseEntity>... classes) {
  return session.createFullTextQuery( query, classes )
      .setSort( new Sort( new SortField( "idSort", SortField.Type.INT ) ) )
      .list();
}

代码示例来源:origin: hibernate/hibernate-search

@Test
public void testEntityCanSortOnId() {
  try ( Session session = openSession() ) {
    FullTextSession fullTextSession = Search.getFullTextSession( session );
    Transaction transaction = fullTextSession.beginTransaction();
    Sort sort = new Sort( new SortField( "id", SortField.Type.STRING ) );
    QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( Villain.class ).get();
    Query q = queryBuilder.keyword().onField( "name" ).matching( LEX ).createQuery();
    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( q, Villain.class );
    fullTextQuery.setSort( sort );
    List list = fullTextQuery.list();
    assertThat( list ).hasSize( 1 );
    Villain actual = (Villain) list.get( 0 );
    assertThat( actual.getName() ).isEqualTo( LEX );
    transaction.commit();
  }
}

代码示例来源:origin: hibernate/hibernate-search

@Test
public void testFirstResultAndMaxResults() throws Exception {
  Session s = openSession();
  FullTextSession session = Search.getFullTextSession( s );
  Transaction tx = s.beginTransaction();
  QueryDescriptor query = ElasticsearchQueries.fromJson( "{ 'query': { 'match' : { 'abstract' : 'Hibernate' } } }" );
  List<?> result = session.createFullTextQuery( query, ScientificArticle.class )
      .setFirstResult( 1 )
      .setMaxResults( 2 )
      .setSort( new Sort( new SortField( "id", SortField.Type.STRING, false ) ) )
      .list();
  assertThat( result ).extracting( "title" ).containsExactlyInAnyOrder( "Latest in ORM", "High-performance ORM" );
  tx.commit();
  s.close();
}

代码示例来源:origin: hibernate/hibernate-search

@Override
protected void execute(FullTextSession fts) {
  Query q = fts.getSearchFactory()
      .buildQueryBuilder()
      .forEntity( Book.class )
      .get()
      .all()
      .createQuery();
  fts.createFullTextQuery( q, Book.class )
      .setSort( new Sort( new SortField( "rating", SortField.Type.FLOAT, true ) ) )
      .setMaxResults( 100 )
      .list();
}

代码示例来源:origin: soabase/exhibitor

public TopDocs   search(Query query, int maxResults) throws IOException
{
  Sort sort = new Sort(new SortField(FieldNames.DATE, SortField.LONG, true));
  return searcher.search(query, maxResults, sort);
}

代码示例来源:origin: org.apache.lucene/lucene-core

sortFields[i] = new SortedNumericSortField(fieldName, sortType, reverse, sortedNumericSelector);
  } else {
   sortFields[i] = new SortField(fieldName, sortType, reverse);
   sortFields[i].setMissingValue(missingValue);
 indexSort = new Sort(sortFields);
} else if (numSortFields < 0) {
 throw new CorruptIndexException("invalid index sort field count: " + numSortFields, input);

代码示例来源:origin: neo4j/neo4j

/**
 * Returns a QueryContext with sorting added to it.
 *
 * @param key The key to sort on.
 * @param additionalKeys Any additional keys to sort on.
 * @return A QueryContext with sorting added to it.
 */
@Deprecated
public QueryContext sort( String key, String... additionalKeys )
{
  SortField firstSortField = new SortedSetSortField( key, false );
  if ( additionalKeys.length == 0 )
  {
    return sort( new Sort( firstSortField ) );
  }
  SortField[] sortFields = new SortField[1 + additionalKeys.length];
  sortFields[0] = firstSortField;
  for ( int i = 0; i < additionalKeys.length; i++ )
  {
    sortFields[1 + i] = new SortedSetSortField( additionalKeys[i], false );
  }
  return sort( new Sort( sortFields ) );
}

代码示例来源:origin: org.apache.lucene/lucene-core

int numSortFields = indexSort == null ? 0 : indexSort.getSort().length;
output.writeVInt(numSortFields);
for (int i = 0; i < numSortFields; ++i) {
 SortField sortField = indexSort.getSort()[i];
 SortField.Type sortType = sortField.getType();
 output.writeString(sortField.getField());
 int sortTypeID;
 switch (sortField.getType()) {
  case STRING:
   sortTypeID = 0;

代码示例来源:origin: org.infinispan/infinispan-query

@Override
public Sort readObject(final ObjectInput input) throws IOException, ClassNotFoundException {
 final int count = UnsignedNumeric.readUnsignedInt(input);
 SortField[] sortfields = new SortField[count];
 for (int i = 0; i < count; i++) {
   sortfields[i] = LuceneSortFieldExternalizer.readObjectStatic(input);
 }
 Sort sort = new Sort();
 sort.setSort(sortfields);
 return sort;
}

代码示例来源:origin: org.apache.lucene/lucene-core

/**
 * Rewrites the SortFields in this Sort, returning a new Sort if any of the fields
 * changes during their rewriting.
 *
 * @param searcher IndexSearcher to use in the rewriting
 * @return {@code this} if the Sort/Fields have not changed, or a new Sort if there
 *        is a change
 * @throws IOException Can be thrown by the rewriting
 * @lucene.experimental
 */
public Sort rewrite(IndexSearcher searcher) throws IOException {
 boolean changed = false;
 
 SortField[] rewrittenSortFields = new SortField[fields.length];
 for (int i = 0; i < fields.length; i++) {
  rewrittenSortFields[i] = fields[i].rewrite(searcher);
  if (fields[i] != rewrittenSortFields[i]) {
   changed = true;
  }
 }
 return (changed) ? new Sort(rewrittenSortFields) : this;
}

代码示例来源:origin: org.apache.lucene/lucene-core

private void validateIndexSortDVType(Sort indexSort, String fieldName, DocValuesType dvType) {
 for (SortField sortField : indexSort.getSort()) {
  if (sortField.getField().equals(fieldName)) {
   switch (dvType) {
    case NUMERIC:
     if (sortField.getType().equals(SortField.Type.INT) == false &&
        sortField.getType().equals(SortField.Type.LONG) == false &&
        sortField.getType().equals(SortField.Type.FLOAT) == false &&
        sortField.getType().equals(SortField.Type.DOUBLE) == false) {

代码示例来源:origin: org.apache.lucene/lucene-core

static boolean canEarlyTerminate(Sort searchSort, Sort indexSort) {
 final SortField[] fields1 = searchSort.getSort();
 final SortField[] fields2 = indexSort.getSort();
 // early termination is possible if fields1 is a prefix of fields2
 if (fields1.length > fields2.length) {
  return false;
 }
 return Arrays.asList(fields1).equals(Arrays.asList(fields2).subList(0, fields1.length));
}

相关文章