org.apache.lucene.search.SortedNumericSortField Java Examples

The following examples show how to use org.apache.lucene.search.SortedNumericSortField. 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 File: SortPaneProvider.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void resetField(JComboBox<String> fieldCombo, JComboBox<String> typeCombo, JComboBox<String> orderCombo) {
  typeCombo.removeAllItems();
  if (StringUtils.isNullOrEmpty((String) fieldCombo.getSelectedItem())) {
    typeCombo.addItem("");
    typeCombo.setEnabled(false);
    orderCombo.setEnabled(false);
  } else {
    List<SortField> sortFields = searchModel.guessSortTypes((String) fieldCombo.getSelectedItem());
    sortFields.stream()
        .map(sf -> {
          if (sf instanceof SortedNumericSortField) {
            return ((SortedNumericSortField) sf).getNumericType().name();
          } else {
            return sf.getType().name();
          }
        }).forEach(typeCombo::addItem);
    typeCombo.setEnabled(true);
    orderCombo.setEnabled(true);
  }
}
 
Example #2
Source File: OrderedLuceneBatchIteratorFactoryTest.java    From crate with Apache License 2.0 6 votes vote down vote up
private LuceneOrderedDocCollector createOrderedCollector(IndexSearcher searcher, int shardId) {
    CollectorContext collectorContext = new CollectorContext();
    List<LuceneCollectorExpression<?>> expressions = Collections.singletonList(
        new OrderByCollectorExpression(reference, orderBy, o -> o));
    return new LuceneOrderedDocCollector(
        new ShardId("dummy", UUIDs.randomBase64UUID(), shardId),
        searcher,
        new MatchAllDocsQuery(),
        null,
        false,
        5, // batchSize < 10 to have at least one searchMore call.
        RamAccounting.NO_ACCOUNTING,
        collectorContext,
        f -> null,
        new Sort(new SortedNumericSortField(columnName, SortField.Type.LONG, reverseFlags[0])),
        expressions,
        expressions
    );
}
 
Example #3
Source File: SearchImplTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetSortType() {
  SearchImpl search = new SearchImpl(reader);

  assertFalse(search.getSortType("f1", "STRING", false).isPresent());

  assertEquals(new SortField("f2", SortField.Type.STRING, false),
      search.getSortType("f2", "STRING", false).get());
  assertFalse(search.getSortType("f2", "INT", false).isPresent());

  assertEquals(new SortedSetSortField("f3", false),
      search.getSortType("f3", "CUSTOM", false).get());

  assertEquals(new SortField("f4", SortField.Type.LONG, false),
      search.getSortType("f4", "LONG", false).get());
  assertFalse(search.getSortType("f4", "STRING", false).isPresent());

  assertEquals(new SortField("f5", SortField.Type.FLOAT, false),
      search.getSortType("f5", "FLOAT", false).get());
  assertFalse(search.getSortType("f5", "STRING", false).isPresent());

  assertEquals(new SortField("f6", SortField.Type.DOUBLE, false),
      search.getSortType("f6", "DOUBLE", false).get());
  assertFalse(search.getSortType("f6", "STRING", false).isPresent());

  assertEquals(new SortedNumericSortField("f7", SortField.Type.LONG, false),
      search.getSortType("f7", "LONG", false).get());
  assertFalse(search.getSortType("f7", "STRING", false).isPresent());
}
 
Example #4
Source File: TestIndexSorting.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testWrongSortFieldType() throws Exception {
  Directory dir = newDirectory();
  List<Field> dvs = new ArrayList<>();
  dvs.add(new SortedDocValuesField("field", new BytesRef("")));
  dvs.add(new SortedSetDocValuesField("field", new BytesRef("")));
  dvs.add(new NumericDocValuesField("field", 42));
  dvs.add(new SortedNumericDocValuesField("field", 42));

  List<SortField> sortFields = new ArrayList<>();
  sortFields.add(new SortField("field", SortField.Type.STRING));
  sortFields.add(new SortedSetSortField("field", false));
  sortFields.add(new SortField("field", SortField.Type.INT));
  sortFields.add(new SortedNumericSortField("field", SortField.Type.INT));

  for (int i = 0; i < sortFields.size(); i++) {
    for (int j = 0; j < dvs.size(); j++) {
      if (i == j) {
        continue;
      }
      Sort indexSort = new Sort(sortFields.get(i));
      IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
      iwc.setIndexSort(indexSort);
      IndexWriter w = new IndexWriter(dir, iwc);
      Document doc = new Document();
      doc.add(dvs.get(j));
      IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc));
      assertThat(exc.getMessage(), containsString("expected field [field] to be "));
      doc.clear();
      doc.add(dvs.get(i));
      w.addDocument(doc);
      doc.add(dvs.get(j));
      exc = expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc));
      assertThat(exc.getMessage(), containsString("cannot change DocValues type"));
      w.rollback();
      IOUtils.close(w);
    }
  }
  IOUtils.close(dir);
}
 
Example #5
Source File: FieldType.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Same as {@link #getSortField} but using {@link SortedNumericSortField}.
 */
protected static SortField getSortedNumericSortField(SchemaField field, SortField.Type sortType,
                                                     SortedNumericSelector.Type selector,
                                                     boolean reverse, Object missingLow, Object missingHigh) {
                                                 
  field.checkSortability();
  SortField sf = new SortedNumericSortField(field.getName(), sortType, reverse, selector);
  applySetMissingValue(field, sf, missingLow, missingHigh);
  
  return sf;
}
 
Example #6
Source File: SearchImpl.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public List<SortField> guessSortTypes(String name) {
  FieldInfo finfo = IndexUtils.getFieldInfo(reader, name);
  if (finfo == null) {
    throw new LukeException("No such field: " + name, new IllegalArgumentException());
  }

  DocValuesType dvType = finfo.getDocValuesType();

  switch (dvType) {
    case NONE:
      return Collections.emptyList();

    case NUMERIC:
      return Arrays.stream(new SortField[]{
          new SortField(name, SortField.Type.INT),
          new SortField(name, SortField.Type.LONG),
          new SortField(name, SortField.Type.FLOAT),
          new SortField(name, SortField.Type.DOUBLE)
      }).collect(Collectors.toList());

    case SORTED_NUMERIC:
      return Arrays.stream(new SortField[]{
          new SortedNumericSortField(name, SortField.Type.INT),
          new SortedNumericSortField(name, SortField.Type.LONG),
          new SortedNumericSortField(name, SortField.Type.FLOAT),
          new SortedNumericSortField(name, SortField.Type.DOUBLE)
      }).collect(Collectors.toList());

    case SORTED:
      return Arrays.stream(new SortField[] {
          new SortField(name, SortField.Type.STRING),
          new SortField(name, SortField.Type.STRING_VAL)
      }).collect(Collectors.toList());

    case SORTED_SET:
      return Collections.singletonList(new SortedSetSortField(name, false));

    default:
      return Collections.singletonList(new SortField(name, SortField.Type.DOC));
  }

}
 
Example #7
Source File: SearchImplTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testGuessSortTypes() {
  SearchImpl search = new SearchImpl(reader);

  assertTrue(search.guessSortTypes("f1").isEmpty());

  assertArrayEquals(
      new SortField[]{
          new SortField("f2", SortField.Type.STRING),
          new SortField("f2", SortField.Type.STRING_VAL)},
      search.guessSortTypes("f2").toArray());

  assertArrayEquals(
      new SortField[]{new SortedSetSortField("f3", false)},
      search.guessSortTypes("f3").toArray());

  assertArrayEquals(
      new SortField[]{
          new SortField("f4", SortField.Type.INT),
          new SortField("f4", SortField.Type.LONG),
          new SortField("f4", SortField.Type.FLOAT),
          new SortField("f4", SortField.Type.DOUBLE)},
      search.guessSortTypes("f4").toArray());

  assertArrayEquals(
      new SortField[]{
          new SortField("f5", SortField.Type.INT),
          new SortField("f5", SortField.Type.LONG),
          new SortField("f5", SortField.Type.FLOAT),
          new SortField("f5", SortField.Type.DOUBLE)},
      search.guessSortTypes("f5").toArray());

  assertArrayEquals(
      new SortField[]{
          new SortField("f6", SortField.Type.INT),
          new SortField("f6", SortField.Type.LONG),
          new SortField("f6", SortField.Type.FLOAT),
          new SortField("f6", SortField.Type.DOUBLE)},
      search.guessSortTypes("f6").toArray());

  assertArrayEquals(
      new SortField[]{
          new SortedNumericSortField("f7", SortField.Type.INT),
          new SortedNumericSortField("f7", SortField.Type.LONG),
          new SortedNumericSortField("f7", SortField.Type.FLOAT),
          new SortedNumericSortField("f7", SortField.Type.DOUBLE)},
      search.guessSortTypes("f7").toArray());
}
 
Example #8
Source File: MultiValuedLongFieldSource.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public SortField getSortField(boolean reverse) {
  return new SortedNumericSortField(field, SortField.Type.LONG, reverse, selector);
}
 
Example #9
Source File: MultiValuedIntFieldSource.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public SortField getSortField(boolean reverse) {
  return new SortedNumericSortField(field, SortField.Type.INT, reverse, selector);
}
 
Example #10
Source File: MultiValuedDoubleFieldSource.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public SortField getSortField(boolean reverse) {
  return new SortedNumericSortField(field, SortField.Type.DOUBLE, reverse, selector);
}
 
Example #11
Source File: MultiValuedFloatFieldSource.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public SortField getSortField(boolean reverse) {
  return new SortedNumericSortField(field, SortField.Type.FLOAT, reverse, selector);
}
 
Example #12
Source File: TestIndexSorting.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testMultiValuedRandom1() throws IOException {
  boolean withDeletes = random().nextBoolean();
  Directory dir = newDirectory();
  IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
  Sort indexSort = new Sort(new SortedNumericSortField("foo", SortField.Type.LONG));
  iwc.setIndexSort(indexSort);
  IndexWriter w = new IndexWriter(dir, iwc);
  final int numDocs = atLeast(200);
  final FixedBitSet deleted = new FixedBitSet(numDocs);
  for (int i = 0; i < numDocs; ++i) {
    Document doc = new Document();
    int num = random().nextInt(10);
    for (int j = 0; j < num; j++) {
      doc.add(new SortedNumericDocValuesField("foo", random().nextInt(2000)));
    }
    doc.add(new StringField("id", Integer.toString(i), Store.YES));
    doc.add(new NumericDocValuesField("id", i));
    w.addDocument(doc);
    if (random().nextInt(5) == 0) {
      w.getReader().close();
    } else if (random().nextInt(30) == 0) {
      w.forceMerge(2);
    } else if (random().nextInt(4) == 0) {
      final int id = TestUtil.nextInt(random(), 0, i);
      deleted.set(id);
      w.deleteDocuments(new Term("id", Integer.toString(id)));
    }
  }

  DirectoryReader reader = w.getReader();
  // Now check that the index is consistent
  IndexSearcher searcher = newSearcher(reader);
  for (int i = 0; i < numDocs; ++i) {
    TermQuery termQuery = new TermQuery(new Term("id", Integer.toString(i)));
    final TopDocs topDocs = searcher.search(termQuery, 1);
    if (deleted.get(i)) {
      assertEquals(0, topDocs.totalHits.value);
    } else {
      assertEquals(1, topDocs.totalHits.value);
      NumericDocValues values = MultiDocValues.getNumericValues(reader, "id");
      assertEquals(topDocs.scoreDocs[0].doc, values.advance(topDocs.scoreDocs[0].doc));
      assertEquals(i, values.longValue());
      Document document = reader.document(topDocs.scoreDocs[0].doc);
      assertEquals(Integer.toString(i), document.get("id"));
    }
  }

  reader.close();
  w.close();
  dir.close();
}