mirror of
https://github.com/apache/lucene.git
synced 2025-02-06 18:18:38 +00:00
SOLR-417 -- moving SortSpec to a top level class.
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@599071 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e76c594d89
commit
46b26e7b21
@ -22,7 +22,7 @@ import org.apache.solr.common.util.NamedList;
|
||||
import org.apache.solr.common.util.RTimer;
|
||||
import org.apache.solr.search.DocListAndSet;
|
||||
import org.apache.solr.search.QParser;
|
||||
import org.apache.solr.search.QueryParsing;
|
||||
import org.apache.solr.search.SortSpec;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -43,7 +43,7 @@ public class ResponseBuilder
|
||||
private String queryString = null;
|
||||
private Query query = null;
|
||||
private List<Query> filters = null;
|
||||
private QueryParsing.SortSpec sortSpec = null;
|
||||
private SortSpec sortSpec = null;
|
||||
|
||||
private DocListAndSet results = null;
|
||||
private NamedList<Object> debugInfo = null;
|
||||
@ -141,11 +141,11 @@ public class ResponseBuilder
|
||||
this.results = results;
|
||||
}
|
||||
|
||||
public QueryParsing.SortSpec getSortSpec() {
|
||||
public SortSpec getSortSpec() {
|
||||
return sortSpec;
|
||||
}
|
||||
|
||||
public void setSortSpec(QueryParsing.SortSpec sort) {
|
||||
public void setSortSpec(SortSpec sort) {
|
||||
this.sortSpec = sort;
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@ package org.apache.solr.search;
|
||||
import org.apache.lucene.queryParser.ParseException;
|
||||
import org.apache.lucene.queryParser.QueryParser;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.Sort;
|
||||
import org.apache.solr.common.SolrException;
|
||||
import org.apache.solr.common.params.CommonParams;
|
||||
import org.apache.solr.common.params.SolrParams;
|
||||
@ -119,12 +120,12 @@ class OldLuceneQParser extends LuceneQParser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryParsing.SortSpec getSort(boolean useGlobal) throws ParseException {
|
||||
QueryParsing.SortSpec sort = super.getSort(useGlobal);
|
||||
public SortSpec getSort(boolean useGlobal) throws ParseException {
|
||||
SortSpec sort = super.getSort(useGlobal);
|
||||
if (sortStr != null && sortStr.length()>0 && sort.getSort()==null) {
|
||||
QueryParsing.SortSpec oldSort = QueryParsing.parseSort(sortStr, getReq().getSchema());
|
||||
Sort oldSort = QueryParsing.parseSort(sortStr, getReq().getSchema());
|
||||
if( oldSort != null ) {
|
||||
sort.sort = oldSort.sort;
|
||||
sort.sort = oldSort;
|
||||
}
|
||||
}
|
||||
return sort;
|
||||
|
@ -18,6 +18,7 @@ package org.apache.solr.search;
|
||||
|
||||
import org.apache.lucene.queryParser.ParseException;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.Sort;
|
||||
import org.apache.solr.common.params.CommonParams;
|
||||
import org.apache.solr.common.params.SolrParams;
|
||||
import org.apache.solr.common.util.NamedList;
|
||||
@ -120,7 +121,7 @@ public abstract class QParser {
|
||||
* @param useGlobalParams look up sort, start, rows in global params if not in local params
|
||||
* @return the sort specification
|
||||
*/
|
||||
public QueryParsing.SortSpec getSort(boolean useGlobalParams) throws ParseException {
|
||||
public SortSpec getSort(boolean useGlobalParams) throws ParseException {
|
||||
getQuery(); // ensure query is parsed first
|
||||
|
||||
String sortStr = null;
|
||||
@ -153,20 +154,11 @@ public abstract class QParser {
|
||||
int start = startS != null ? Integer.parseInt(startS) : 0;
|
||||
int rows = rowsS != null ? Integer.parseInt(rowsS) : 10;
|
||||
|
||||
QueryParsing.SortSpec sort = null;
|
||||
if (sortStr != null) {
|
||||
// may return null if 'score desc'
|
||||
Sort sort = null;
|
||||
if( sortStr != null ) {
|
||||
sort = QueryParsing.parseSort(sortStr, req.getSchema());
|
||||
}
|
||||
|
||||
if( sort == null ) {
|
||||
sort = new QueryParsing.SortSpec(null, start, rows);
|
||||
}
|
||||
else {
|
||||
sort.offset = start;
|
||||
sort.num = rows;
|
||||
}
|
||||
return sort;
|
||||
return new SortSpec( sort, start, rows );
|
||||
}
|
||||
|
||||
public String[] getDefaultHighlightFields() {
|
||||
|
@ -202,52 +202,6 @@ public class QueryParsing {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/***
|
||||
* SortSpec encapsulates a Lucene Sort and a count of the number of documents
|
||||
* to return.
|
||||
*/
|
||||
public static class SortSpec {
|
||||
Sort sort;
|
||||
int num;
|
||||
int offset;
|
||||
|
||||
SortSpec(Sort sort, int num) {
|
||||
this(sort,0,num);
|
||||
}
|
||||
|
||||
SortSpec(Sort sort, int offset, int num) {
|
||||
this.sort=sort;
|
||||
this.offset=offset;
|
||||
this.num=num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Lucene Sort object, or null for the default sort
|
||||
* by score descending.
|
||||
*/
|
||||
public Sort getSort() { return sort; }
|
||||
|
||||
/**
|
||||
* Offset into the list of results.
|
||||
*/
|
||||
public int getOffset() { return offset; }
|
||||
|
||||
/**
|
||||
* Gets the number of documens to return after sorting.
|
||||
*
|
||||
* @return number of docs to return, or -1 for no cut off (just sort)
|
||||
*/
|
||||
public int getCount() { return num; }
|
||||
|
||||
public String toString() {
|
||||
return "start="+offset+"&rows="+num
|
||||
+ (sort==null ? "" : "sort="+sort);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static Pattern sortSep = Pattern.compile(",");
|
||||
|
||||
/**
|
||||
@ -271,7 +225,7 @@ public class QueryParsing {
|
||||
* </pre>
|
||||
*
|
||||
*/
|
||||
public static SortSpec parseSort(String sortSpec, IndexSchema schema) {
|
||||
public static Sort parseSort(String sortSpec, IndexSchema schema) {
|
||||
if (sortSpec==null || sortSpec.length()==0) return null;
|
||||
|
||||
String[] parts = sortSep.split(sortSpec.trim());
|
||||
@ -285,24 +239,24 @@ public class QueryParsing {
|
||||
int idx = part.indexOf( ' ' );
|
||||
if( idx > 0 ) {
|
||||
String order = part.substring( idx+1 ).trim();
|
||||
if( "desc".equals( order ) || "top".equals(order) ) {
|
||||
top = true;
|
||||
}
|
||||
else if ("asc".equals(order) || "bottom".equals(order)) {
|
||||
top = false;
|
||||
}
|
||||
else {
|
||||
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Unknown sort order: "+order);
|
||||
}
|
||||
part = part.substring( 0, idx ).trim();
|
||||
if( "desc".equals( order ) || "top".equals(order) ) {
|
||||
top = true;
|
||||
}
|
||||
else if ("asc".equals(order) || "bottom".equals(order)) {
|
||||
top = false;
|
||||
}
|
||||
else {
|
||||
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Unknown sort order: "+order);
|
||||
}
|
||||
part = part.substring( 0, idx ).trim();
|
||||
}
|
||||
else {
|
||||
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Missing sort order." );
|
||||
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Missing sort order." );
|
||||
}
|
||||
|
||||
if( "score".equals(part) ) {
|
||||
if (top) {
|
||||
// If thre is only one thing in the list, just do the regular thing...
|
||||
// If there is only one thing in the list, just do the regular thing...
|
||||
if( parts.length == 1 ) {
|
||||
return null; // do normal scoring...
|
||||
}
|
||||
@ -327,9 +281,7 @@ public class QueryParsing {
|
||||
lst[i] = f.getType().getSortField(f,top);
|
||||
}
|
||||
}
|
||||
// For more info on the 'num' field, -1,
|
||||
// see: https://issues.apache.org/jira/browse/SOLR-99
|
||||
return new SortSpec( new Sort(lst),-1);
|
||||
return new Sort(lst);
|
||||
}
|
||||
|
||||
|
||||
|
64
src/java/org/apache/solr/search/SortSpec.java
Normal file
64
src/java/org/apache/solr/search/SortSpec.java
Normal file
@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.solr.search;
|
||||
|
||||
import org.apache.lucene.search.Sort;
|
||||
|
||||
/***
|
||||
* SortSpec encapsulates a Lucene Sort and a count of the number of documents
|
||||
* to return.
|
||||
*/
|
||||
public class SortSpec
|
||||
{
|
||||
Sort sort;
|
||||
int num;
|
||||
int offset;
|
||||
|
||||
public SortSpec(Sort sort, int num) {
|
||||
this(sort,0,num);
|
||||
}
|
||||
|
||||
public SortSpec(Sort sort, int offset, int num) {
|
||||
this.sort=sort;
|
||||
this.offset=offset;
|
||||
this.num=num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Lucene Sort object, or null for the default sort
|
||||
* by score descending.
|
||||
*/
|
||||
public Sort getSort() { return sort; }
|
||||
|
||||
/**
|
||||
* Offset into the list of results.
|
||||
*/
|
||||
public int getOffset() { return offset; }
|
||||
|
||||
/**
|
||||
* Gets the number of documents to return after sorting.
|
||||
*
|
||||
* @return number of docs to return, or -1 for no cut off (just sort)
|
||||
*/
|
||||
public int getCount() { return num; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "start="+offset+ "&rows="+num + (sort==null ? "" : "&sort="+sort);
|
||||
}
|
||||
}
|
@ -62,12 +62,7 @@ public class OldRequestHandler implements SolrRequestHandler {
|
||||
// we can use the Lucene sort ability.
|
||||
Sort sort = null;
|
||||
if (commands.size() >= 2) {
|
||||
QueryParsing.SortSpec sortSpec = QueryParsing.parseSort(commands.get(1), req.getSchema());
|
||||
if (sortSpec != null) {
|
||||
sort = sortSpec.getSort();
|
||||
// ignore the count for now... it's currently only controlled by start & limit on req
|
||||
// count = sortSpec.getCount();
|
||||
}
|
||||
sort = QueryParsing.parseSort(commands.get(1), req.getSchema());
|
||||
}
|
||||
|
||||
Hits hits=null;
|
||||
|
@ -103,12 +103,7 @@ public class TestRequestHandler implements SolrRequestHandler {
|
||||
// we can use the Lucene sort ability.
|
||||
Sort sort = null;
|
||||
if (commands.size() >= 2) {
|
||||
QueryParsing.SortSpec sortSpec = QueryParsing.parseSort(commands.get(1), req.getSchema());
|
||||
if (sortSpec != null) {
|
||||
sort = sortSpec.getSort();
|
||||
// ignore the count for now... it's currently only controlled by start & limit on req
|
||||
// count = sortSpec.getCount();
|
||||
}
|
||||
sort = QueryParsing.parseSort(commands.get(1), req.getSchema());
|
||||
}
|
||||
|
||||
SolrIndexSearcher searcher = req.getSearcher();
|
||||
|
@ -458,13 +458,7 @@ public class SolrPluginUtils {
|
||||
// we can use the Lucene sort ability.
|
||||
Sort sort = null;
|
||||
if (commands.size() >= 2) {
|
||||
QueryParsing.SortSpec sortSpec = QueryParsing.parseSort(commands.get(1), schema);
|
||||
if (sortSpec != null) {
|
||||
sort = sortSpec.getSort();
|
||||
if (sortSpec.getCount() >= 0) {
|
||||
limit = sortSpec.getCount();
|
||||
}
|
||||
}
|
||||
sort = QueryParsing.parseSort(commands.get(1), schema);
|
||||
}
|
||||
|
||||
DocList results = searcher.getDocList(query,(DocSet)null, sort, start, limit);
|
||||
@ -793,7 +787,7 @@ public class SolrPluginUtils {
|
||||
}
|
||||
|
||||
SolrException sortE = null;
|
||||
QueryParsing.SortSpec ss = null;
|
||||
Sort ss = null;
|
||||
try {
|
||||
ss = QueryParsing.parseSort(sort, req.getSchema());
|
||||
} catch (SolrException e) {
|
||||
@ -808,7 +802,7 @@ public class SolrPluginUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ss.getSort();
|
||||
return ss;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user