Search in sources :

Example 6 with SolrDocument

use of org.apache.solr.common.SolrDocument in project camel by apache.

the class SolrUpdateTest method indexDocumentsToCSVUpdateHandlerWithParameters.

@Test
public void indexDocumentsToCSVUpdateHandlerWithParameters() throws Exception {
    solrEndpoint.setRequestHandler("/update/csv");
    Exchange exchange = createExchangeWithBody(new File("src/test/resources/data/books.csv"));
    exchange.getIn().setHeader(SolrConstants.OPERATION, SolrConstants.OPERATION_INSERT);
    exchange.getIn().setHeader(SolrConstants.PARAM + UpdateParams.ASSUME_CONTENT_TYPE, "text/csv");
    exchange.getIn().setHeader("SolrParam.fieldnames", "id,cat,name,price,inStock,author_t,series_t,sequence_i,genre_s");
    exchange.getIn().setHeader("SolrParam.skip", "cat,sequence_i,genre_s");
    exchange.getIn().setHeader("SolrParam.skipLines", 1);
    template.send("direct:start", exchange);
    solrCommit();
    QueryResponse response = executeSolrQuery("*:*");
    assertEquals(0, response.getStatus());
    assertEquals(10, response.getResults().getNumFound());
    SolrDocument doc = response.getResults().get(0);
    assertFalse(doc.getFieldNames().contains("cat"));
}
Also used : Exchange(org.apache.camel.Exchange) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) File(java.io.File) Test(org.junit.Test)

Example 7 with SolrDocument

use of org.apache.solr.common.SolrDocument in project camel by apache.

the class SolrUpdateTest method indexPDFDocumentToExtractingRequestHandler.

@Test
public void indexPDFDocumentToExtractingRequestHandler() throws Exception {
    solrEndpoint.setRequestHandler("/update/extract");
    Exchange exchange = createExchangeWithBody(new File("src/test/resources/data/tutorial.pdf"));
    exchange.getIn().setHeader(SolrConstants.OPERATION, SolrConstants.OPERATION_INSERT);
    exchange.getIn().setHeader("SolrParam.literal.id", "tutorial.pdf");
    template.send("direct:start", exchange);
    solrCommit();
    QueryResponse response = executeSolrQuery("*:*");
    assertEquals(0, response.getStatus());
    assertEquals(1, response.getResults().getNumFound());
    SolrDocument doc = response.getResults().get(0);
    assertEquals("Solr", doc.getFieldValue("subject"));
    assertEquals("tutorial.pdf", doc.getFieldValue("id"));
    assertEquals(Arrays.asList("application/pdf"), doc.getFieldValue("content_type"));
}
Also used : Exchange(org.apache.camel.Exchange) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) File(java.io.File) Test(org.junit.Test)

Example 8 with SolrDocument

use of org.apache.solr.common.SolrDocument in project titan by thinkaurelius.

the class SolrIndex method query.

@Override
public Iterable<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    List<RawQuery.Result<String>> result;
    String collection = query.getStore();
    String keyIdField = getKeyFieldId(collection);
    SolrQuery solrQuery = new SolrQuery(query.getQuery()).addField(keyIdField).setIncludeScore(true).setStart(query.getOffset()).setRows(query.hasLimit() ? query.getLimit() : maxResults);
    try {
        QueryResponse response = solrClient.query(collection, solrQuery);
        if (logger.isDebugEnabled())
            logger.debug("Executed query [{}] in {} ms", query.getQuery(), response.getElapsedTime());
        int totalHits = response.getResults().size();
        if (!query.hasLimit() && totalHits >= maxResults) {
            logger.warn("Query result set truncated to first [{}] elements for query: {}", maxResults, query);
        }
        result = new ArrayList<RawQuery.Result<String>>(totalHits);
        for (SolrDocument hit : response.getResults()) {
            double score = Double.parseDouble(hit.getFieldValue("score").toString());
            result.add(new RawQuery.Result<String>(hit.getFieldValue(keyIdField).toString(), score));
        }
    } catch (IOException e) {
        logger.error("Query did not complete : ", e);
        throw new PermanentBackendException(e);
    } catch (SolrServerException e) {
        logger.error("Unable to query Solr index.", e);
        throw new PermanentBackendException(e);
    }
    return result;
}
Also used : IOException(java.io.IOException) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse)

Example 9 with SolrDocument

use of org.apache.solr.common.SolrDocument in project titan by thinkaurelius.

the class SolrIndex method query.

@Override
public List<String> query(IndexQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    List<String> result;
    String collection = query.getStore();
    String keyIdField = getKeyFieldId(collection);
    SolrQuery solrQuery = new SolrQuery("*:*");
    String queryFilter = buildQueryFilter(query.getCondition(), informations.get(collection));
    solrQuery.addFilterQuery(queryFilter);
    if (!query.getOrder().isEmpty()) {
        List<IndexQuery.OrderEntry> orders = query.getOrder();
        for (IndexQuery.OrderEntry order1 : orders) {
            String item = order1.getKey();
            SolrQuery.ORDER order = order1.getOrder() == Order.ASC ? SolrQuery.ORDER.asc : SolrQuery.ORDER.desc;
            solrQuery.addSort(new SolrQuery.SortClause(item, order));
        }
    }
    solrQuery.setStart(0);
    if (query.hasLimit()) {
        solrQuery.setRows(query.getLimit());
    } else {
        solrQuery.setRows(maxResults);
    }
    try {
        QueryResponse response = solrClient.query(collection, solrQuery);
        if (logger.isDebugEnabled())
            logger.debug("Executed query [{}] in {} ms", query.getCondition(), response.getElapsedTime());
        int totalHits = response.getResults().size();
        if (!query.hasLimit() && totalHits >= maxResults)
            logger.warn("Query result set truncated to first [{}] elements for query: {}", maxResults, query);
        result = new ArrayList<String>(totalHits);
        for (SolrDocument hit : response.getResults()) {
            result.add(hit.getFieldValue(keyIdField).toString());
        }
    } catch (IOException e) {
        logger.error("Query did not complete : ", e);
        throw new PermanentBackendException(e);
    } catch (SolrServerException e) {
        logger.error("Unable to query Solr index.", e);
        throw new PermanentBackendException(e);
    }
    return result;
}
Also used : IOException(java.io.IOException) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse)

Example 10 with SolrDocument

use of org.apache.solr.common.SolrDocument in project YCSB by brianfrankcooper.

the class SolrClient method scan.

/**
   * Perform a range scan for a set of records in the database. Each field/value pair from the
   * result will be stored in a HashMap.
   *
   * @param table
   *          The name of the table
   * @param startkey
   *          The record key of the first record to read.
   * @param recordcount
   *          The number of records to read
   * @param fields
   *          The list of fields to read, or null for all of them
   * @param result
   *          A Vector of HashMaps, where each HashMap is a set field/value pairs for one record
   * @return Zero on success, a non-zero error code on error. See this class's description for a
   *         discussion of error codes.
   */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    try {
        Boolean returnFields = false;
        String[] fieldList = null;
        if (fields != null) {
            returnFields = true;
            fieldList = fields.toArray(new String[fields.size()]);
        }
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        query.setParam("fq", "id:[ " + startkey + " TO * ]");
        if (returnFields) {
            query.setFields(fieldList);
        }
        query.setRows(recordcount);
        final QueryResponse response = client.query(table, query);
        SolrDocumentList results = response.getResults();
        HashMap<String, ByteIterator> entry;
        for (SolrDocument hit : results) {
            entry = new HashMap<>((int) results.getNumFound());
            for (String field : hit.getFieldNames()) {
                entry.put(field, new StringByteIterator(String.valueOf(hit.getFirstValue(field))));
            }
            result.add(entry);
        }
        return checkStatus(response.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrDocument(org.apache.solr.common.SolrDocument) StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) StringByteIterator(com.yahoo.ycsb.StringByteIterator)

Aggregations

SolrDocument (org.apache.solr.common.SolrDocument)227 SolrDocumentList (org.apache.solr.common.SolrDocumentList)92 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)78 ArrayList (java.util.ArrayList)49 Test (org.junit.Test)46 SolrQuery (org.apache.solr.client.solrj.SolrQuery)45 SolrParams (org.apache.solr.common.params.SolrParams)37 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)35 SolrServerException (org.apache.solr.client.solrj.SolrServerException)33 IOException (java.io.IOException)31 SolrInputDocument (org.apache.solr.common.SolrInputDocument)28 HashMap (java.util.HashMap)25 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)24 NamedList (org.apache.solr.common.util.NamedList)21 Map (java.util.Map)19 List (java.util.List)18 SolrClient (org.apache.solr.client.solrj.SolrClient)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)12 SolrException (org.apache.solr.common.SolrException)12