Java Code Examples for org.apache.solr.common.util.ContentStreamBase#StringStream

The following examples show how to use org.apache.solr.common.util.ContentStreamBase#StringStream . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractAlfrescoSolrIT.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a solr request.
 * @param params
 * @param json
 * @return
 */
public SolrServletRequest areq(ModifiableSolrParams params, String json) 
{
    if(params.get("wt" ) == null)
    {
        params.add("wt","xml");
    }
    SolrServletRequest req =  new SolrServletRequest(getCore(), null);
    req.setParams(params);
    if(json != null) 
    {
        ContentStream stream = new ContentStreamBase.StringStream(json);
        ArrayList<ContentStream> streams = new ArrayList<ContentStream>();
        streams.add(stream);
        req.setContentStreams(streams);
    }
    return req;
}
 
Example 2
Source File: SolrDao.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Set the given properties and values using the config API.
 * @param props properties to set.
 * @throws IOException network error.
 * @throws SolrServerException solr error.
 */
public void setProperties(Map<String, String> props) throws SolrServerException, IOException
{
   // Solrj does not support the config API yet.
   StringBuilder command = new StringBuilder("{\"set-property\": {");
   for (Map.Entry<String, String> entry: props.entrySet())
   {
      command.append('"').append(entry.getKey()).append('"').append(':');
      command.append(entry.getValue()).append(',');
   }
   command.setLength(command.length()-1); // remove last comma
   command.append("}}");

   GenericSolrRequest rq = new GenericSolrRequest(SolrRequest.METHOD.POST, "/config", null);
   ContentStream content = new ContentStreamBase.StringStream(command.toString());
   rq.setContentStreams(Collections.singleton(content));
   rq.process(solrClient);
}
 
Example 3
Source File: SolrDao.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Unset the given properties that have been previously set with {@link #setProperties(Map)}.
 * @param props properties to unset.
 * @throws IOException network error.
 * @throws SolrServerException solr error.
 */
public void unsetProperties(Set<String> props) throws SolrServerException, IOException
{
   // Solrj does not support the config API yet.
   StringBuilder command = new StringBuilder("{\"unset-property\": [");
   for (String prop: props)
   {
      command.append('"').append(prop).append('"').append(',');
   }
   command.setLength(command.length()-1); // remove last comma
   command.append("]}");

   GenericSolrRequest rq = new GenericSolrRequest(SolrRequest.METHOD.POST, "/config", null);
   ContentStream content = new ContentStreamBase.StringStream(command.toString());
   rq.setContentStreams(Collections.singleton(content));
   rq.process(solrClient);
}
 
Example 4
Source File: ClientUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Take a string and make it an iterable ContentStream
 */
public static Collection<ContentStream> toContentStreams( final String str, final String contentType )
{
  if( str == null )
    return null;

  ArrayList<ContentStream> streams = new ArrayList<>( 1 );
  ContentStreamBase ccc = new ContentStreamBase.StringStream( str );
  ccc.setContentType( contentType );
  streams.add( ccc );
  return streams;
}
 
Example 5
Source File: SolrCLI.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static NamedList<Object> postJsonToSolr(SolrClient solrClient, String updatePath, String jsonBody) throws Exception {
  ContentStreamBase.StringStream contentStream = new ContentStreamBase.StringStream(jsonBody);
  contentStream.setContentType(JSON_CONTENT_TYPE);
  ContentStreamUpdateRequest req = new ContentStreamUpdateRequest(updatePath);
  req.addContentStream(contentStream);
  return solrClient.request(req);
}
 
Example 6
Source File: DefaultJsonContentStreamMapper.java    From storm-solr with Apache License 2.0 5 votes vote down vote up
public ContentStream toContentStream(String docId, Object docObj) throws Exception {
  if (docObj instanceof String) {
    // already a string, so just stream it out directly
    ContentStreamBase.StringStream ss = new ContentStreamBase.StringStream((String)docObj);
    ss.setContentType(JSON_CONTENT_TYPE);
    return ss;
  }

  // pipe the bytes written by the ObjectMapper during JSON serialization to the InputStream
  PipedInputContentStream contentStream = new PipedInputContentStream(mapper, docObj);
  contentStream.setContentType(JSON_CONTENT_TYPE);
  Thread serThread = new Thread(contentStream);
  serThread.start(); // start pumping bytes from the JSON serializer into the input stream using a separate thread
  return contentStream;
}
 
Example 7
Source File: StreamlineSolrJsonMapper.java    From streamline with Apache License 2.0 5 votes vote down vote up
private SolrRequest<UpdateResponse> createSolrRequest(String json) {
    final ContentStreamUpdateRequest request = new ContentStreamUpdateRequest(jsonUpdateUrl);
    final ContentStream cs = new ContentStreamBase.StringStream(json, CONTENT_TYPE);
    request.addContentStream(cs);
    LOG.debug("Request generated with JSON: {}", json);
    return request;
}
 
Example 8
Source File: DocumentAnalysisRequestHandlerTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the {@link DocumentAnalysisRequestHandler#resolveAnalysisRequest(org.apache.solr.request.SolrQueryRequest)}
 */
@Test
public void testResolveAnalysisRequest() throws Exception {

  String docsInput =
          "<docs>" +
                  "<doc>" +
                  "<field name=\"id\">1</field>" +
                  "<field name=\"whitetok\">The Whitetok</field>" +
                  "<field name=\"text\">The Text</field>" +
                  "</doc>" +
                  "</docs>";

  final ContentStream cs = new ContentStreamBase.StringStream(docsInput);
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add("analysis.query", "The Query String");
  params.add("analysis.showmatch", "true");
  SolrQueryRequest req = new SolrQueryRequestBase(h.getCore(), params) {
    @Override
    public Iterable<ContentStream> getContentStreams() {
      return Collections.singleton(cs);
    }
  };

  DocumentAnalysisRequest request = handler.resolveAnalysisRequest(req);

  assertNotNull(request);
  assertTrue(request.isShowMatch());
  assertNotNull(request.getQuery());
  assertEquals("The Query String", request.getQuery());
  List<SolrInputDocument> documents = request.getDocuments();
  assertNotNull(documents);
  assertEquals(1, documents.size());
  SolrInputDocument document = documents.get(0);
  SolrInputField field = document.getField("id");
  assertNotNull(field);
  assertEquals("1", field.getFirstValue());
  field = document.getField("whitetok");
  assertNotNull(field);
  assertEquals("The Whitetok", field.getFirstValue());
  field = document.getField("text");
  assertNotNull(field);
  assertEquals("The Text", field.getFirstValue());

  req.close();
}