org.apache.lucene.search.SortedSetSortField Java Examples

The following examples show how to use org.apache.lucene.search.SortedSetSortField. 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: 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 #2
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 #3
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 SortedSetSortField}
 */
protected static SortField getSortedSetSortField(SchemaField field, SortedSetSelector.Type selector,
                                                 boolean reverse, Object missingLow, Object missingHigh) {
                                                 
  field.checkSortability();
  SortField sf = new SortedSetSortField(field.getName(), reverse, selector);
  applySetMissingValue(field, sf, missingLow, missingHigh);
  
  return sf;
}
 
Example #4
Source File: BasicStorageTest.java    From lumongo with Apache License 2.0 5 votes vote down vote up
private static int runQuery(IndexReader indexReader, int count, Query q) throws IOException {
	long start = System.currentTimeMillis();
	IndexSearcher searcher = new IndexSearcher(indexReader);

	Sort sort = new Sort();

	sort.setSort(new SortedSetSortField("category", false));

	TopFieldCollector collector = TopFieldCollector.create(sort, count, null, true, true, true);

	searcher.search(q, collector);

	ScoreDoc[] hits = collector.topDocs().scoreDocs;
	int totalHits = collector.getTotalHits();
	@SuppressWarnings("unused") long searchTime = System.currentTimeMillis() - start;

	start = System.currentTimeMillis();

	List<String> ids = new ArrayList<>();
	for (ScoreDoc hit : hits) {
		int docId = hit.doc;
		Document d = searcher.doc(docId);
		ids.add(d.get("uid"));

	}
	@SuppressWarnings("unused") long fetchTime = System.currentTimeMillis() - start;

	return totalHits;
}
 
Example #5
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 #6
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 #7
Source File: SortedSetFieldSource.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public SortField getSortField(boolean reverse) {
  return new SortedSetSortField(this.field, reverse, this.selector);
}