Example usage for org.apache.solr.client.solrj SolrClient commit

List of usage examples for org.apache.solr.client.solrj SolrClient commit

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrClient commit.

Prototype

public UpdateResponse commit() throws SolrServerException, IOException 

Source Link

Document

Performs an explicit commit, causing pending documents to be committed for indexing waitFlush=true and waitSearcher=true to be inline with the defaults for plain HTTP access <p> Be very careful when triggering commits from the client side.

Usage

From source file:org.broadleafcommerce.core.search.service.solr.index.SolrIndexServiceImpl.java

License:Open Source License

@Override
public void deleteAllNamespaceDocuments(SolrClient server) throws ServiceException {
    try {/*from   w w  w. ja v  a2 s  .c om*/
        String deleteQuery = shs.getNamespaceFieldName() + ":(\"" + solrConfiguration.getNamespace() + "\")";
        LOG.debug("Deleting by query: " + deleteQuery);
        server.deleteByQuery(deleteQuery);

        //Explicitly do a hard commit here since we just deleted the entire index
        server.commit();
    } catch (Exception e) {
        if (ServiceException.class.isAssignableFrom(e.getClass())) {
            throw (ServiceException) e;
        }
        throw new ServiceException("Could not delete documents", e);
    }
}

From source file:org.broadleafcommerce.core.search.service.solr.index.SolrIndexServiceImpl.java

License:Open Source License

@Override
public void deleteAllDocuments(SolrClient server) throws ServiceException {
    try {//from w  w  w  .j  av  a 2s  .  c o m
        String deleteQuery = "*:*";
        LOG.debug("Deleting by query: " + deleteQuery);
        server.deleteByQuery(deleteQuery);
        server.commit();
    } catch (Exception e) {
        throw new ServiceException("Could not delete documents", e);
    }
}

From source file:org.hadatac.console.models.SecurityRole.java

License:Apache License

public void save() {
    SolrClient solrClient = new HttpSolrClient(
            Play.application().configuration().getString("hadatac.solr.users")
                    + Collections.AUTHENTICATE_ROLES);

    if (this.id_s == null) {
        this.id_s = UUID.randomUUID().toString();
    }//w  w w .j  a  va 2s.c  o m

    try {
        solrClient.addBean(this);
        solrClient.commit();
        solrClient.close();
    } catch (Exception e) {
        System.out.println("[ERROR] SecurityRole.save - Exception message: " + e.getMessage());
    }
}

From source file:org.intermine.api.searchengine.solr.SolrIndexHandler.java

License:GNU General Public License

@Override
public void createIndex(ObjectStore os, Map<String, List<FieldDescriptor>> classKeys) throws IOException {
    long time = System.currentTimeMillis();
    LOG.debug("Creating keyword search index...");

    SolrClient solrClient = SolrClientManager.getClientInstance(os);

    //delete previous documents in solr

    LOG.debug("Delete previous index begins");
    long deleteStartTime = System.currentTimeMillis();

    createFieldTypeDefinitions(solrClient);

    try {/*from w w  w .  j av  a2  s.  com*/
        solrClient.deleteByQuery("*:*");
        solrClient.commit();

    } catch (SolrServerException e) {
        LOG.error("Deleting old index failed", e);
    }

    LOG.debug(
            "Delete previous index ends and it took " + (System.currentTimeMillis() - deleteStartTime) + "ms");

    KeywordSearchPropertiesManager keywordSearchPropertiesManager = KeywordSearchPropertiesManager
            .getInstance(os);

    addFieldNameToSchema("classname", ANALYZED_FIELD_TYPE_NAME, false, true, solrClient);
    addFieldNameToSchema("Category", "string", false, true, solrClient);

    for (KeywordSearchFacetData facetData : keywordSearchPropertiesManager.getFacets()) {
        for (String field : facetData.getFields()) {
            addFieldNameToSchema(field, ANALYZED_FIELD_TYPE_NAME, false, true, solrClient);
            addFieldNameToSchema("facet_" + field, "string", false, true, solrClient);
            addCopyFieldToSchema(field, "facet_" + field, solrClient);
        }
    }

    LOG.info("Starting fetcher thread...");
    SolrObjectHandler fetchThread = new SolrObjectHandler(os, keywordSearchPropertiesManager.getClassKeys(),
            indexingQueue, keywordSearchPropertiesManager.getIgnoredClasses(),
            keywordSearchPropertiesManager.getIgnoredFields(),
            keywordSearchPropertiesManager.getSpecialReferences(),
            keywordSearchPropertiesManager.getClassBoost(), keywordSearchPropertiesManager.getFacets(),
            keywordSearchPropertiesManager.getAttributePrefixes(), solrClient);
    fetchThread.start();

    int indexed = 0;

    List<SolrInputDocument> solrInputDocuments = new ArrayList<SolrInputDocument>();

    // loop and index while we still have fetchers running
    LOG.debug("Starting to index...");

    long indexStartTime = System.currentTimeMillis();

    int tempDocs = 0;
    long tempTime = System.currentTimeMillis();

    while (indexingQueue.hasNext()) {
        SolrInputDocument doc = indexingQueue.next();

        solrInputDocuments.add(doc);

        indexed++;

        if (solrInputDocuments.size() == keywordSearchPropertiesManager.getIndexBatchSize()) {

            tempTime = System.currentTimeMillis();

            addSolrDocuments(solrClient, solrInputDocuments);

            tempDocs = indexed - tempDocs;

            LOG.info("docs indexed=" + indexed + "; thread state=" + fetchThread.getState() + "; docs/ms="
                    + tempDocs * 1.0F / (System.currentTimeMillis() - tempTime) + "; memory="
                    + Runtime.getRuntime().freeMemory() / 1024 + "k/" + Runtime.getRuntime().maxMemory() / 1024
                    + "k" + "; time=" + (System.currentTimeMillis() - time) + "ms");

            solrInputDocuments.clear();
        }

    }

    addSolrDocuments(solrClient, solrInputDocuments);

    commit(solrClient);

    if (keywordSearchPropertiesManager.getEnableOptimize()) {
        optimize(solrClient);
    }

    LOG.debug("Solr indexing ends and it took " + (System.currentTimeMillis() - indexStartTime) + "ms");

    if (fetchThread.getException() != null) {

        throw new RuntimeException("Indexing failed.", fetchThread.getException());
    }

    time = System.currentTimeMillis() - time;
    int seconds = (int) Math.floor(time / 1000);
    LOG.info("Indexing of " + indexed + " documents finished in "
            + String.format("%02d:%02d.%03d", (int) Math.floor(seconds / 60), seconds % 60, time % 1000)
            + " minutes");
}

From source file:org.intermine.api.searchengine.solr.SolrIndexHandler.java

License:GNU General Public License

private void commit(SolrClient solrClient) throws IOException {
    try {//ww  w.j av a 2s. co m
        solrClient.commit();

    } catch (SolrServerException e) {
        LOG.error("Error while commiting.", e);
        e.printStackTrace();
    }
}

From source file:org.intermine.web.autocompletion.AutoCompleter.java

License:GNU General Public License

/**
 * Build the index from the database blob
 * @param os Objectstore/*from   w w  w.  jav  a2  s . c  o  m*/
 * @throws IOException IOException
 * @throws ObjectStoreException ObjectStoreException
 * @throws ClassNotFoundException ClassNotFoundException
 */
public void buildIndex(ObjectStore os) throws IOException, ObjectStoreException, ClassNotFoundException {

    List<SolrInputDocument> solrDocumentList = new ArrayList<SolrInputDocument>();
    List<String> fieldList = new ArrayList<String>();

    fieldList.add(CLASSNAME_FIELD);

    for (Map.Entry<String, String> entry : classFieldMap.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        String className = key;
        ClassDescriptor cld = os.getModel().getClassDescriptorByName(className);
        if (cld == null) {
            throw new RuntimeException("a class mentioned in ObjectStore summary properties " + "file ("
                    + className + ") is not in the model");
        }
        List<String> fieldNames = Arrays.asList(value.split(" "));
        for (Iterator<String> i = fieldNames.iterator(); i.hasNext();) {

            String fieldName = i.next();
            String classAndField = cld.getUnqualifiedName() + "." + fieldName;
            System.out.println("Indexing " + classAndField);

            if (!fieldList.contains(fieldName)) {
                fieldList.add(fieldName);
            }

            Query q = new Query();
            q.setDistinct(true);
            QueryClass qc = new QueryClass(Class.forName(cld.getName()));
            q.addToSelect(new QueryField(qc, fieldName));
            q.addFrom(qc);
            Results results = os.execute(q);

            for (Object resRow : results) {
                @SuppressWarnings("rawtypes")
                Object fieldValue = ((ResultsRow) resRow).get(0);
                if (fieldValue != null) {
                    SolrInputDocument solrInputDocument = new SolrInputDocument();
                    solrInputDocument.addField(fieldName, fieldValue.toString());
                    solrInputDocument.addField(CLASSNAME_FIELD, cld.getUnqualifiedName());
                    solrDocumentList.add(solrInputDocument);
                }
            }
        }
    }

    SolrClient solrClient = SolrClientHandler.getClientInstance(this.propertiesManager.getSolrUrl());

    try {
        solrClient.deleteByQuery("*:*");
        solrClient.commit();
    } catch (SolrServerException e) {
        LOG.error("Deleting old index failed", e);
    } catch (IOException e) {
        e.printStackTrace();
    }

    for (String fieldName : fieldList) {
        Map<String, Object> fieldAttributes = new HashMap();
        fieldAttributes.put("name", fieldName);
        fieldAttributes.put("type", "text_general");
        fieldAttributes.put("stored", true);
        fieldAttributes.put("indexed", true);
        fieldAttributes.put("multiValued", true);
        fieldAttributes.put("required", false);

        try {
            SchemaRequest.AddField schemaRequest = new SchemaRequest.AddField(fieldAttributes);
            SchemaResponse.UpdateResponse response = schemaRequest.process(solrClient);

        } catch (SolrServerException e) {
            LOG.error("Error while adding autocomplete fields to the solrclient.", e);
            e.printStackTrace();
        }
    }

    try {
        UpdateResponse response = solrClient.add(solrDocumentList);

        solrClient.commit();
    } catch (SolrServerException e) {

        LOG.error("Error while commiting the AutoComplete " + "SolrInputdocuments to the Solrclient. "
                + "Make sure the Solr instance is up", e);

        e.printStackTrace();
    }
}

From source file:org.opencms.search.solr.spellchecking.CmsSpellcheckDictionaryIndexer.java

License:Open Source License

/**
 * Add a list of documents to the Solr client.<p>
 *
 * @param client The SolrClient instance object.
 * @param documents The documents that should be added.
 * @param commit boolean flag indicating whether a "commit" call should be made after adding the documents
 *
 * @throws IOException in case something goes wrong
 * @throws SolrServerException in case something goes wrong
 *///from w  w w . j  a v a 2  s.  c  om
static void addDocuments(SolrClient client, List<SolrInputDocument> documents, boolean commit)
        throws IOException, SolrServerException {

    if ((null == client) || (null == documents)) {
        return;
    }

    if (!documents.isEmpty()) {
        client.add(documents);
    }

    if (commit) {
        client.commit();
    }
}

From source file:org.opencms.search.solr.spellchecking.CmsSpellcheckDictionaryIndexer.java

License:Open Source License

/**
 * Deletes all documents from the Solr client.<p>
 *
 * @param client The SolrClient instance object.
 *
 * @throws IOException in case something goes wrong
 * @throws SolrServerException in case something goes wrong
 *///from   w  ww.  j a v  a  2 s .c  o  m
static void deleteAllFiles(SolrClient client) throws IOException, SolrServerException {

    if (null == client) {
        return;
    }

    client.deleteByQuery("*:*");
    client.commit();
}

From source file:richtercloud.solr.bean.indexing.NewMain.java

/**
 * @param args the command line arguments
 *///from   w  w w . j  ava 2s. c o m
public static void main(String[] args) {
    SolrClient solrServer;
    solrServer = new HttpSolrClient.Builder("http://localhost:8983/solr/test1").build();
    List<MyBean> myBeans = new LinkedList<>(
            Arrays.asList(new MyBean("a", "1", 1), new MyBean("b", "2", 2), new MyBean("c", "3", 3)));
    String searchTerm = "a";
    try {
        solrServer.addBeans(myBeans);
        solrServer.commit();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q", searchTerm);
        QueryResponse queryResponse = solrServer.query(solrQuery);
        List<MyBean> foundDocuments = queryResponse.getBeans(MyBean.class);
        System.out.println(foundDocuments);
    } catch (SolrServerException | IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:se.uu.ub.cora.solrindex.SolrRecordIndexer.java

License:Open Source License

private void sendDocumentToSolr() {
    try {/*from w  w  w .ja  v  a 2  s  . c  o m*/
        SolrClient solrClient = solrClientProvider.getSolrClient();
        solrClient.add(document);
        solrClient.commit();
    } catch (Exception e) {
        throw SolrIndexException.withMessage(
                "Error while indexing record with type: " + type + " and id: " + id + " " + e.getMessage());
    }
}