use of org.apache.solr.common.SolrInputDocument in project qi4j-sdk by Qi4j.
the class SolrEntityIndexerMixin method notifyChanges.
@Override
public void notifyChanges(Iterable<EntityState> entityStates) {
try {
try {
// Figure out what to update
List<String> deleted = null;
List<SolrInputDocument> added = new ArrayList<SolrInputDocument>();
for (EntityState entityState : entityStates) {
if (entityState.entityDescriptor().queryable()) {
if (entityState.status().equals(EntityStatus.REMOVED)) {
if (deleted == null)
deleted = new ArrayList<String>();
deleted.add(entityState.identity().identity());
} else if (entityState.status().equals(EntityStatus.UPDATED)) {
added.add(indexEntityState(entityState, server));
} else if (entityState.status().equals(EntityStatus.NEW)) {
added.add(indexEntityState(entityState, server));
}
}
}
// Send changes to Solr
if (deleted != null)
server.deleteById(deleted);
if (!added.isEmpty())
server.add(added);
} finally {
if (server != null) {
server.commit(false, false);
}
}
} catch (Throwable e) {
logger.error("Could not update Solr", e);
//TODO What shall we do with the exception?
}
}
use of org.apache.solr.common.SolrInputDocument in project qi4j-sdk by Qi4j.
the class SolrEntityIndexerMixin method indexEntityState.
private SolrInputDocument indexEntityState(final EntityState entityState, final SolrServer server) throws IOException, SolrServerException, JSONException {
Graph graph = new GraphImpl();
stateSerializer.serialize(entityState, false, graph);
SolrInputDocument input = new SolrInputDocument();
input.addField("id", entityState.identity().identity());
input.addField("type", first(entityState.entityDescriptor().types()).getName());
input.addField("lastModified", new Date(entityState.lastModified()));
for (Statement statement : graph) {
SchemaField field = indexedFields.get(statement.getPredicate().getLocalName());
if (field != null) {
if (statement.getObject() instanceof Literal) {
String value = statement.getObject().stringValue();
if (field.getType().getTypeName().equals("json")) {
if (value.charAt(0) == '[') {
JSONArray array = new JSONArray(value);
indexJson(input, array);
} else if (value.charAt(0) == '{') {
JSONObject object = new JSONObject(value);
indexJson(input, object);
}
} else {
input.addField(field.getName(), value);
}
} else if (statement.getObject() instanceof URI && !"type".equals(field.getName())) {
String value = statement.getObject().stringValue();
value = value.substring(value.lastIndexOf(':') + 1, value.length());
String name = field.getName();
input.addField(name, value);
} else if (statement.getObject() instanceof BNode) {
Iterator<Statement> seq = graph.match((Resource) statement.getObject(), new URIImpl("http://www.w3.org/1999/02/22-rdf-syntax-ns#li"), null, (Resource) null);
while (seq.hasNext()) {
Statement seqStatement = seq.next();
String value = seqStatement.getObject().stringValue();
value = value.substring(value.lastIndexOf(':') + 1, value.length());
input.addField(field.getName(), value);
}
}
}
}
return input;
}
use of org.apache.solr.common.SolrInputDocument 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.common.SolrInputDocument 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.common.SolrInputDocument 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());
}
Aggregations