Java Code Examples for

The following examples show how to use 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:    License: Apache License 2.0 5 votes vote down vote up
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 Project: lucene-solr   Source File:    License: 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) {
      Sort indexSort = new Sort(sortFields.get(i));
      IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
      IndexWriter w = new IndexWriter(dir, iwc);
      Document doc = new Document();
      IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc));
      assertThat(exc.getMessage(), containsString("expected field [field] to be "));
      exc = expectThrows(IllegalArgumentException.class, () -> w.addDocument(doc));
      assertThat(exc.getMessage(), containsString("cannot change DocValues type"));
Example 3
Source Project: lucene-solr   Source File:    License: 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) {
  SortField sf = new SortedSetSortField(field.getName(), reverse, selector);
  applySetMissingValue(field, sf, missingLow, missingHigh);
  return sf;
Example 4
Source Project: lumongo   Source File:    License: 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);, 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);

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

	return totalHits;
Example 5
Source Project: lucene-solr   Source File:    License: Apache License 2.0 4 votes vote down vote up
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 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)

      return 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)

    case SORTED:
      return SortField[] {
          new SortField(name, SortField.Type.STRING),
          new SortField(name, SortField.Type.STRING_VAL)

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

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

Example 6
Source Project: lucene-solr   Source File:    License: Apache License 2.0 4 votes vote down vote up
public void testGuessSortTypes() {
  SearchImpl search = new SearchImpl(reader);


      new SortField[]{
          new SortField("f2", SortField.Type.STRING),
          new SortField("f2", SortField.Type.STRING_VAL)},

      new SortField[]{new SortedSetSortField("f3", false)},

      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)},

      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)},

      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)},

      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)},
Example 7
Source Project: lucene-solr   Source File:    License: Apache License 2.0 4 votes vote down vote up
public SortField getSortField(boolean reverse) {
  return new SortedSetSortField(this.field, reverse, this.selector);