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));
}
}
}
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);
}
}
}
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"));
}
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;
}
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;
}
Aggregations