Java Code Examples for org.apache.lucene.search.SortField.Type

The following examples show how to use org.apache.lucene.search.SortField.Type. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lucene-solr   Source File: TestSort.java    License: Apache License 2.0 6 votes vote down vote up
private static SortField getStringSortField(String fieldName, boolean reverse, boolean nullLast, boolean nullFirst) {
  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;
}
 
Example 2
Source Project: lucene-solr   Source File: ChildFieldValueSourceParser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public SortField getSortField(boolean reverse) {
  final Type type = childField.getSortField(reverse).getType();
    return new ToParentBlockJoinSortField(childField.getName(), 
        type, reverse, 
        parentFilter, childFilter) {
      @SuppressWarnings("unchecked")
      @Override
      public FieldComparator<?> getComparator(int numHits, int sortPos) {
        final FieldComparator<?> comparator = super.getComparator(numHits, sortPos);
        return type ==Type.STRING ?  new BytesToStringComparator((FieldComparator<BytesRef>) comparator)
            : comparator;
      }
    };
}
 
Example 3
@Override
public SortField getSortField(boolean reverse) {
  if (reverse) {
    return new SortField(getFieldName(), Type.LONG, reverse);
  }
  return new SortField(getFieldName(), Type.LONG);
}
 
Example 4
@Override
public SortField getSortField(boolean reverse) {
  if (reverse) {
    return new SortField(getFieldName(), Type.STRING, reverse);
  }
  return new SortField(getFieldName(), Type.STRING);
}
 
Example 5
@Override
public SortField getSortField(boolean reverse) {
  if (reverse) {
    return new SortField(getFieldName(), Type.LONG, reverse);
  }
  return new SortField(getFieldName(), Type.LONG);
}
 
Example 6
@Override
public SortField getSortField(boolean reverse) {
  if (reverse) {
    return new SortField(getFieldName(), Type.DOUBLE, reverse);
  }
  return new SortField(getFieldName(), Type.DOUBLE);
}
 
Example 7
@Override
public SortField getSortField(boolean reverse) {
  if (reverse) {
    return new SortField(getFieldName(), Type.STRING, reverse);
  }
  return new SortField(getFieldName(), Type.STRING);
}
 
Example 8
@Override
public SortField getSortField(boolean reverse) {
  if (reverse) {
    return new SortField(getFieldName(), Type.INT, reverse);
  }
  return new SortField(getFieldName(), Type.INT);
}
 
Example 9
@Override
public SortField getSortField(boolean reverse) {
  if (reverse) {
    return new SortField(getFieldName(), Type.STRING, reverse);
  }
  return new SortField(getFieldName(), Type.STRING);
}
 
Example 10
@Override
public SortField getSortField(boolean reverse) {
  if (reverse) {
    return new SortField(getFieldName(), Type.FLOAT, reverse);
  }
  return new SortField(getFieldName(), Type.FLOAT);
}
 
Example 11
@Test
public void testSimpleSearchPagingWithSorting() throws Exception {
  IndexReader reader = getReaderFlatScore(length);
  IndexSearcherCloseable searcher = getSearcher(reader);

  TotalHitsRef totalHitsRef = new TotalHitsRef();
  ProgressRef progressRef = new ProgressRef();

  printHeapSize();
  TermQuery query = new TermQuery(new Term("f1", "value"));
  Sort sort = new Sort(new SortField("index", Type.INT, true));
  IterablePaging paging = new IterablePaging(new AtomicBoolean(true), searcher, query, 100, null, null, false, sort,
      new DeepPagingCache());
  IterablePaging itPaging = paging.skipTo(90).gather(20).totalHits(totalHitsRef).progress(progressRef);
  BlurIterator<ScoreDoc, BlurException> iterator = itPaging.iterator();
  int position = 90;
  int searches = 1;
  while (iterator.hasNext()) {
    ScoreDoc sd = iterator.next();
    assertEquals(position, progressRef.currentHitPosition());
    assertEquals(searches, progressRef.searchesPerformed());
    System.out.println("time [" + progressRef.queryTime() + "] " + "total hits [" + totalHitsRef.totalHits() + "] "
        + "searches [" + progressRef.searchesPerformed() + "] " + "position [" + progressRef.currentHitPosition()
        + "] " + "doc id [" + sd.doc + "] " + "score [" + sd.score + "]");
    position++;
    if (position == 100) {
      searches++;
    }
  }
  printHeapSize();
}
 
Example 12
Source Project: lucene-solr   Source File: FloatFieldSource.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SortField getSortField(boolean reverse) {
  return new SortField(field, Type.FLOAT, reverse);
}
 
Example 13
Source Project: lucene-solr   Source File: LongFieldSource.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SortField getSortField(boolean reverse) {
  return new SortField(field, Type.LONG, reverse);
}
 
Example 14
Source Project: lucene-solr   Source File: IntFieldSource.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SortField getSortField(boolean reverse) {
  return new SortField(field, Type.INT, reverse);
}
 
Example 15
Source Project: lucene-solr   Source File: DoubleFieldSource.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SortField getSortField(boolean reverse) {
  return new SortField(field, Type.DOUBLE, reverse);
}
 
Example 16
Source Project: lucene-solr   Source File: TestSort.java    License: Apache License 2.0 4 votes vote down vote up
public void testRandomFieldNameSorts() throws Exception {
  SolrQueryRequest req = lrf.makeRequest("q", "*:*");

  final int iters = atLeast(5000);

  // infinite loop abort when trying to generate a non-blank sort "name"
  final int nonBlankAttempts = 37;

  for (int i = 0; i < iters; i++) {
    final StringBuilder input = new StringBuilder();
    final String[] names = new String[TestUtil.nextInt(r, 1, 10)];
    final boolean[] reverse = new boolean[names.length];
    for (int j = 0; j < names.length; j++) {
      names[j] = null;
      for (int k = 0; k < nonBlankAttempts && null == names[j]; k++) {
        names[j] = TestUtil.randomRealisticUnicodeString(r, 1, 100);

        // munge anything that might make this a function
        names[j] = names[j].replaceFirst("\\{","\\}\\{");
        names[j] = names[j].replaceFirst("\\(","\\)\\(");
        names[j] = names[j].replaceFirst("(\\\"|\\')","$1$1z");
        names[j] = names[j].replaceFirst("(\\d)","$1x");

        // eliminate pesky problem chars
        names[j] = names[j].replaceAll("\\p{Cntrl}|\\p{javaWhitespace}","");
        
        if (0 == names[j].length()) {
          names[j] = null;
        }
      }
      // with luck this bad, never go to vegas
      // alternatively: if (null == names[j]) names[j] = "never_go_to_vegas";
      assertNotNull("Unable to generate a (non-blank) names["+j+"] after "
                    + nonBlankAttempts + " attempts", names[j]);

      reverse[j] = r.nextBoolean();

      input.append(r.nextBoolean() ? " " : "");
      input.append(names[j]);
      input.append(" ");
      input.append(reverse[j] ? "desc," : "asc,");
    }
    input.deleteCharAt(input.length()-1);
    SortField[] sorts = null;
    List<SchemaField> fields = null;
    try {
      SortSpec spec = SortSpecParsing.parseSortSpec(input.toString(), req);
      sorts = spec.getSort().getSort();
      fields = spec.getSchemaFields();
    } catch (RuntimeException e) {
      throw new RuntimeException("Failed to parse sort: " + input, e);
    }
    assertEquals("parsed sorts had unexpected size", 
                 names.length, sorts.length);
    assertEquals("parsed sort schema fields had unexpected size", 
                 names.length, fields.size());
    for (int j = 0; j < names.length; j++) {
      assertEquals("sorts["+j+"] had unexpected reverse: " + input,
                   reverse[j], sorts[j].getReverse());

      final Type type = sorts[j].getType();

      if (Type.SCORE.equals(type)) {
        assertEquals("sorts["+j+"] is (unexpectedly) type score : " + input,
                     "score", names[j]);
      } else if (Type.DOC.equals(type)) {
        assertEquals("sorts["+j+"] is (unexpectedly) type doc : " + input,
                     "_docid_", names[j]);
      } else if (Type.CUSTOM.equals(type) || Type.REWRITEABLE.equals(type)) {
        log.error("names[{}] : {}", j, names[j]);
        log.error("sorts[{}] : {}", j, sorts[j]);
        fail("sorts["+j+"] resulted in a '" + type.toString()
             + "', either sort parsing code is broken, or func/query " 
             + "semantics have gotten broader and munging in this test "
             + "needs improved: " + input);

      } else {
        assertEquals("sorts["+j+"] ("+type.toString()+
                     ") had unexpected field in: " + input,
                     names[j], sorts[j].getField());
        assertEquals("fields["+j+"] ("+type.toString()+
                     ") had unexpected name in: " + input,
                     names[j], fields.get(j).getName());
      }
    }
  }
}
 
Example 17
Source Project: stratio-cassandra   Source File: GeoShapeMapper.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.LONG, reverse);
}
 
Example 18
Source Project: stratio-cassandra   Source File: ColumnMapperBlob.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.STRING, reverse);
}
 
Example 19
Source Project: stratio-cassandra   Source File: ColumnMapperFloat.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.FLOAT, reverse);
}
 
Example 20
Source Project: stratio-cassandra   Source File: ColumnMapperBigInteger.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.STRING, reverse);
}
 
Example 21
Source Project: stratio-cassandra   Source File: ColumnMapperString.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.STRING, reverse);
}
 
Example 22
Source Project: stratio-cassandra   Source File: ColumnMapperInteger.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.INT, reverse);
}
 
Example 23
Source Project: stratio-cassandra   Source File: ColumnMapperInet.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.STRING, reverse);
}
 
Example 24
Source Project: stratio-cassandra   Source File: ColumnMapperDate.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.LONG, reverse);
}
 
Example 25
Source Project: stratio-cassandra   Source File: ColumnMapperText.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.STRING, reverse);
}
 
Example 26
Source Project: stratio-cassandra   Source File: ColumnMapperBoolean.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.STRING, reverse);
}
 
Example 27
Source Project: stratio-cassandra   Source File: ColumnMapperUUID.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.STRING, reverse);
}
 
Example 28
Source Project: stratio-cassandra   Source File: ColumnMapperDouble.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.DOUBLE, reverse);
}
 
Example 29
Source Project: stratio-cassandra   Source File: ColumnMapperLong.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.LONG, reverse);
}
 
Example 30
Source Project: stratio-cassandra   Source File: ColumnMapperBigDecimal.java    License: Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public SortField sortField(String field, boolean reverse) {
    return new SortField(field, Type.STRING, reverse);
}