Search in sources :

Example 1 with SolrInputDocument

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?
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) ArrayList(java.util.ArrayList) EntityState(org.qi4j.spi.entity.EntityState)

Example 2 with SolrInputDocument

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;
}
Also used : BNode(org.openrdf.model.BNode) Statement(org.openrdf.model.Statement) JSONArray(org.json.JSONArray) URIImpl(org.openrdf.model.impl.URIImpl) URI(org.openrdf.model.URI) Date(java.util.Date) SchemaField(org.apache.solr.schema.SchemaField) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Graph(org.openrdf.model.Graph) JSONObject(org.json.JSONObject) GraphImpl(org.openrdf.model.impl.GraphImpl) Literal(org.openrdf.model.Literal)

Example 3 with SolrInputDocument

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));
        }
    }
}
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 4 with SolrInputDocument

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);
        }
    }
}
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 5 with SolrInputDocument

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());
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Test(org.junit.Test)

Aggregations

SolrInputDocument (org.apache.solr.common.SolrInputDocument)511 Test (org.junit.Test)166 ArrayList (java.util.ArrayList)96 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)75 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)68 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)63 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)55 SolrQuery (org.apache.solr.client.solrj.SolrQuery)53 SolrException (org.apache.solr.common.SolrException)47 IOException (java.io.IOException)43 IndexSchema (org.apache.solr.schema.IndexSchema)41 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)41 HashMap (java.util.HashMap)34 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)34 List (java.util.List)31 SolrDocument (org.apache.solr.common.SolrDocument)31 NamedList (org.apache.solr.common.util.NamedList)31 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)29 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)29 SolrServerException (org.apache.solr.client.solrj.SolrServerException)28