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

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

Introduction

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

Prototype

public UpdateResponse deleteByQuery(String query) throws SolrServerException, IOException 

Source Link

Document

Deletes documents from the index based on a query

Usage

From source file:com.frank.search.solr.core.SolrTemplate.java

License:Apache License

@Override
public UpdateResponse delete(SolrDataQuery query) {
    Assert.notNull(query, "Query must not be 'null'.");

    final String queryString = this.queryParsers.getForClass(query.getClass()).getQueryString(query);

    return execute(new SolrCallback<UpdateResponse>() {
        @Override/*w  w  w  . ja  va2  s . c om*/
        public UpdateResponse doInSolr(SolrClient solrClient) throws SolrServerException, IOException {
            return solrClient.deleteByQuery(queryString);
        }
    });
}

From source file:net.hasor.search.server.rsf.service.SorlDumpService.java

License:Apache License

private UpdateSearchResult deleteByQuery(final String queryString, final Integer commitWithinMs) {
    if (StringUtils.isBlank(queryString) == true) {
        UpdateSearchResult result = new UpdateSearchResult();
        result.setSuccess(false);/*  w  w  w .j  a  v a  2 s.c o m*/
        result.setMessage("queryString is empty or null.");
        LoggerHelper.logWarn("deleteByQuery failure, %s", result);
        return result;
    }
    //
    return this.doExecute(new ExecuteService() {
        @Override
        public UpdateResponse doExecute(SolrClient solrClient) throws Throwable {
            if (commitWithinMs == null) {
                return solrClient.deleteByQuery(queryString);
            } else {
                return solrClient.deleteByQuery(queryString, commitWithinMs.intValue());
            }
        }
    });
}

From source file:net.yacy.cora.federate.solr.instance.ServerShard.java

License:Open Source License

/**
 * Deletes documents from the index based on a query
 * @param query  the query expressing what documents to delete
 * @throws IOException If there is a low-level I/O error.
 *//*from   w w w  .j  a  v a 2  s  .co  m*/
@Override
public UpdateResponse deleteByQuery(String query) throws SolrServerException, IOException {
    if (!this.writeEnabled)
        return _dummyOKResponse;
    UpdateResponse ur = null;
    for (SolrClient s : this.shards.server4read())
        ur = s.deleteByQuery(query);
    return ur;
}

From source file:org.apache.nutch.indexwriter.solr.TestSolrJ.java

License:Apache License

/**
 * query the example/*ww w . ja v  a2 s.  c o m*/
 */
@Test
public void testDelete() throws Exception {
    SolrClient client = new HttpSolrClient.Builder(serverUrl).build();

    // Empty the database...
    client.deleteByQuery("*:*");// delete everything!
}

From source file:org.apache.nutch.indexwriter.solr.TestSolrJ.java

License:Apache License

/**
 * query the example/*w  w w. j  a  va  2 s.c o  m*/
 */
@Test
public void testQuery() throws Exception {
    SolrClient client = new HttpSolrClient.Builder(serverUrl).build();

    // Empty the database...
    client.deleteByQuery("*:*");// delete everything!

    // Now add something...
    SolrInputDocument doc = new SolrInputDocument();
    String docID = "1112211111";
    doc.addField("id", docID, 1.0f);
    doc.addField("name", "my name!", 1.0f);

    assertEquals(null, doc.getField("foo"));
    assertTrue(doc.getField("name").getValue() != null);

    UpdateResponse upres = client.add(doc);
    // System.out.println( "ADD:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    upres = client.commit(true, true);
    // System.out.println( "COMMIT:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    upres = client.optimize(true, true);
    // System.out.println( "OPTIMIZE:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    SolrQuery query = new SolrQuery();
    query.setQuery("id:" + docID);
    QueryResponse response = client.query(query);

    assertEquals(docID, response.getResults().get(0).getFieldValue("id"));

    // Now add a few docs for facet testing...
    List<SolrInputDocument> docs = new ArrayList<>();
    SolrInputDocument doc2 = new SolrInputDocument();
    doc2.addField("id", "2", 1.0f);
    doc2.addField("inStock", true, 1.0f);
    doc2.addField("price", 2, 1.0f);
    doc2.addField("timestamp_dt", new java.util.Date(), 1.0f);
    docs.add(doc2);
    SolrInputDocument doc3 = new SolrInputDocument();
    doc3.addField("id", "3", 1.0f);
    doc3.addField("inStock", false, 1.0f);
    doc3.addField("price", 3, 1.0f);
    doc3.addField("timestamp_dt", new java.util.Date(), 1.0f);
    docs.add(doc3);
    SolrInputDocument doc4 = new SolrInputDocument();
    doc4.addField("id", "4", 1.0f);
    doc4.addField("inStock", true, 1.0f);
    doc4.addField("price", 4, 1.0f);
    doc4.addField("timestamp_dt", new java.util.Date(), 1.0f);
    docs.add(doc4);
    SolrInputDocument doc5 = new SolrInputDocument();
    doc5.addField("id", "5", 1.0f);
    doc5.addField("inStock", false, 1.0f);
    doc5.addField("price", 5, 1.0f);
    doc5.addField("timestamp_dt", new java.util.Date(), 1.0f);
    docs.add(doc5);

    upres = client.add(docs);
    // System.out.println( "ADD:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    upres = client.commit(true, true);
    // System.out.println( "COMMIT:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    upres = client.optimize(true, true);
    // System.out.println( "OPTIMIZE:"+upres.getResponse() );
    assertEquals(0, upres.getStatus());

    query = new SolrQuery("*:*");
    query.addFacetQuery("price:[* TO 2]");
    query.addFacetQuery("price:[2 TO 4]");
    query.addFacetQuery("price:[5 TO *]");
    query.addFacetField("inStock");
    query.addFacetField("price");
    query.addFacetField("timestamp_dt");
    query.removeFilterQuery("inStock:true");

    response = client.query(query);
    assertEquals(0, response.getStatus());
    assertEquals(5, response.getResults().getNumFound());
    assertEquals(3, response.getFacetQuery().size());
    assertEquals(2, response.getFacetField("inStock").getValueCount());
    assertEquals(4, response.getFacetField("price").getValueCount());

    // test a second query, test making a copy of the main query
    SolrQuery query2 = query.getCopy();
    query2.addFilterQuery("inStock:true");
    response = client.query(query2);
    assertEquals(1, query2.getFilterQueries().length);
    assertEquals(0, response.getStatus());
    assertEquals(2, response.getResults().getNumFound());
    assertFalse(query.getFilterQueries() == query2.getFilterQueries());

    // sanity check round tripping of params...
    query = new SolrQuery("foo");
    query.addFilterQuery("{!field f=inStock}true");
    query.addFilterQuery("{!term f=name}hoss");
    query.addFacetQuery("price:[* TO 2]");
    query.addFacetQuery("price:[2 TO 4]");

    response = client.query(query);
    assertTrue("echoed params are not a NamedList: " + response.getResponseHeader().get("params").getClass(),
            response.getResponseHeader().get("params") instanceof NamedList);
    NamedList echo = (NamedList) response.getResponseHeader().get("params");
    List values = null;
    assertEquals("foo", echo.get("q"));
    assertTrue("echoed fq is not a List: " + echo.get("fq").getClass(), echo.get("fq") instanceof List);
    values = (List) echo.get("fq");
    assertEquals(2, values.size());
    assertEquals("{!field f=inStock}true", values.get(0));
    assertEquals("{!term f=name}hoss", values.get(1));
    assertTrue("echoed facet.query is not a List: " + echo.get("facet.query").getClass(),
            echo.get("facet.query") instanceof List);
    values = (List) echo.get("facet.query");
    assertEquals(2, values.size());
    assertEquals("price:[* TO 2]", values.get(0));
    assertEquals("price:[2 TO 4]", values.get(1));
}

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 .  j a  va2  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 {// w ww.ja  v a  2  s . 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.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 {/*ww w.  j  a v a 2 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.web.autocompletion.AutoCompleter.java

License:GNU General Public License

/**
 * Build the index from the database blob
 * @param os Objectstore/*  w w  w  .  j  a  va2 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

/**
 * 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  .jav  a2s.  c o  m
static void deleteAllFiles(SolrClient client) throws IOException, SolrServerException {

    if (null == client) {
        return;
    }

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