Search in sources :

Example 1 with SolrQuery

use of org.apache.solr.client.solrj.SolrQuery in project platformlayer by platformlayer.

the class ITSolrService method testSolrCustomField.

private void testSolrCustomField(String url, String field) throws SolrServerException, IOException {
    CommonsHttpSolrServer client = new CommonsHttpSolrServer(url);
    int docCount = 10;
    List<String> fieldValues = Lists.newArrayList();
    // Add some documents
    {
        for (int i = 0; i < docCount; i++) {
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", i);
            doc.addField("value_i", i);
            String fieldValue = random.randomText(40, 2000);
            doc.addField(field, fieldValue);
            fieldValues.add(fieldValue);
            client.add(doc);
        }
        client.commit();
    }
    // Query the documents
    {
        SolrQuery query = new SolrQuery();
        query.setQuery("value_i:[* TO 9]");
        query.addSortField("value_i", SolrQuery.ORDER.asc);
        query.setRows(Integer.MAX_VALUE);
        QueryResponse response = client.query(query);
        SolrDocumentList results = response.getResults();
        Assert.assertEquals(results.getNumFound(), 10);
        for (int i = 0; i < results.size(); i++) {
            SolrDocument doc = results.get(i);
            int docId = i;
            Assert.assertEquals(doc.get("id"), String.valueOf(docId));
            Assert.assertEquals(doc.get("value_i"), docId);
            Assert.assertEquals(doc.get(field), fieldValues.get(i));
        }
    }
}
Also used : CommonsHttpSolrServer(org.apache.solr.client.solrj.impl.CommonsHttpSolrServer) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 2 with SolrQuery

use of org.apache.solr.client.solrj.SolrQuery in project platformlayer by platformlayer.

the class ITSolrService method testSolr.

private void testSolr(String url) throws SolrServerException, IOException {
    CommonsHttpSolrServer client = new CommonsHttpSolrServer(url);
    int docCount = 1000;
    // Add some documents
    {
        for (int i = 0; i < docCount; i++) {
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", i);
            doc.addField("name_t", "document" + i);
            doc.addField("value_i", i);
            doc.addField("units_i", i % 10);
            doc.addField("content_t", random.randomText(40, 2000));
            client.add(doc);
        }
        client.commit();
    }
    // Query the documents
    {
        SolrQuery query = new SolrQuery();
        query.setQuery("units_i:2");
        query.addSortField("value_i", SolrQuery.ORDER.asc);
        query.setRows(Integer.MAX_VALUE);
        QueryResponse response = client.query(query);
        SolrDocumentList results = response.getResults();
        Assert.assertEquals(results.getNumFound(), docCount / 10);
        for (int i = 0; i < results.size(); i++) {
            SolrDocument doc = results.get(i);
            int docId = (i * 10) + 2;
            Assert.assertEquals(doc.get("id"), String.valueOf(docId));
            Assert.assertEquals(doc.get("units_i"), 2);
            Assert.assertEquals(doc.get("name_t"), "document" + docId);
        }
    }
}
Also used : CommonsHttpSolrServer(org.apache.solr.client.solrj.impl.CommonsHttpSolrServer) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 3 with SolrQuery

use of org.apache.solr.client.solrj.SolrQuery in project play-cookbook by spinscale.

the class SolrSearchTest method simpleUserTest.

@Test
public void simpleUserTest() throws Exception {
    SolrQuery query = new SolrQuery();
    query.setQuery("name:alex");
    QueryResponse rp = server.query(query);
    SolrDocumentList results = rp.getResults();
    assertEquals(1, results.size());
    assertEquals("alex", results.get(0).getFieldValue("name"));
    User u = User.find("byName", "alex").first();
    assertEquals(u.getClass().getName() + ":" + u.id.toString(), results.get(0).getFieldValue("id"));
}
Also used : User(models.User) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) UnitTest(play.test.UnitTest) Test(org.junit.Test)

Example 4 with SolrQuery

use of org.apache.solr.client.solrj.SolrQuery in project YCSB by brianfrankcooper.

the class SolrClient method read.

/**
   * Read a record from the database. Each field/value pair from the result will be stored in a
   * HashMap.
   *
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to read.
   * @param fields
   *          The list of fields to read, or null for all of them
   * @param result
   *          A HashMap of field/value pairs for the result
   * @return Zero on success, a non-zero error code on error or "not found".
   */
@Override
public Status read(String table, String key, Set<String> fields, 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("id:" + key);
        if (returnFields) {
            query.setFields(fieldList);
        }
        final QueryResponse response = client.query(table, query);
        SolrDocumentList results = response.getResults();
        if ((results != null) && (results.getNumFound() > 0)) {
            for (String field : results.get(0).getFieldNames()) {
                result.put(field, new StringByteIterator(String.valueOf(results.get(0).getFirstValue(field))));
            }
        }
        return checkStatus(response.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) StringByteIterator(com.yahoo.ycsb.StringByteIterator) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 5 with SolrQuery

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

SolrQuery (org.apache.solr.client.solrj.SolrQuery)316 Test (org.junit.Test)174 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)123 SolrInputDocument (org.apache.solr.common.SolrInputDocument)52 SolrServerException (org.apache.solr.client.solrj.SolrServerException)50 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)45 SolrDocument (org.apache.solr.common.SolrDocument)45 ArrayList (java.util.ArrayList)44 IOException (java.io.IOException)33 SolrDocumentList (org.apache.solr.common.SolrDocumentList)33 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)32 SolrClient (org.apache.solr.client.solrj.SolrClient)27 LinearModel (org.apache.solr.ltr.model.LinearModel)24 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)23 Replica (org.apache.solr.common.cloud.Replica)23 Slice (org.apache.solr.common.cloud.Slice)23 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)21 Map (java.util.Map)20 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)19 SolrException (org.apache.solr.common.SolrException)18