Java Code Examples for org.apache.solr.common.SolrInputDocument

The following examples show how to use org.apache.solr.common.SolrInputDocument. These examples are extracted from open source projects. 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 Project: BioSolr   Source File: TextIndexSolr5.java    License: Apache License 2.0 6 votes vote down vote up
private SolrInputDocument solrDoc(Entity entity)
{
	SolrInputDocument doc = new SolrInputDocument() ;
	doc.addField(docDef.getEntityField(), entity.getId()) ;

	String graphField = docDef.getGraphField() ;
	if ( graphField != null ) {
		doc.addField(graphField, entity.getGraph()) ;
	}

	// the addition needs to be done as a partial update
	// otherwise, if we have multiple fields, each successive
	// addition will replace the previous one and we are left
	// with only the last field indexed.
	// see
	// http://stackoverflow.com/questions/12183798/solrj-api-for-partial-document-update
	// and
	// https://svn.apache.org/repos/asf/lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java
	HashMap<String, Object> map = new HashMap<>() ;
	for ( Map.Entry<String, Object> e : entity.getMap().entrySet() ) {
		map.put("add", e.getValue()) ;
		doc.addField(e.getKey(), map) ;
	}
	return doc ;
}
 
Example 2
public void testFailedParseMixedBoolean() throws Exception {
  IndexSchema schema = h.getCore().getLatestSchema();
  assertNull(schema.getFieldOrNull("not_in_schema"));
  Map<Object,Object> mixed = new HashMap<>();
  Long longVal = 294423L;
  mixed.put(true, "true");
  mixed.put(longVal, longVal); // Float-typed field value
  mixed.put(false, "false");
  mixed.put(true, "true");
  SolrInputDocument d = processAdd("parse-boolean-no-run-processor",
                                   doc(f("id", "7207"), f("not_in_schema", mixed.values())));
  assertNotNull(d);
  boolean foundLong = false;
  for (Object o : d.getFieldValues("not_in_schema")) {
    if (longVal == o) {
      foundLong = true;
    } else {
      assertThat(o, IS_STRING);
    }
    mixed.values().remove(o);
  }
  assertTrue(foundLong);
  assertTrue(mixed.isEmpty());
}
 
Example 3
public void testParseAlternateValueBooleans() throws Exception {
  IndexSchema schema = h.getCore().getLatestSchema();
  assertNotNull(schema.getFieldOrNull("boolean1_b")); // should match dynamic field "*_b"
  assertNotNull(schema.getFieldOrNull("boolean2_b")); // should match dynamic field "*_b"
  assertNotNull(schema.getFieldOrNull("boolean3_b")); // should match dynamic field "*_b"
  assertNotNull(schema.getFieldOrNull("boolean4_b")); // should match dynamic field "*_b"
  assertNotNull(schema.getFieldOrNull("boolean5_b")); // should match dynamic field "*_b"
  assertNull(schema.getFieldOrNull("not_in_schema"));
  boolean[] values      = { true, true, true, false, false, false };
  String[] stringValues = { "on", "yes", "True", "Off", "no", "FALSE" };
  String[] fieldNames   = { "boolean1_b", "boolean2_b", "boolean3_b", "boolean4_b", "boolean5_b", "not_in_schema" };
  SolrInputDocument d = doc(f("id", "55"));
  for (int i = 0 ; i < values.length ; ++i) {
    d.addField(fieldNames[i], stringValues[i]);
  }
  d = processAdd("parse-boolean-alternate-values-no-run-processor", d);
  assertNotNull(d);

  for (int i = 0 ; i < values.length ; ++i) {
    assertThat(d.getFieldValue(fieldNames[i]), IS_BOOLEAN);
    assertEquals(values[i], d.getFieldValue(fieldNames[i]));
  }
}
 
Example 4
Source Project: lucene-solr   Source File: TestTlogReplayVsRecovery.java    License: Apache License 2.0 6 votes vote down vote up
/** 
 * Adds the specified number of docs directly to the leader, 
 * using increasing docIds begining with startId.  Commits if and only if the boolean is true.
 */
private void addDocs(final boolean commit, final int numDocs, final int startId) throws SolrServerException, IOException {

  List<SolrInputDocument> docs = new ArrayList<>(numDocs);
  for (int i = 0; i < numDocs; i++) {
    int id = startId + i;
    docs.add(new SolrInputDocument("id", String.valueOf(id), "fieldName_s", String.valueOf(id)));
  }
  // For simplicity, we always add out docs directly to NODE0
  // (where the leader should be) and bypass the proxy...
  try (HttpSolrClient client = getHttpSolrClient(NODE0.getBaseUrl().toString())) {
    assertEquals(0, client.add(COLLECTION, docs).getStatus());
    if (commit) {
      assertEquals(0, client.commit(COLLECTION).getStatus());
    }
  }
}
 
Example 5
Source Project: vind   Source File: SolrSearchServerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws IOException, SolrServerException {
    MockitoAnnotations.initMocks(this);
    when(solrClient.ping()).thenReturn(solrPingResponse);
    when(solrPingResponse.getStatus()).thenReturn(0);
    when(solrPingResponse.getQTime()).thenReturn(10);


    when(solrClient.query(any(), any(SolrRequest.METHOD.class))).thenReturn(response);
    when(response.getResults()).thenReturn(new SolrDocumentList());
    when(response.getResults()).thenReturn(new SolrDocumentList());

    when(solrClient.add(org.mockito.Matchers.<Collection<SolrInputDocument>>any())).thenReturn(iResponse);
    when(solrClient.add(any(SolrInputDocument.class))).thenReturn(iResponse);
    when(iResponse.getQTime()).thenReturn(10);
    when(iResponse.getElapsedTime()).thenReturn(15l);

    //we use the protected constructor to avoid schema checking
    server = new SolrSearchServer(solrClient, false);
}
 
Example 6
Source Project: mycore   Source File: MCRSolrAltoExtractor.java    License: GNU General Public License v3.0 6 votes vote down vote up
private void extract(Element root, SolrInputDocument document) {
    StringBuilder altoContent = new StringBuilder();
    for (Element stringElement : STRING_EXP.evaluate(root)) {
        String content = stringElement.getAttributeValue("CONTENT");
        String hpos = stringElement.getAttributeValue("HPOS");
        String vpos = stringElement.getAttributeValue("VPOS");
        String width = stringElement.getAttributeValue("WIDTH");
        String height = stringElement.getAttributeValue("HEIGHT");
        if (hpos == null || vpos == null || width == null || height == null) {
            continue;
        }
        String regEx = "\\.0";
        String altoWord = String.join("|", content, hpos.replaceAll(regEx, ""), vpos.replaceAll(regEx, ""),
            width.replaceAll(regEx, ""), height.replaceAll(regEx, ""));
        altoContent.append(content).append(' ');
        document.addField("alto_words", altoWord);
    }
    document.addField("alto_content", altoContent.toString().trim());
}
 
Example 7
Source Project: lucene-solr   Source File: TestSolrJErrorHandling.java    License: Apache License 2.0 6 votes vote down vote up
Iterator<SolrInputDocument> manyDocs(final int base, final int numDocs) {
  return new Iterator<SolrInputDocument>() {
    int count = 0;
    @Override
    public boolean hasNext() {
      return count < numDocs;
    }

    @Override
    public SolrInputDocument next() {
      int id = base + count++;
      if (count == 1) {  // first doc is legit, and will increment a counter
        return sdoc("id","test", "count_i", map("inc",1));
      }
      // include "ignore_exception" so the log doesn't fill up with known exceptions, and change the values for each doc
      // so binary format won't compress too much
      return sdoc("id",Integer.toString(id),"ignore_exception_field_does_not_exist_"+id,"fieldval"+id);
    }

    @Override
    public void remove() {
    }
  };
}
 
Example 8
Source Project: lucene-solr   Source File: SolrExampleTests.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * recursive method for generating a document, which may also have child documents;
 * adds all documents constructed (including decendents) to allDocs via their id 
 */
private SolrInputDocument genNestedDocuments(Map<String,SolrInputDocument> allDocs, 
                                             int thisLevel,
                                             int maxDepth) {
  String id = "" + (idCounter++);
  SolrInputDocument sdoc = new SolrInputDocument();
  allDocs.put(id, sdoc);

  sdoc.addField("id", id);
  sdoc.addField("level_i", thisLevel);
  sdoc.addField("name", names[TestUtil.nextInt(random(), 0, names.length-1)]);
  
  if (0 < maxDepth) {
    // NOTE: range include negative to increase odds of no kids
    int numKids = TestUtil.nextInt(random(), -2, 7);
    for(int i=0; i<numKids; i++) {
      sdoc.addChildDocument(genNestedDocuments(allDocs, thisLevel+1, maxDepth-1));
    }
  }
  return sdoc;
}
 
Example 9
Source Project: lucene-solr   Source File: CdcrBootstrapTest.java    License: Apache License 2.0 6 votes vote down vote up
private int indexDocs(CloudSolrClient sourceSolrClient, String collection, int batches) throws IOException, SolrServerException {
  sourceSolrClient.setDefaultCollection(collection);
  int numDocs = 0;
  for (int k = 0; k < batches; k++) {
    UpdateRequest req = new UpdateRequest();
    for (; numDocs < (k + 1) * 100; numDocs++) {
      SolrInputDocument doc = new SolrInputDocument();
      doc.addField("id", "source_" + numDocs);
      doc.addField("xyz", numDocs);
      req.add(doc);
    }
    req.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    req.process(sourceSolrClient);
  }
  log.info("Adding numDocs={}", numDocs);
  return numDocs;
}
 
Example 10
public void testParseBooleanRoundTrip() throws Exception {
  IndexSchema schema = h.getCore().getLatestSchema();
  assertNotNull(schema.getFieldOrNull("boolean1_b")); // should match dynamic field "*_b"
  assertNotNull(schema.getFieldOrNull("boolean2_b")); // should match dynamic field "*_b"
  boolean value1 = true;
  boolean value2 = false;
  SolrInputDocument d = processAdd("parse-boolean",
      doc(f("id", "141"), f("boolean1_b", value1), f("boolean2_b", value2)));
  assertNotNull(d);
  assertThat(d.getFieldValue("boolean1_b"), IS_BOOLEAN);
  assertEquals(value1, d.getFieldValue("boolean1_b"));
  assertThat(d.getFieldValue("boolean2_b"), IS_BOOLEAN);
  assertEquals(value2, d.getFieldValue("boolean2_b"));

  assertU(commit());
  assertQ(req("id:141")
      ,"//bool[@name='boolean1_b'][.='" + value1 + "']"
      ,"//bool[@name='boolean2_b'][.='" + value2 + "']");
}
 
Example 11
public void testMixedFloats() throws Exception {
  IndexSchema schema = h.getCore().getLatestSchema();
  assertNotNull(schema.getFieldOrNull("float_tf")); // should match dynamic field "*_tf"
  Map<Float,Object> mixedFloats = new HashMap<>();
  mixedFloats.put(85.0f, "85");
  mixedFloats.put(2894518.0f, "2,894,518");
  mixedFloats.put(2.94423E-9f, 2.94423E-9f); // Float-typed field value
  mixedFloats.put(48794721.937f, "48,794,721.937");
  SolrInputDocument d = processAdd("parse-float-no-run-processor", 
                                   doc(f("id", "342"), f("float_tf", mixedFloats.values())));
  assertNotNull(d);
  for (Object o : d.getFieldValues("float_tf")) {
    assertThat(o, IS_FLOAT);
    mixedFloats.remove(o);
  }
  assertTrue(mixedFloats.isEmpty());
}
 
Example 12
public void testTrimRegex() throws Exception {
  SolrInputDocument d = null;
  d = processAdd("trim-field-regexes", 
                 doc(f("id", "1111"),
                     f("foo_t", " string1 "),
                     f("foozat_s", " string2 "),
                     f("bar_t", " string3 "),
                     f("bar_s", " string4 ")));

  assertNotNull(d);

  assertEquals("string1", d.getFieldValue("foo_t"));
  assertEquals("string2", d.getFieldValue("foozat_s"));
  assertEquals(" string3 ", d.getFieldValue("bar_t"));
  assertEquals("string4", d.getFieldValue("bar_s"));

}
 
Example 13
Source Project: titan1withtp3.1   Source File: SolrIndex.java    License: Apache License 2.0 6 votes vote down vote up
private void deleteIndividualFieldsFromIndex(String collectionName, String keyIdField, String docId, HashSet<IndexEntry> fieldDeletions) throws SolrServerException, IOException {
    if (fieldDeletions.isEmpty()) return;

    Map<String, String> fieldDeletes = new HashMap<String, String>(1) {{ put("set", null); }};

    SolrInputDocument doc = new SolrInputDocument();
    doc.addField(keyIdField, docId);
    StringBuilder sb = new StringBuilder();
    for (IndexEntry fieldToDelete : fieldDeletions) {
        doc.addField(fieldToDelete.field, fieldDeletes);
        sb.append(fieldToDelete).append(",");
    }

    if (logger.isTraceEnabled())
        logger.trace("Deleting individual fields [{}] for document {}", sb.toString(), docId);

    UpdateRequest singleDocument = newUpdateRequest();
    singleDocument.add(doc);
    solrClient.request(singleDocument, collectionName);
}
 
Example 14
public static SolrInputDocument getSolrInputDocument(Element input) {
    SolrInputDocument doc = new SolrInputDocument();
    HashSet<MCRSolrInputField> duplicateFilter = new HashSet<>();
    List<Element> fieldElements = input.getChildren("field");
    for (Element fieldElement : fieldElements) {
        MCRSolrInputField field = new MCRSolrInputField();
        field.setName(fieldElement.getAttributeValue("name"));
        field.setValue(fieldElement.getText());
        if (field.getValue().isEmpty() || duplicateFilter.contains(field)) {
            continue;
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("adding {}={}", field.getName(), field.getValue());
        }
        duplicateFilter.add(field);
        doc.addField(field.getName(), field.getValue());
    }
    List<Element> docElements = input.getChildren("doc");
    for (Element child : docElements) {
        SolrInputDocument solrChild = getSolrInputDocument(child);
        doc.addChildDocument(solrChild);
    }
    return doc;
}
 
Example 15
Source Project: lucene-solr   Source File: JsonLoaderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testIntegerValuesInAdd() throws Exception {
  String str = "{'add':[{'id':'1','i1':256,'i2':-5123456789,'i3':[0,1]}]}".replace('\'', '"');
  SolrQueryRequest req = req();
  SolrQueryResponse rsp = new SolrQueryResponse();
  BufferingRequestProcessor p = new BufferingRequestProcessor(null);
  JsonLoader loader = new JsonLoader();
  loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);

  assertEquals(1, p.addCommands.size());

  AddUpdateCommand add = p.addCommands.get(0);
  SolrInputDocument d = add.solrDoc;
  SolrInputField f = d.getField("i1");
  assertEquals(256L, f.getValue());
  f = d.getField("i2");
  assertEquals(-5123456789L, f.getValue());
  f = d.getField("i3");
  assertEquals(2, ((List)f.getValue()).size());
  assertEquals(0L, ((List)f.getValue()).get(0));
  assertEquals(1L, ((List)f.getValue()).get(1));

  req.close();
}
 
Example 16
@Test
public void testDeleteWithXml() throws IOException, SolrServerException {
    final SolrClient solrClient = createEmbeddedSolrClient(DEFAULT_SOLR_CORE);
    final TestableProcessor proc = new TestableProcessor(solrClient);

    final TestRunner runner = createDefaultTestRunner(proc);
    runner.setProperty(PutSolrContentStream.CONTENT_STREAM_PATH, "/update");
    runner.setProperty(PutSolrContentStream.CONTENT_TYPE, "application/xml");
    runner.setProperty("commit", "true");

    // add a document so there is something to delete
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("first", "bob");
    doc.addField("last", "smith");
    doc.addField("created", new Date());

    solrClient.add(doc);
    solrClient.commit();

    // prove the document got added
    SolrQuery query = new SolrQuery("*:*");
    QueryResponse qResponse = solrClient.query(query);
    Assert.assertEquals(1, qResponse.getResults().getNumFound());

    // run the processor with a delete-by-query command
    runner.enqueue("<delete><query>first:bob</query></delete>".getBytes("UTF-8"));
    runner.run(1, false);

    // prove the document got deleted
    qResponse = solrClient.query(query);
    Assert.assertEquals(0, qResponse.getResults().getNumFound());
}
 
Example 17
Source Project: lucene-solr   Source File: TestRandomFlRTGCloud.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the docIds specified and asserts the results are valid, updateing knownDocs accordingly
 */
private void assertDelete(final SolrInputDocument[] knownDocs, final int[] docIds) throws IOException, SolrServerException {
  List<String> ids = new ArrayList<>(docIds.length);
  for (final int docId : docIds) {
    ids.add("" + docId);
    knownDocs[docId] = null;
  }
  assertEquals("Failed delete: " + docIds, 0, getRandClient(random()).deleteById(ids).getStatus());
}
 
Example 18
private void addDocs(String collection, int numDocs, boolean commit) throws SolrServerException, IOException {
  for (int docId = 1; docId <= numDocs; docId++) {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", docId);
    doc.addField("text", "shard" + docId % 5);
    cloudClient.setDefaultCollection(collection);
    cloudClient.add(doc);
  }
  if (commit) {
    cloudClient.commit();
  }
}
 
Example 19
private void retryAddsIndividually(int shard, Collection<SolrInputDocument> inputDocuments) throws SolrServerException,
        IOException {
    for (SolrInputDocument inputDocument : inputDocuments) {
        try {
            solrServers.get(shard).add(inputDocument);
            indexAddMeter.mark();
        } catch (SolrException e) {
            logOrThrowSolrException(e);
            // No exception thrown through, so we can update the metric
            documentAddErrorMeter.mark();
        }
    }
}
 
Example 20
Source Project: metron   Source File: SolrUpdateDaoTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void updateShouldProperlyUpdateDocumentExplicitIndex() throws Exception {
  Document document = new Document(new HashMap<String, Object>(){{
    put("field", "value");
  }}, "guid", "bro", 0L);

  SolrInputDocument solrInputDocument = new SolrInputDocument();
  solrInputDocument.addField("field", "value");

  solrUpdateDao.update(document, Optional.of("bro"));

  verify(client).add(eq("bro"), argThat(new SolrInputDocumentMatcher(solrInputDocument)));
}
 
Example 21
/**
 * Instantiate with the underlying writer to delegate to, and the size of the internal buffer to use.
 *
 * @param delegateWriter underlying writer to delegate writes and deletes to
 * @param bufferSize size of the internal write buffer to use
 * @param documentOutputCounter Hadoop counter for recording the number of Solr documents output
 * @param documentBatchOutputCounter Hadoop counter for recording the number of document batches output
 */
public BufferedSolrInputDocumentWriter(SolrInputDocumentWriter delegateWriter, int bufferSize,
        Counter documentOutputCounter, Counter documentBatchOutputCounter) {
    this.delegateWriter = delegateWriter;
    this.bufferSize = bufferSize;
    this.adds = 0;
    this.writeBuffers = CacheBuilder.newBuilder().build(new CacheLoader<Integer, Map<String, SolrInputDocument>>() {
        @Override
        public Map<String, SolrInputDocument> load(Integer key) throws Exception {
            return new HashMap(BufferedSolrInputDocumentWriter.this.bufferSize);
        }
    });
    this.docOutputCounter = documentOutputCounter;
    this.docBatchCounter = documentBatchOutputCounter;
}
 
Example 22
Source Project: lucene-solr   Source File: TestCollectionAPI.java    License: Apache License 2.0 5 votes vote down vote up
private void clusterStatusWithRouteKey() throws IOException, SolrServerException {
  try (CloudSolrClient client = createCloudClient(DEFAULT_COLLECTION)) {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", "a!123"); // goes to shard2. see ShardRoutingTest for details
    client.add(doc);
    client.commit();

    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("action", CollectionParams.CollectionAction.CLUSTERSTATUS.toString());
    params.set("collection", DEFAULT_COLLECTION);
    params.set(ShardParams._ROUTE_, "a!");
    @SuppressWarnings({"rawtypes"})
    SolrRequest request = new QueryRequest(params);
    request.setPath("/admin/collections");

    NamedList<Object> rsp = client.request(request);
    @SuppressWarnings({"unchecked"})
    NamedList<Object> cluster = (NamedList<Object>) rsp.get("cluster");
    assertNotNull("Cluster state should not be null", cluster);
    @SuppressWarnings({"unchecked"})
    NamedList<Object> collections = (NamedList<Object>) cluster.get("collections");
    assertNotNull("Collections should not be null in cluster state", collections);
    assertNotNull(collections.get(DEFAULT_COLLECTION));
    assertEquals(1, collections.size());
    @SuppressWarnings({"unchecked"})
    Map<String, Object> collection = (Map<String, Object>) collections.get(DEFAULT_COLLECTION);
    assertEquals("conf1", collection.get("configName"));
    @SuppressWarnings({"unchecked"})
    Map<String, Object> shardStatus = (Map<String, Object>) collection.get("shards");
    assertEquals(1, shardStatus.size());
    @SuppressWarnings({"unchecked"})
    Map<String, Object> selectedShardStatus = (Map<String, Object>) shardStatus.get(SHARD2);
    assertNotNull(selectedShardStatus);
  }
}
 
Example 23
Source Project: lucene-solr   Source File: TestDynamicURP.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testUrp() throws Exception {

  ByteBuffer jar = getFileContent("runtimecode/runtimeurp.jar.bin");

  String blobName = "urptest";
  TestBlobHandler.postAndCheck(cluster.getSolrClient(), cluster.getRandomJetty(random()).getBaseUrl().toString(),
      blobName, jar, 1);

  new V2Request.Builder("/c/" + COLLECTION + "/config")
      .withPayload(singletonMap("add-runtimelib", (MapWriter) ew1 -> ew1
          .put("name", blobName)
          .put("version", "1")))
      .withMethod(POST)
      .build()
      .process(cluster.getSolrClient());
  TestSolrConfigHandler.testForResponseElement(null,
      cluster.getRandomJetty(random()).getBaseUrl().toString(),
      "/"+COLLECTION+"/config/overlay",
      cluster.getSolrClient(),
      Arrays.asList("overlay", "runtimeLib", blobName, "version")
      ,"1",10);

  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", "123");
  doc.addField("name_s", "Test URP");
  new UpdateRequest()
      .add(doc)
      .commit(cluster.getSolrClient(), COLLECTION);
  QueryResponse result = cluster.getSolrClient().query(COLLECTION, new SolrQuery("id:123"));
  assertEquals(1, result.getResults().getNumFound());
  Object time_s = result.getResults().get(0).getFirstValue("time_s");
  assertNotNull(time_s);



}
 
Example 24
Source Project: lucene-solr   Source File: JsonLoaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyAnonymousChildDocs() throws Exception {
  String str = "{\n" +
      "    \"add\": {\n" +
      "        \"doc\": {\n" +
      "            \"id\": \"1\",\n" +
      "            \"_childDocuments_\": []\n" +
      "        }\n" +
      "    }\n" +
      "}";
  SolrQueryRequest req = req("commit","true");
  SolrQueryResponse rsp = new SolrQueryResponse();
  BufferingRequestProcessor p = new BufferingRequestProcessor(null);
  JsonLoader loader = new JsonLoader();
  loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);

  assertEquals( 1, p.addCommands.size() );

  AddUpdateCommand add = p.addCommands.get(0);
  SolrInputDocument d = add.solrDoc;
  SolrInputField f = d.getField( "id" );
  assertEquals("1", f.getValue());
  List<SolrInputDocument> cd = d.getChildDocuments();
  assertNull(cd);

  req.close();
}
 
Example 25
Source Project: storm-solr   Source File: NestedDocumentMapper.java    License: Apache License 2.0 5 votes vote down vote up
protected void addFieldsToDoc(String blockId, SolrInputDocument doc, Map map) {
  for (Object key : map.keySet()) {
    Object val = map.get(key);
    if (val != null)
      addFieldToDoc(blockId, doc, key.toString(), val);
  }
}
 
Example 26
Source Project: lucene-solr   Source File: SystemLogListener.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes"})
private void addOperations(SolrInputDocument doc, List<SolrRequest> operations) {
  if (operations == null || operations.isEmpty()) {
    return;
  }
  Set<String> collections = new HashSet<>();
  for (SolrRequest req : operations) {
    SolrParams params = req.getParams();
    if (params == null) {
      continue;
    }
    if (params.get(CollectionAdminParams.COLLECTION) != null) {
      collections.add(params.get(CollectionAdminParams.COLLECTION));
    }
    // build a whitespace-separated param string
    StringJoiner paramJoiner = new StringJoiner(" ");
    paramJoiner.setEmptyValue("");
    for (Iterator<String> it = params.getParameterNamesIterator(); it.hasNext(); ) {
      final String name = it.next();
      final String [] values = params.getParams(name);
      for (String value : values) {
        paramJoiner.add(name + "=" + value);
      }
    }
    String paramString = paramJoiner.toString();
    if (!paramString.isEmpty()) {
      doc.addField("operations.params_ts", paramString);
    }
  }
  if (!collections.isEmpty()) {
    doc.addField(COLLECTIONS_FIELD, collections);
  }
}
 
Example 27
Source Project: lucene-solr   Source File: AtomicUpdateDocumentMerger.java    License: Apache License 2.0 5 votes vote down vote up
protected void doInc(SolrInputDocument toDoc, SolrInputField sif, Object fieldVal) {
  SolrInputField numericField = toDoc.get(sif.getName());
  SchemaField sf = schema.getField(sif.getName());
  if (numericField != null || sf.getDefaultValue() != null) {
    // TODO: fieldtype needs externalToObject?
    String oldValS = (numericField != null) ?
        numericField.getFirstValue().toString(): sf.getDefaultValue().toString();
    BytesRefBuilder term = new BytesRefBuilder();
    sf.getType().readableToIndexed(oldValS, term);
    Object oldVal = sf.getType().toObject(sf, term.get());

    String fieldValS = fieldVal.toString();
    Number result;
    if (oldVal instanceof Long) {
      result = ((Long) oldVal).longValue() + Long.parseLong(fieldValS);
    } else if (oldVal instanceof Float) {
      result = ((Float) oldVal).floatValue() + Float.parseFloat(fieldValS);
    } else if (oldVal instanceof Double) {
      result = ((Double) oldVal).doubleValue() + Double.parseDouble(fieldValS);
    } else {
      // int, short, byte
      result = ((Integer) oldVal).intValue() + Integer.parseInt(fieldValS);
    }

    toDoc.setField(sif.getName(),  result);
  } else {
    toDoc.setField(sif.getName(), fieldVal);
  }
}
 
Example 28
Source Project: lucene-solr   Source File: AddBlockUpdateTest.java    License: Apache License 2.0 5 votes vote down vote up
private void indexSolrInputDocumentsDirectly(SolrInputDocument ... docs) throws IOException {
  SolrQueryRequest coreReq = new LocalSolrQueryRequest(h.getCore(), new ModifiableSolrParams());
  AddUpdateCommand updateCmd = new AddUpdateCommand(coreReq);
  for (SolrInputDocument doc: docs) {
    updateCmd.solrDoc = doc;
    h.getCore().getUpdateHandler().addDoc(updateCmd);
    updateCmd.clear();
  }
  assertU(commit());
}
 
Example 29
Source Project: lucene-solr   Source File: ChaosMonkeySafeLeaderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void indexr(Object... fields) throws Exception {
  SolrInputDocument doc = new SolrInputDocument();
  addFields(doc, fields);
  addFields(doc, "rnd_b", true);
  indexDoc(doc);
}
 
Example 30
Source Project: tutorials   Source File: SolrJavaIntegration.java    License: MIT License 5 votes vote down vote up
public void addSolrDocument(String documentId, String itemName, String itemPrice) throws SolrServerException, IOException {

        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", documentId);
        document.addField("name", itemName);
        document.addField("price", itemPrice);
        solrClient.add(document);
        solrClient.commit();
    }