Search in sources :

Example 6 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project camel by apache.

the class SolrUpdateTest method testInsertStreaming.

@Test
public void testInsertStreaming() throws Exception {
    Exchange exchange = createExchangeWithBody(null);
    exchange.getIn().setHeader(SolrConstants.OPERATION, SolrConstants.OPERATION_INSERT_STREAMING);
    exchange.getIn().setHeader("SolrField.id", "MA147LL/A");
    template.send("direct:start", exchange);
    Thread.sleep(500);
    solrCommit();
    QueryResponse response = executeSolrQuery("id:MA147LL/A");
    assertEquals(0, response.getStatus());
    assertEquals(1, response.getResults().getNumFound());
}
Also used : Exchange(org.apache.camel.Exchange) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Test(org.junit.Test)

Example 7 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project camel by apache.

the class SolrUpdateTest method testInsertSolrInputDocumentAsXMLWithAddRoot.

@Test
public void testInsertSolrInputDocumentAsXMLWithAddRoot() throws Exception {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", "MA147LL/A", 1.0f);
    String docAsXml = "<add>" + ClientUtils.toXML(doc) + "</add>";
    template.sendBodyAndHeader("direct:start", docAsXml, SolrConstants.OPERATION, SolrConstants.OPERATION_INSERT);
    solrCommit();
    QueryResponse response = executeSolrQuery("id:MA147LL/A");
    assertEquals(0, response.getStatus());
    assertEquals(1, response.getResults().getNumFound());
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Test(org.junit.Test)

Example 8 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse 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 9 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse 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 10 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse 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)

Aggregations

QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)285 SolrQuery (org.apache.solr.client.solrj.SolrQuery)123 Test (org.junit.Test)111 SolrDocument (org.apache.solr.common.SolrDocument)78 SolrInputDocument (org.apache.solr.common.SolrInputDocument)67 SolrDocumentList (org.apache.solr.common.SolrDocumentList)60 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)58 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)56 SolrServerException (org.apache.solr.client.solrj.SolrServerException)42 ArrayList (java.util.ArrayList)39 IOException (java.io.IOException)35 NamedList (org.apache.solr.common.util.NamedList)32 SolrClient (org.apache.solr.client.solrj.SolrClient)27 SolrParams (org.apache.solr.common.params.SolrParams)27 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)26 ErrorTrackingConcurrentUpdateSolrClient (org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient)25 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)25 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)23 HashMap (java.util.HashMap)21 PivotField (org.apache.solr.client.solrj.response.PivotField)19