Example usage for org.apache.solr.client.solrj.request DirectXmlRequest DirectXmlRequest

List of usage examples for org.apache.solr.client.solrj.request DirectXmlRequest DirectXmlRequest

Introduction

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

Prototype

public DirectXmlRequest(String path, String body) 

Source Link

Usage

From source file:au.edu.usq.fascinator.access.solr.SolrAccessControl.java

License:Open Source License

private void removeFromIndex(String recordId, String role) throws Exception {
    String doc = writeDeleteString(recordId, role);
    core.request(new DirectXmlRequest("/update", doc));
    core.commit();/*from  w w  w .j  av a 2  s .c  om*/
}

From source file:au.edu.usq.fascinator.access.solr.SolrAccessControl.java

License:Open Source License

private void addToIndex(String recordId, String role) throws Exception {
    String doc = writeUpdateString(recordId, role);
    core.request(new DirectXmlRequest("/update", doc));
    core.commit();/*w  w w  .  j  a  v  a  2 s .  com*/
}

From source file:com.googlecode.fascinator.indexer.SolrIndexer.java

License:Open Source License

/**
 * Index a specific annotation/*w w  w . j a v  a 2s  . c om*/
 * 
 * @param object : The annotation's object
 * @param pid : The annotation payload
 * @throws IndexerException if there were errors during indexing
 */
private void annotate(DigitalObject object, Payload payload) throws IndexerException {
    String pid = payload.getId();
    if (propertiesId.equals(pid)) {
        return;
    }

    try {
        Properties props = new Properties();
        props.setProperty("metaPid", pid);

        String doc = index(object, payload, null, ANOTAR_RULES_OID, props);
        if (doc != null) {
            doc = "<add>" + doc + "</add>";
            anotar.request(new DirectXmlRequest("/update", doc));
            if (anotarAutoCommit) {
                anotar.commit();
            }
        }
    } catch (Exception e) {
        log.error("Indexing failed!\n-----\n", e);
    }
}

From source file:com.googlecode.fascinator.indexer.SolrWrapperQueueConsumer.java

License:Open Source License

/**
 * Submit all documents currently in the buffer to Solr, then purge
 * /*  w w w. ja v  a2s .  c  om*/
 */
private void submitBuffer(boolean forceCommit) {
    int size = docBuffer.size();
    if (size > 0) {
        // Debugging
        // String age = String.valueOf(
        // ((new Date().getTime()) - bufferOldest) / 1000);
        // String length = String.valueOf(bufferSize);
        // log.debug("Submitting buffer: " + size + " documents, " + length
        // +
        // " bytes, " + age + "s");
        log.debug("=== Submitting buffer: " + size + " documents");

        // Concatenate all documents in the buffer
        StringBuffer submissionBuffer = new StringBuffer();
        for (String doc : docBuffer.keySet()) {
            submissionBuffer.append(docBuffer.get(doc));
            // log.debug("DOC: {}", doc);
        }

        // Submit if the result is valid
        if (submissionBuffer.length() > 0) {
            // Wrap in the basic Solr 'add' node
            String submission = submissionBuffer.insert(0, "<add>").append("</add>").toString();
            // And submit
            try {
                solr.request(new DirectXmlRequest("/update", submission));
            } catch (Exception ex) {
                log.error("Error submitting documents to Solr!", ex);
            }
            // Commit if required
            if (autoCommit || forceCommit) {
                log.info("Running forced commit!");
                try {
                    // HTTP commits for embedded
                    if (commit != null) {
                        solr.commit();
                        commit.commit();
                        // or just HTTP on it's own
                    } else {
                        solr.commit();
                    }
                } catch (Exception e) {
                    log.warn("Solr forced commit failed. Document will"
                            + " not be visible until Solr autocommit fires." + " Error message: {}", e);
                }
            }
        }
    }
    purgeBuffer();
}

From source file:com.googlecode.fascinator.subscriber.solrEventLog.SolrEventLogSubscriber.java

License:Open Source License

/**
 * Submit all documents currently in the buffer to Solr, then purge
 *
 *///from  w  w  w.j av  a 2s.  c  o m
private void submitBuffer(boolean forceCommit) {
    int size = docBuffer.size();
    if (size > 0) {
        // Debugging
        //String age = String.valueOf(
        //        ((new Date().getTime()) - bufferOldest) / 1000);
        //String length = String.valueOf(bufferSize);
        //log.debug("Submitting buffer: " + size + " documents, " + length +
        //        " bytes, " + age + "s");
        log.debug("=== Submitting buffer: " + size + " documents");

        // Concatenate all documents in the buffer
        String submission = "";
        for (String doc : docBuffer) {
            submission += doc;
            //log.debug("DOC: {}", doc);
        }

        // Submit if the result is valid
        if (!submission.equals("")) {
            // Wrap in the basic Solr 'add' node
            submission = "<add>" + submission + "</add>";
            // And submit
            try {
                core.request(new DirectXmlRequest("/update", submission));
            } catch (Exception ex) {
                log.error("Error submitting documents to Solr!", ex);
            }
            // Commit if required
            if (forceCommit) {
                log.info("Running forced commit!");
                try {
                    core.commit();
                } catch (Exception e) {
                    log.warn("Solr forced commit failed. Document will"
                            + " not be visible until Solr autocommit fires." + " Error message: {}", e);
                }
            }
        }
    }
    purgeBuffer();
}

From source file:de.archeoinf.indexing.mods2solr.Mods2Solr.java

License:Open Source License

public void indexSolr(String project, boolean cleanIndex)
        throws SolrServerException, IOException, JDOMException {

    SearchEngineSolr.startRemoteSolr(projectProperties.getProperty("solr.host"),
            projectProperties.getProperty("solr.port"), projectProperties.getProperty("solr.base"), cleanIndex);
    SolrServer solrServer = SearchEngineSolr.getSolrServer();

    String[] entries = new File(this.projectProperties.getProperty("watchfolder") + File.separatorChar + project
            + File.separatorChar + this.projectProperties.getProperty(project + "_solr")).list();
    Arrays.sort(entries);/*from   ww w  .  ja v  a 2 s  . com*/
    logger.info("Files in " + this.projectProperties.getProperty("watchfolder") + File.separatorChar + project
            + File.separatorChar + this.projectProperties.getProperty(project + "_solr"));
    logger.info(Arrays.toString(entries));

    for (String entry : entries) {
        Document doc = new SAXBuilder().build(this.projectProperties.getProperty("watchfolder")
                + File.separatorChar + project + File.separatorChar
                + this.projectProperties.getProperty(project + "_solr") + File.separatorChar + entry);

        Format format = Format.getPrettyFormat();
        format.setEncoding("UTF-8");
        XMLOutputter xml = new XMLOutputter(format);

        DirectXmlRequest xmlreq = new DirectXmlRequest("/update", xml.outputString(doc));

        solrServer.request(xmlreq);
    }

    solrServer.commit();
}

From source file:de.hybris.platform.solrfacetsearch.embedded.SolrSearchQueryTest.java

License:Open Source License

@Test
public void testSearchQuery() throws Exception {

    //create a file reader to get the string in the xml file
    final String xmlFile = readXmlFile("/test/TestFacetSearchQuery.xml");
    final DirectXmlRequest xmlRequest = new DirectXmlRequest("/update", xmlFile);
    server.request(xmlRequest);//from  ww w .ja va  2  s .co m
    server.commit();

    //test number of all imported products
    final SearchQuery query = new SearchQuery(facetSearchConfig, indexedType);

    String language = "de";
    query.setLanguage(language);
    final String currency = "eur";
    query.setCurrency(currency);

    assertEquals(10, server.query(converter.convertSolrQuery(query)).getResults().getNumFound());

    //test without field
    query.search("maxtor");
    LOG.debug(server.query(converter.convertSolrQuery(query)));
    assertEquals(2, server.query(converter.convertSolrQuery(query)).getResults().getNumFound());
    query.clearAllFields();

    //test single field
    final String FIELD_NAME = "name";
    final String FIELD_CATEGORY = "category";
    query.addFacetValue(FIELD_NAME, "Dell");
    assertEquals(1, server.query(converter.convertSolrQuery(query)).getResults().getNumFound());
    query.clearAllFields();
    query.addFacetValue(FIELD_NAME, "maxtor");

    assertEquals(2, server.query(converter.convertSolrQuery(query)).getResults().getNumFound());
    query.clearAllFields();
    query.addFacetValue(FIELD_NAME, "notFound");

    assertEquals(0, server.query(converter.convertSolrQuery(query)).getResults().getNumFound());

    //test multifields
    language = "en";
    query.setLanguage(language);
    query.clearAllFields();
    query.addFacetValue(FIELD_CATEGORY, "camera");
    assertEquals(3, server.query(converter.convertSolrQuery(query)).getResults().getNumFound());
    query.addFacetValue(FIELD_NAME, "sony");
    assertEquals(2, server.query(converter.convertSolrQuery(query)).getResults().getNumFound());

    //test remove field with value
    query.removeFacetValue(FIELD_NAME, "sony");
    assertEquals(3, server.query(converter.convertSolrQuery(query)).getResults().getNumFound());
    //test remove field
    query.removeField("category");
    assertEquals(10, server.query(converter.convertSolrQuery(query)).getResults().getNumFound());
}

From source file:de.hybris.platform.solrfacetsearch.embedded.SolrSearchQueryTest.java

License:Open Source License

@Test
public void testSearchQueryWithPreProcessors() throws Exception {

    //create a file reader to get the string in the xml file
    final String xmlFile = readXmlFile("/test/TestFacetSearchQuery.xml");
    final DirectXmlRequest xmlRequest = new DirectXmlRequest("/update", xmlFile);
    server.request(xmlRequest);//  w  w w .ja  va 2  s  . c  om
    server.commit();

    //test number of all imported products
    final FacetSearchConfig config = facetSearchConfigService.getConfiguration(localConfig.getName());
    final SearchQuery query = new SearchQuery(config, indexedType);

    final String language = "de";
    query.setLanguage(language);
    final String currency = "eur";
    query.setCurrency(currency);

    final DefaultSolrQueryConverter converter = new DefaultSolrQueryConverter();
    converter.setSolrFieldNameProvider(solrFieldNameProvider);
    converter.setQueryPostProcessors(Collections.<SolrQueryPostProcessor>list(new SolrQueryPostProcessor() {

        @Override
        public SolrQuery process(final SolrQuery query, final SearchQuery solrSearchQuery) {
            query.setSortField("id", ORDER.asc);
            query.setStart(Integer.valueOf(0));
            query.setRows(Integer.valueOf(10));
            return query;
        }
    }));
    Object prev = null;
    SolrDocumentList results = server.query(converter.convertSolrQuery(query)).getResults();
    assertEquals("Result size limited in PostProcessor", 10, results.size());
    for (final SolrDocument doc : results) {
        final Object fieldValue = doc.getFieldValue("id");
        if (prev != null) {
            assertTrue(prev instanceof Comparable);
            assertTrue(((Comparable) prev).compareTo(fieldValue) < 0);
        }
        prev = fieldValue;
    }
    converter.setQueryPostProcessors(Collections.<SolrQueryPostProcessor>list(new SolrQueryPostProcessor() {

        @Override
        public SolrQuery process(final SolrQuery query, final SearchQuery solrSearchQuery) {
            query.setSortField("id", ORDER.desc);
            query.setStart(Integer.valueOf(2));
            query.setRows(Integer.valueOf(10));
            return query;
        }
    }));
    results = server.query(converter.convertSolrQuery(query)).getResults();
    assertEquals("Result size limited in PostProcessor", 8, results.size());
    for (final SolrDocument doc : results) {
        final Object fieldValue = doc.getFieldValue("id");
        if (prev != null) {
            assertTrue(prev instanceof Comparable);
            assertTrue(((Comparable) prev).compareTo(fieldValue) > 0);
        }
        prev = fieldValue;
    }
    converter.setQueryPostProcessors(Collections.<SolrQueryPostProcessor>list(new SolrQueryPostProcessor() {
        @Override
        public SolrQuery process(final SolrQuery query, final SearchQuery solrSearchQuery) {
            query.setSortField("id", ORDER.asc);
            return query;
        }
    }, new SolrQueryPostProcessor() {

        @Override
        public SolrQuery process(final SolrQuery query, final SearchQuery solrSearchQuery) {
            query.setStart(Integer.valueOf(3));
            return query;
        }
    }, new SolrQueryPostProcessor() {

        @Override
        public SolrQuery process(final SolrQuery query, final SearchQuery solrSearchQuery) {
            query.setRows(Integer.valueOf(6));
            return query;
        }
    }));
    results = server.query(converter.convertSolrQuery(query)).getResults();
    assertEquals("Result size limited in PostProcessor", 6, results.size());
    for (final SolrDocument doc : results) {
        final Object fieldValue = doc.getFieldValue("id");
        if (prev != null) {
            assertTrue(prev instanceof Comparable);
            assertTrue(((Comparable) prev).compareTo(fieldValue) <= 0);
        }
        prev = fieldValue;
    }
}

From source file:de.hybris.platform.solrfacetsearch.integration.SolrConfigurationServiceStandaloneTest.java

License:Open Source License

@Test
public void testUpdateSynonyms()
        throws SolrServerException, IOException, FacetSearchException, IndexUpdateException {
    final String xmlFile = readXmlFile("/test/SolrConfigurationServiceEmbeddedTest.xml");
    final DirectXmlRequest xmlRequest = new DirectXmlRequest("/update", xmlFile);
    solrServer.request(xmlRequest);/*from w  w  w  .j  ava 2s. c  o  m*/
    solrServer.commit();

    final SearchQuery query = new SearchQuery(facetSearchConfig, indexedType);

    query.setLanguage("en");
    query.setCurrency("eur");

    SolrDocumentList result = solrServer.query(converter.convertSolrQuery(query)).getResults();
    assertTrue("Result set should not be empty", result.size() > 0);

    query.search("bank");
    result = solrServer.query(converter.convertSolrQuery(query)).getResults();
    assertEquals("One bank exists for English", 1, result.size());

    query.search("bank");
    query.setLanguage("de");

    result = solrServer.query(converter.convertSolrQuery(query)).getResults();
    assertEquals("One hit expected for 'bank' in German", 1, result.size());

    solrConfigurationService.updateSynonyms(solrServer,
            new LanguageSynonymMappings().addMapping("de", "bank => sparkasse, bank"));

    result = solrServer.query(converter.convertSolrQuery(query)).getResults();
    assertEquals("Two hits for 'bank' expected for German since we have defined synonyms", 2, result.size());

    query.clearAllFields();
    query.setLanguage("en");
    query.setCurrency("eur");
    query.search("usb");

    result = solrServer.query(converter.convertSolrQuery(query)).getResults();
    assertEquals("One usb exists for English", 1, result.size());

    query.clearAllFields();
    query.setLanguage("en");
    query.setCurrency("eur");
    query.search("ueesbee");
    result = solrServer.query(converter.convertSolrQuery(query)).getResults();
    assertEquals("Zero ueesbee exists for English", 0, result.size());

    solrConfigurationService.updateSynonyms(solrServer,
            new LanguageSynonymMappings().addMapping("en", "ueesbee, usb"));

    result = solrServer.query(converter.convertSolrQuery(query)).getResults();
    assertEquals("One ueesbee exists for English", 1, result.size());

    query.clearAllFields();
    query.setLanguage("de");
    query.setCurrency("eur");
    query.search("ueesbee");

    result = solrServer.query(converter.convertSolrQuery(query)).getResults();
    assertEquals("Zero ueesbee exists for German", 0, result.size());

    solrConfigurationService.updateSynonyms(solrServer,
            new LanguageSynonymMappings().addMapping("de", "ueesbee, usb"));

    result = solrServer.query(converter.convertSolrQuery(query)).getResults();
    assertEquals("One ueesbee exists for German", 1, result.size());
}

From source file:de.hybris.platform.solrfacetsearch.solr.SolrConfigurationServiceEmbeddedTest.java

License:Open Source License

@Override
@Before//  w ww.j a  v  a2 s  . c  o m
public void setUp() throws Exception {
    super.setUp();
    solrServer = getSolrService().getSolrServerMaster(solrConfig, indexedType);

    solrServer.deleteByQuery("*:*");
    solrServer.commit();

    final String xmlFile = readXmlFile("/test/SolrConfigurationServiceEmbeddedTest.xml");
    final DirectXmlRequest xmlRequest = new DirectXmlRequest("/update", xmlFile);
    solrServer.request(xmlRequest);
    solrServer.commit();
}