use of org.apache.solr.search.SolrIndexReader in project Solbase by Photobucket.
the class SolbaseComponent method prepare.
public void prepare(ResponseBuilder rb) throws IOException {
// Only applies to my lucandra index readers
if (rb.req.getSearcher().getIndexReader().getVersion() != Long.MAX_VALUE)
return;
if (!hasSolbaseSchema.get()) {
hasSolbaseSchema.set(true);
}
// If this is a shard request then no need to do anything
if (rb.req.getParams().getBool(ShardParams.IS_SHARD, false)) {
String indexName = (String) rb.req.getContext().get("solbase-index");
if (indexName == null)
throw new IOException("Missing core name");
logger.debug(indexName);
IndexReader reader = (IndexReader) ((SolrIndexReader) rb.req.getSearcher().getIndexReader()).getWrappedReader();
reader.setIndexName(indexName);
return;
}
String indexName = rb.req.getCore().getName();
if (indexName.equals("")) {
//
return;
} else {
logger.debug("core: " + indexName);
}
if (rb.shards == null) {
// find number of shards
// this is current max doc id
// for real time, we'd have to fetch max doc id from table. maybe put some caching around this number
//int docId = SolbaseUtil.getSequenceId();
int numShards = SolbaseShardUtil.getNumShard();
//run local
if (numShards == 0) {
IndexReader reader = (IndexReader) ((SolrIndexReader) rb.req.getSearcher().getIndexReader()).getWrappedReader();
String subIndex = indexName;
reader.setIndexName(subIndex);
return;
}
String[] shards = new String[numShards];
// assign shards
List<String> hosts = SolbaseShardUtil.getShardHosts();
for (int i = 0; i < hosts.size(); i++) {
String host = hosts.get(i);
String shard = host + "/solbase/" + indexName + "~" + i;
if (logger.isDebugEnabled())
logger.debug("Adding shard(" + indexName + "): " + shard);
shards[i] = shard;
}
// assign to shards
rb.shards = shards;
return;
}
}
use of org.apache.solr.search.SolrIndexReader in project Solbase by Photobucket.
the class SolbaseQueryComponent method process.
/**
* Actually run the query
*/
@Override
public void process(ResponseBuilder rb) throws IOException {
SolrQueryRequest req = rb.req;
SolrQueryResponse rsp = rb.rsp;
SolrParams params = req.getParams();
if (!params.getBool(COMPONENT_NAME, true)) {
return;
}
SolrIndexSearcher searcher = req.getSearcher();
if (rb.getQueryCommand().getOffset() < 0) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'start' parameter cannot be negative");
}
// -1 as flag if not set.
long timeAllowed = (long) params.getInt(CommonParams.TIME_ALLOWED, -1);
// Optional: This could also be implemented by the top-level searcher
// sending
// a filter that lists the ids... that would be transparent to
// the request handler, but would be more expensive (and would preserve
// score
// too if desired).
String ids = params.get(ShardParams.IDS);
if (ids != null) {
List<String> idArr = StrUtils.splitSmart(ids, ",", true);
int[] luceneIds = new int[idArr.size()];
int docs = 0;
for (int i = 0; i < idArr.size(); i++) {
luceneIds[docs++] = Integer.parseInt(idArr.get(i));
}
// we are indexing docId as solr uniq_id. by doing this, we are
// bound to INTEGER.MAX_VALUE ~= 2 billion
// docs is number of docs
DocListAndSet res = new DocListAndSet();
res.docList = new DocSlice(0, docs, luceneIds, null, docs, 0, null);
if (rb.isNeedDocSet()) {
List<Query> queries = new ArrayList<Query>();
queries.add(rb.getQuery());
List<Query> filters = rb.getFilters();
if (filters != null)
queries.addAll(filters);
res.docSet = searcher.getDocSet(queries);
}
rb.setResults(res);
rsp.add("response", rb.getResults().docList);
return;
}
SolrIndexSearcher.QueryCommand cmd = rb.getQueryCommand();
cmd.setTimeAllowed(timeAllowed);
SolrIndexSearcher.QueryResult result = new SolrIndexSearcher.QueryResult();
searcher.search(result, cmd);
rb.setResult(result);
rsp.add("response", rb.getResults().docList);
rsp.getToLog().add("hits", rb.getResults().docList.matches());
// The query cache doesn't currently store sort field values, and
// SolrIndexSearcher doesn't
// currently have an option to return sort field values. Because of
// this, we
// take the documents given and re-derive the sort values.
boolean fsv = req.getParams().getBool(ResponseBuilder.FIELD_SORT_VALUES, false);
if (fsv) {
Sort sort = rb.getSortSpec().getSort();
SortField[] sortFields = sort == null ? new SortField[] { SortField.FIELD_SCORE } : sort.getSort();
// order is important for the
NamedList sortVals = new NamedList();
// sort fields
// a
Field field = new Field("dummy", "", Field.Store.YES, Field.Index.NO);
// dummy
// Field
SolrIndexReader reader = searcher.getReader();
SolrIndexReader[] readers = reader.getLeafReaders();
SolrIndexReader subReader = reader;
if (readers.length == 1) {
// if there is a single segment, use that subReader and avoid
// looking up each time
subReader = readers[0];
readers = null;
}
int[] offsets = reader.getLeafOffsets();
//TODO: need to fetch sort value from collector instead of re-derive lookup from id
for (SortField sortField : sortFields) {
int type = sortField.getType();
if (type == SortField.SCORE || type == SortField.DOC)
continue;
FieldComparator comparator = null;
FieldComparator[] comparators = (readers == null) ? null : new FieldComparator[readers.length];
String fieldname = sortField.getField();
FieldType ft = fieldname == null ? null : req.getSchema().getFieldTypeNoEx(fieldname);
DocSlice docList = (DocSlice) rb.getResults().docList;
ArrayList<Object> vals = new ArrayList<Object>(docList.size());
for (int i = docList.offset; i < docList.len; i++) {
vals.add(new Integer(docList.sorts[i][((EmbeddedSortField) sortField).getFieldNumber() - 1]));
}
sortVals.add(fieldname, vals);
}
rsp.add("sort_values", sortVals);
}
// pre-fetch returned documents
if (!req.getParams().getBool(ShardParams.IS_SHARD, false) && rb.getResults().docList != null && rb.getResults().docList.size() <= 50) {
// TODO: this may depend on the highlighter component (or other
// components?)
SolrPluginUtils.optimizePreFetchDocs(rb.getResults().docList, rb.getQuery(), req, rsp);
}
}
use of org.apache.solr.search.SolrIndexReader in project Solbase by Photobucket.
the class SolbaseComponent method process.
public void process(ResponseBuilder rb) throws IOException {
DocList list = rb.getResults().docList;
DocIterator it = list.iterator();
List<Integer> docIds = new ArrayList<Integer>(list.size());
while (it.hasNext()) docIds.add(it.next());
IndexReader reader = (IndexReader) ((SolrIndexReader) rb.req.getSearcher().getIndexReader()).getWrappedReader();
SolrQueryRequest req = rb.req;
SolrParams params = req.getParams();
String ids = params.get(ShardParams.IDS);
// first phase of sharding only tries to fetch docids and scores which are already in tv
if (SolbaseShardUtil.getNumShard() != 0) {
if (ids != null) {
IndexReader.firstPhase.set(false);
} else {
IndexReader.firstPhase.set(true);
}
} else {
// it's always false in case of stand alone
IndexReader.firstPhase.set(false);
}
logger.debug(reader.getIndexName() + " : Fetching " + docIds.size() + " Docs");
if (docIds.size() > 0) {
List<byte[]> fieldFilter = null;
Set<String> returnFields = rb.rsp.getReturnFields();
if (returnFields != null) {
// copy return fields list
fieldFilter = new ArrayList<byte[]>(returnFields.size());
for (String field : returnFields) {
fieldFilter.add(Bytes.toBytes(field));
}
// add highlight fields
SolrHighlighter highligher = rb.req.getCore().getHighlighter();
if (highligher.isHighlightingEnabled(rb.req.getParams())) {
for (String field : highligher.getHighlightFields(rb.getQuery(), rb.req, null)) if (!returnFields.contains(field))
fieldFilter.add(Bytes.toBytes(field));
}
// fetch unique key if one exists.
SchemaField keyField = rb.req.getSearcher().getSchema().getUniqueKeyField();
if (null != keyField)
if (!returnFields.contains(keyField))
fieldFilter.add(Bytes.toBytes(keyField.getName()));
}
FieldSelector selector = new SolbaseFieldSelector(docIds, fieldFilter);
// This will bulk load these docs
rb.req.getSearcher().getReader().document(docIds.get(0), selector);
}
ReaderCache.flushThreadLocalCaches(reader.getIndexName());
}
Aggregations