Java Code Examples for org.apache.solr.common.SolrInputDocument#setField()

The following examples show how to use org.apache.solr.common.SolrInputDocument#setField() . 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: SolrIOTestUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a list of test documents for insertion.
 *
 * @return the list of json String representing the documents
 */
static List<SolrInputDocument> createDocuments(long numDocs) {
  String[] scientists = {
    "Lovelace",
    "Franklin",
    "Meitner",
    "Hopper",
    "Curie",
    "Faraday",
    "Newton",
    "Bohr",
    "Galilei",
    "Maxwell"
  };
  ArrayList<SolrInputDocument> data = new ArrayList<>();
  for (int i = 0; i < numDocs; i++) {
    int index = i % scientists.length;
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", String.valueOf(i));
    doc.setField("scientist", scientists[index]);
    data.add(doc);
  }
  return data;
}
 
Example 2
Source File: RemoveTrailingUnderscoreProcessor.java    From apache-solr-essentials with Apache License 2.0 6 votes vote down vote up
/**
 * Intercept the add document operation.
 * Here this process gets a chance to change the incoming {@link SolrInputDocument}.
 * 
 * @param command the update command.
 * @throws IOException in case of I/O failure.
 */
@Override
public void processAdd(final AddUpdateCommand command) throws IOException {
	// 1. Retrieve the SolrInputDocument that contains data to be indexed.
	final SolrInputDocument document = command.getSolrInputDocument();
	
	// 2. Loop through the target fields
	for (final String fieldName : fields) {
		
		// 3. Get the field values (for simplicity we assume fields are monovalued and are strings)
		final String fieldValue = (String) document.getFieldValue(fieldName);
		
		// 4. Check and eventually change the value of that field.
		if (fieldValue != null && fieldValue.endsWith("_")) {
			document.setField(fieldName, fieldValue.substring(0, fieldValue.length() -1));
		}
	}
	
	// 5. IMPORTANT: forward the control to the next processor in the chain.
	super.processAdd(command);
}
 
Example 3
Source File: ItemServiceImpl.java    From learning-taotaoMall with MIT License 5 votes vote down vote up
@Override
public TaotaoResult importAllItems() {
	try {
		//查询商品列表
		List<Item> list = itemMapper.getItemList();
		//把商品信息写入索引库
		for (Item item : list) {
			//创建一个SolrInputDocument对象
			SolrInputDocument document = new SolrInputDocument();
			document.setField("id", item.getId());
			document.setField("item_title", item.getTitle());
			document.setField("item_sell_point", item.getSell_point());
			document.setField("item_price", item.getPrice());
			document.setField("item_image", item.getImage());
			document.setField("item_category_name", item.getCategory_name());
			//document.setField("item_desc", item.getItem_des());
			//写入索引库
			solrServer.add(document);
		}
		//提交修改
		solrServer.commit();
	} catch (Exception e) {
		e.printStackTrace();
		return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
	}
	return TaotaoResult.ok();
}
 
Example 4
Source File: SolrSpewer.java    From extract with MIT License 5 votes vote down vote up
/**
 * Set a list of values to a multivalued field on a Solr document.
 *
 * @param document the document to add the values to
 * @param name the name of the field
 * @param values the values
 */
void setFieldValue(final SolrInputDocument document, final String name, final String[] values) {
	if (atomicWrites) {
		final Map<String, String[]> atomic = new HashMap<>();
		atomic.put("set", values);
		document.setField(name, atomic);
	} else {
		document.setField(name, values);
	}
}
 
Example 5
Source File: TermsResponseTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testTermsResponse() throws Exception {
  SolrInputDocument doc = new SolrInputDocument();
  doc.setField("id", 1);
  doc.setField("terms_s", "samsung");
  getSolrClient().add(doc);
  getSolrClient().commit(true, true);

  SolrQuery query = new SolrQuery();
  query.setRequestHandler("/terms");
  query.setTerms(true);
  query.setTermsLimit(5);
  query.setTermsLower("s");
  query.setTermsPrefix("s");
  query.addTermsField("terms_s");
  query.setTermsMinCount(1);

  QueryRequest request = new QueryRequest(query);
  List<Term> terms = request.process(getSolrClient()).getTermsResponse().getTerms("terms_s");

  Assert.assertNotNull(terms);
  Assert.assertEquals(terms.size(), 1);

  Term term = terms.get(0);
  Assert.assertEquals(term.getTerm(), "samsung");
  Assert.assertEquals(term.getFrequency(), 1);
}
 
Example 6
Source File: SolrPingTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Before
@Override
public void setUp() throws Exception {
  super.setUp();
  clearIndex();
  assertU(commit());
  assertU(optimize());
  SolrInputDocument doc = new SolrInputDocument();
  doc.setField("id", 1);
  doc.setField("terms_s", "samsung");
  getSolrClient().add(doc);
  getSolrClient().commit(true, true);
}
 
Example 7
Source File: FieldInjectorRegistry.java    From SolRDF with Apache License 2.0 5 votes vote down vote up
@Override
public void inject(final SolrInputDocument document, final Object value) {
	final Calendar calendar = ((XSDDateTime)value).asCalendar();
	final long millis = calendar.getTimeInMillis();
	document.setField(
			Field.DATE_OBJECT, 
			new Date(millis + calendar.getTimeZone().getOffset(millis)));
}
 
Example 8
Source File: ConcurrentUpdateSolrClientMultiCollectionTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void splitDocumentsAcrossCollections(SolrClient client, int numTotalDocs) throws IOException, SolrServerException {
  for (int docNum = 0; docNum < numTotalDocs; docNum++) {
    final SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", "value" + docNum);

    if (docNum %2 == 0) {
      client.add(COLLECTION_ONE_NAME, doc);
    } else {
      client.add(COLLECTION_TWO_NAME, doc);
    }
  }

  client.commit(COLLECTION_ONE_NAME);
  client.commit(COLLECTION_TWO_NAME);
}
 
Example 9
Source File: NestedUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void setPathField(SolrInputDocument sdoc, String fullPath) {
  sdoc.setField(IndexSchema.NEST_PATH_FIELD_NAME, fullPath);
}
 
Example 10
Source File: SolrInputDocumentConverter.java    From kafka-connect-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected void setMap(SolrInputDocument result, String fieldName, Schema schema, Map value) {
  SolrInputDocument childDocument = convert(value);
  result.setField(fieldName, childDocument);
}
 
Example 11
Source File: DefaultSolrInputDocumentMapper.java    From storm-solr with Apache License 2.0 4 votes vote down vote up
protected SolrInputDocument map2doc(SolrInputDocument doc, Map map) {
  for (Object key : map.keySet()) {
    doc.setField((String)key, map.get(key));
  }
  return doc;
}
 
Example 12
Source File: SolrInputDocumentConverter.java    From kafka-connect-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected void setArray(SolrInputDocument result, String fieldName, Schema schema, List value) {
  result.setField(fieldName, value);
}
 
Example 13
Source File: FieldInjectorRegistry.java    From SolRDF with Apache License 2.0 4 votes vote down vote up
@Override
public void inject(final SolrInputDocument document, final Object value) {
	document.setField(Field.NUMERIC_OBJECT, value instanceof BigDecimal ? ((BigDecimal)value).doubleValue() : value);
}
 
Example 14
Source File: MergeIndexesExampleTestBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private UpdateRequest setupCores() throws SolrServerException, IOException {
  UpdateRequest up = new UpdateRequest();
  up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
  up.deleteByQuery("*:*");
  up.process(getSolrCore0());
  up.process(getSolrCore1());
  up.clear();

  // Add something to each core
  SolrInputDocument doc = new SolrInputDocument();
  doc.setField("id", "AAA");
  doc.setField("name", "core0");

  // Add to core0
  up.add(doc);
  up.process(getSolrCore0());

  // Add to core1
  doc.setField("id", "BBB");
  doc.setField("name", "core1");
  up.add(doc);
  up.process(getSolrCore1());

  // Now Make sure AAA is in 0 and BBB in 1
  SolrQuery q = new SolrQuery();
  QueryRequest r = new QueryRequest(q);
  q.setQuery("id:AAA");
  assertEquals(1, r.process(getSolrCore0()).getResults().size());
  assertEquals(0, r.process(getSolrCore1()).getResults().size());

  assertEquals(1,
      getSolrCore0().query(new SolrQuery("id:AAA")).getResults().size());
  assertEquals(0,
      getSolrCore0().query(new SolrQuery("id:BBB")).getResults().size());

  assertEquals(0,
      getSolrCore1().query(new SolrQuery("id:AAA")).getResults().size());
  assertEquals(1,
      getSolrCore1().query(new SolrQuery("id:BBB")).getResults().size());

  return up;
}
 
Example 15
Source File: SolrInputDocumentConverter.java    From kafka-connect-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected void setStringField(SolrInputDocument result, String fieldName, String value) {
  result.setField(fieldName, value);
}
 
Example 16
Source File: SolrLogPostTool.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void setFieldIfUnset(SolrInputDocument doc, String fieldName, String fieldValue) {
  if (doc.containsKey(fieldName)) return;

  doc.setField(fieldName, fieldValue);
}
 
Example 17
Source File: SolrInputDocumentConverter.java    From kafka-connect-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected void setBytesField(SolrInputDocument result, String fieldName, byte[] value) {
  result.setField(fieldName, value);
}
 
Example 18
Source File: TestSolrProperties.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testProperties() throws Exception {

  UpdateRequest up = new UpdateRequest();
  up.setAction(ACTION.COMMIT, true, true);
  up.deleteByQuery("*:*");
  up.process(getSolrCore0());
  up.process(getSolrCore1());
  up.clear();

  // Add something to each core
  SolrInputDocument doc = new SolrInputDocument();
  doc.setField("id", "AAA");
  doc.setField("core0", "yup stopfra stopfrb stopena stopenb");

  // Add to core0
  up.add(doc);
  up.process(getSolrCore0());

  SolrTestCaseJ4.ignoreException("unknown field");

  // You can't add it to core1
  expectThrows(Exception.class, () -> up.process(getSolrCore1()));

  // Add to core1
  doc.setField("id", "BBB");
  doc.setField("core1", "yup stopfra stopfrb stopena stopenb");
  doc.removeField("core0");
  up.add(doc);
  up.process(getSolrCore1());

  // You can't add it to core1
  SolrTestCaseJ4.ignoreException("core0");
  expectThrows(Exception.class, () -> up.process(getSolrCore0()));
  SolrTestCaseJ4.resetExceptionIgnores();

  // now Make sure AAA is in 0 and BBB in 1
  SolrQuery q = new SolrQuery();
  QueryRequest r = new QueryRequest(q);
  q.setQuery("id:AAA");
  assertEquals(1, r.process(getSolrCore0()).getResults().size());
  assertEquals(0, r.process(getSolrCore1()).getResults().size());

  // Now test Changing the default core
  assertEquals(1, getSolrCore0().query(new SolrQuery("id:AAA")).getResults().size());
  assertEquals(0, getSolrCore0().query(new SolrQuery("id:BBB")).getResults().size());

  assertEquals(0, getSolrCore1().query(new SolrQuery("id:AAA")).getResults().size());
  assertEquals(1, getSolrCore1().query(new SolrQuery("id:BBB")).getResults().size());

  // Now test reloading it should have a newer open time
  String name = "core0";
  SolrClient coreadmin = getSolrAdmin();
  CoreAdminResponse mcr = CoreAdminRequest.getStatus(name, coreadmin);
  long before = mcr.getStartTime(name).getTime();
  CoreAdminRequest.reloadCore(name, coreadmin);

  mcr = CoreAdminRequest.getStatus(name, coreadmin);
  long after = mcr.getStartTime(name).getTime();
  assertTrue("should have more recent time: " + after + "," + before, after > before);

}
 
Example 19
Source File: FieldInjectorRegistry.java    From SolRDF with Apache License 2.0 4 votes vote down vote up
@Override
public void inject(final SolrInputDocument document, final Object value) {
	document.setField(Field.BOOLEAN_OBJECT, value);
}
 
Example 20
Source File: DocBasedVersionConstraintsProcessor.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a tombstone document, that will be used to update a document that's being deleted by ID. The returned document will contain:
 * <ul>
 * <li>uniqueKey</li>
 * <li>versions (All the fields configured as in the {@code versionField} parameter)</li>
 * <li>All the fields set in the {@code tombstoneConfig} parameter</li>
 * </ul>
 * 
 * Note that if the schema contains required fields (other than version fields or uniqueKey), they should be populated by the {@code tombstoneConfig}, 
 * otherwise this tombstone will fail when indexing (and consequently the delete will fail). Alternatively, required fields must be populated by some
 * other means (like {@code DocBasedVersionConstraintsProcessorFactory} or similar) 
 */
protected SolrInputDocument createTombstoneDocument(IndexSchema schema, String id, String[] versionFieldNames, String[] deleteParamValues, NamedList<Object> tombstoneConfig) {
  final SolrInputDocument newDoc = new SolrInputDocument();
  
  if (tombstoneConfig != null) {
    tombstoneConfig.forEach((k,v) -> newDoc.addField(k, v));
  }
  newDoc.setField(schema.getUniqueKeyField().getName(), id);
  setDeleteParamValues(newDoc, versionFieldNames, deleteParamValues);
  return newDoc;
}