Java Code Examples for org.apache.solr.search.SortSpec

The following examples show how to use org.apache.solr.search.SortSpec. 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: QueryElevationComponent.java    License: Apache License 2.0 6 votes vote down vote up
private void setSortSpec(ResponseBuilder rb, boolean forceElevation, ElevationComparatorSource comparator) {
  // if the sort is 'score desc' use a custom sorting method to
  // insert documents in their proper place
  SortSpec sortSpec = rb.getSortSpec();
  if (sortSpec.getSort() == null) {
    sortSpec.setSortAndFields(
            new Sort(
                    new SortField("_elevate_", comparator, true),
                    new SortField(null, SortField.Type.SCORE, false)),
            Arrays.asList(new SchemaField[2]));
  } else {
    // Check if the sort is based on score
    SortSpec modSortSpec = this.modifySortSpec(sortSpec, forceElevation, comparator);
    if (null != modSortSpec) {
      rb.setSortSpec(modSortSpec);
    }
  }
}
 
Example 2
Source Project: lucene-solr   Source File: QueryElevationComponent.java    License: Apache License 2.0 6 votes vote down vote up
private void setGroupingSpec(ResponseBuilder rb, boolean forceElevation, ElevationComparatorSource comparator) {
  // alter the sorting in the grouping specification if there is one
  GroupingSpecification groupingSpec = rb.getGroupingSpec();
  if(groupingSpec != null) {
    SortSpec groupSortSpec = groupingSpec.getGroupSortSpec();
    SortSpec modGroupSortSpec = this.modifySortSpec(groupSortSpec, forceElevation, comparator);
    if (modGroupSortSpec != null) {
      groupingSpec.setGroupSortSpec(modGroupSortSpec);
    }
    SortSpec withinGroupSortSpec = groupingSpec.getWithinGroupSortSpec();
    SortSpec modWithinGroupSortSpec = this.modifySortSpec(withinGroupSortSpec, forceElevation, comparator);
    if (modWithinGroupSortSpec != null) {
      groupingSpec.setWithinGroupSortSpec(modWithinGroupSortSpec);
    }
  }
}
 
Example 3
Source Project: lucene-solr   Source File: TestNestedDocsSort.java    License: Apache License 2.0 6 votes vote down vote up
private SortField parse(String a) {
    final SolrQueryRequest req = req("q", "{!parent which=type_s1:parent}whatever_s1:foo",
        "q2", "{!parent which=type_s1:parent}nomater_s1:what",
        "notbjq", "foo_s1:bar");
    try {
    final SortSpec spec = SortSpecParsing.parseSortSpec(a,
        req);
    assertNull(spec.getSchemaFields().get(0));
    final Sort sort = spec.getSort();
    final SortField field = sort.getSort()[0];
    assertNotNull(field);
    return field;
  } finally {
    req.close();
  }
}
 
Example 4
Source Project: lucene-solr   Source File: QueryElevationComponent.java    License: Apache License 2.0 5 votes vote down vote up
private SortSpec modifySortSpec(SortSpec current, boolean forceElevation, ElevationComparatorSource comparator) {
  boolean modify = false;
  SortField[] currentSorts = current.getSort().getSort();
  List<SchemaField> currentFields = current.getSchemaFields();

  ArrayList<SortField> sorts = new ArrayList<>(currentSorts.length + 1);
  List<SchemaField> fields = new ArrayList<>(currentFields.size() + 1);

  // Perhaps force it to always sort by score
  if (forceElevation && currentSorts[0].getType() != SortField.Type.SCORE) {
    sorts.add(new SortField("_elevate_", comparator, true));
    fields.add(null);
    modify = true;
  }
  for (int i = 0; i < currentSorts.length; i++) {
    SortField sf = currentSorts[i];
    if (sf.getType() == SortField.Type.SCORE) {
      sorts.add(new SortField("_elevate_", comparator, !sf.getReverse()));
      fields.add(null);
      modify = true;
    }
    sorts.add(sf);
    fields.add(currentFields.get(i));
  }
  return modify ?
          new SortSpec(new Sort(sorts.toArray(new SortField[0])),
                  fields,
                  current.getCount(),
                  current.getOffset())
          : null;
}
 
Example 5
Source Project: SolRDF   Source File: DeepPagingIterator.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Builds a new iterator with the given data.
 * 
 * @param searcher the Solr index searcher.
 * @param queryCommand the query command that will be submitted.static 
 * @param sort the sort specs.
 * @param consumer the Graph event consumer that will be notified on relevant events.
 */
DeepPagingIterator(
		final SolrIndexSearcher searcher, 
		final SolrIndexSearcher.QueryCommand queryCommand, 
		final SortSpec sort, 
		final GraphEventConsumer consumer) {
	this.searcher = searcher;
	this.queryCommand = queryCommand;
	this.sentCursorMark = new CursorMark(searcher.getSchema(), sort);
	this.queryCommand.setCursorMark(sentCursorMark);
	this.consumer = consumer;
}
 
Example 6
Source Project: SolRDF   Source File: LocalGraph.java    License: Apache License 2.0 5 votes vote down vote up
SortSpec sortSpec() throws SyntaxError {
	if (sortSpec == null) {
			sortSpec = qParser != null 
		    		? qParser.getSort(true) 
		    		: QueryParsing.parseSortSpec("id asc", request);
		    sortSpec.setOffset(0);		
	}
	return sortSpec;
}
 
Example 7
Source Project: BioSolr   Source File: DJoinMergeStrategy.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private NamedList unmarshalSortValues(SortSpec sortSpec, NamedList sortFieldValues, IndexSchema schema) {
  NamedList unmarshalledSortValsPerField = new NamedList();

  if (0 == sortFieldValues.size())
    return unmarshalledSortValsPerField;

  List<SchemaField> schemaFields = sortSpec.getSchemaFields();
  SortField[] sortFields = sortSpec.getSort().getSort();

  int marshalledFieldNum = 0;
  for (int sortFieldNum = 0; sortFieldNum < sortFields.length; sortFieldNum++) {
    final SortField sortField = sortFields[sortFieldNum];
    final SortField.Type type = sortField.getType();

    // :TODO: would be simpler to always serialize every position of SortField[]
    if (type == SortField.Type.SCORE || type == SortField.Type.DOC)
      continue;

    final String sortFieldName = sortField.getField();
    final String valueFieldName = sortFieldValues.getName(marshalledFieldNum);
    assert sortFieldName.equals(valueFieldName) : "sortFieldValues name key does not match expected SortField.getField";

    List sortVals = (List) sortFieldValues.getVal(marshalledFieldNum);

    final SchemaField schemaField = schemaFields.get(sortFieldNum);
    if (null == schemaField) {
      unmarshalledSortValsPerField.add(sortField.getField(), sortVals);
    } else {
      FieldType fieldType = schemaField.getType();
      List unmarshalledSortVals = new ArrayList();
      for (Object sortVal : sortVals) {
        unmarshalledSortVals.add(fieldType.unmarshalSortValue(sortVal));
      }
      unmarshalledSortValsPerField.add(sortField.getField(), unmarshalledSortVals);
    }
    marshalledFieldNum++;
  }
  return unmarshalledSortValsPerField;
}
 
Example 8
Source Project: lucene-solr   Source File: GroupingSpecification.java    License: Apache License 2.0 4 votes vote down vote up
public SortSpec getGroupSortSpec() {
  return groupSortSpec;
}
 
Example 9
Source Project: lucene-solr   Source File: GroupingSpecification.java    License: Apache License 2.0 4 votes vote down vote up
public void setGroupSortSpec(SortSpec groupSortSpec) {
  this.groupSortSpec = groupSortSpec;
}
 
Example 10
Source Project: lucene-solr   Source File: GroupingSpecification.java    License: Apache License 2.0 4 votes vote down vote up
public SortSpec getWithinGroupSortSpec() {
  return withinGroupSortSpec;
}
 
Example 11
Source Project: lucene-solr   Source File: GroupingSpecification.java    License: Apache License 2.0 4 votes vote down vote up
public void setWithinGroupSortSpec(SortSpec withinGroupSortSpec) {
  this.withinGroupSortSpec = withinGroupSortSpec;
}
 
Example 12
Source Project: lucene-solr   Source File: ResponseBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public SortSpec getSortSpec() {
  return sortSpec;
}
 
Example 13
Source Project: lucene-solr   Source File: ResponseBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public void setSortSpec(SortSpec sortSpec) {
  this.sortSpec = sortSpec;
}
 
Example 14
Source Project: lucene-solr   Source File: QueryComponent.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
protected NamedList unmarshalSortValues(SortSpec sortSpec,
                                      NamedList sortFieldValues, 
                                      IndexSchema schema) {
  NamedList unmarshalledSortValsPerField = new NamedList();

  if (0 == sortFieldValues.size()) return unmarshalledSortValsPerField;
  
  List<SchemaField> schemaFields = sortSpec.getSchemaFields();
  SortField[] sortFields = sortSpec.getSort().getSort();

  int marshalledFieldNum = 0;
  for (int sortFieldNum = 0; sortFieldNum < sortFields.length; sortFieldNum++) {
    final SortField sortField = sortFields[sortFieldNum];
    final SortField.Type type = sortField.getType();

    // :TODO: would be simpler to always serialize every position of SortField[]
    if (type==SortField.Type.SCORE || type==SortField.Type.DOC) continue;

    final String sortFieldName = sortField.getField();
    final String valueFieldName = sortFieldValues.getName(marshalledFieldNum);
    assert sortFieldName.equals(valueFieldName)
      : "sortFieldValues name key does not match expected SortField.getField";

    List sortVals = (List)sortFieldValues.getVal(marshalledFieldNum);

    final SchemaField schemaField = schemaFields.get(sortFieldNum);
    if (null == schemaField) {
      unmarshalledSortValsPerField.add(sortField.getField(), sortVals);
    } else {
      FieldType fieldType = schemaField.getType();
      List unmarshalledSortVals = new ArrayList();
      for (Object sortVal : sortVals) {
        unmarshalledSortVals.add(fieldType.unmarshalSortValue(sortVal));
      }
      unmarshalledSortValsPerField.add(sortField.getField(), unmarshalledSortVals);
    }
    marshalledFieldNum++;
  }
  return unmarshalledSortValsPerField;
}
 
Example 15
Source Project: SolRDF   Source File: LocalGraph.java    License: Apache License 2.0 4 votes vote down vote up
SolrIndexSearcher.QueryCommand queryCommand(final Triple pattern, final SortSpec sortSpec) throws SyntaxError {
    final SolrIndexSearcher.QueryCommand cmd = new SolrIndexSearcher.QueryCommand();
    cmd.setQuery(new MatchAllDocsQuery());
    cmd.setSort(sortSpec.getSort());
    cmd.setLen(queryFetchSize);
    cmd.setFlags(cmd.getFlags() | SolrIndexSearcher.GET_DOCSET);
    
    final List<Query> filters = new ArrayList<Query>();
    
	final Node s = pattern.getMatchSubject();
	final Node p = pattern.getMatchPredicate();
	final Node o = pattern.getMatchObject();
	
	if (s != null) {
		filters.add(new TermQuery(new Term(Field.S, asNt(s))));
	}
	
	if (p != null) {
		filters.add(new TermQuery(new Term(Field.P, asNtURI(p))));
	}
	
	if (o != null) {
		if (o.isLiteral()) {
			final String language = o.getLiteralLanguage();
			filters.add(
					isNotNullOrEmptyString(language) 
						? languageTermQuery(language) 
						: NULL_LANGUAGE_TERM_QUERY);
			
			final String literalValue = o.getLiteralLexicalForm(); 
			final RDFDatatype dataType = o.getLiteralDatatype();
			registry.get(
					dataType != null 
						? dataType.getURI() 
						: null).addFilterConstraint(filters, literalValue, request);
		} else {
			filters.add(new TermQuery(new Term(Field.TEXT_OBJECT, asNt(o))));		
		}
	}
	
	filters.add(graphTermQuery);				
	
	cmd.setFilterList(filters);
	return cmd;
}