Search in sources :

Example 1 with IndexReader

use of org.solbase.lucenehbase.IndexReader 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;
    }
}
Also used : SolrIndexReader(org.apache.solr.search.SolrIndexReader) IndexReader(org.solbase.lucenehbase.IndexReader) IOException(java.io.IOException)

Example 2 with IndexReader

use of org.solbase.lucenehbase.IndexReader 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());
}
Also used : DocIterator(org.apache.solr.search.DocIterator) ArrayList(java.util.ArrayList) SolrHighlighter(org.apache.solr.highlight.SolrHighlighter) SchemaField(org.apache.solr.schema.SchemaField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrIndexReader(org.apache.solr.search.SolrIndexReader) IndexReader(org.solbase.lucenehbase.IndexReader) SolrParams(org.apache.solr.common.params.SolrParams) FieldSelector(org.apache.lucene.document.FieldSelector) DocList(org.apache.solr.search.DocList)

Aggregations

SolrIndexReader (org.apache.solr.search.SolrIndexReader)2 IndexReader (org.solbase.lucenehbase.IndexReader)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 FieldSelector (org.apache.lucene.document.FieldSelector)1 SolrParams (org.apache.solr.common.params.SolrParams)1 SolrHighlighter (org.apache.solr.highlight.SolrHighlighter)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 SchemaField (org.apache.solr.schema.SchemaField)1 DocIterator (org.apache.solr.search.DocIterator)1 DocList (org.apache.solr.search.DocList)1