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

The following examples show how to use org.apache.solr.common.SolrInputDocument#getFieldValues() . 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: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param fullDoc the full doc to  be compared against
 * @param partialDoc the sub document to be tested
 * @return whether partialDoc is derived from fullDoc
 */
public static boolean isDerivedFromDoc(SolrInputDocument fullDoc, SolrInputDocument partialDoc) {
  for(SolrInputField subSif: partialDoc) {
    Collection<Object> fieldValues = fullDoc.getFieldValues(subSif.getName());
    if (fieldValues == null) return false;
    if (fieldValues.size() < subSif.getValueCount()) return false;
    Collection<Object> partialFieldValues = subSif.getValues();
    // filter all derived child docs from partial field values since they fail List#containsAll check (uses SolrInputDocument#equals which fails).
    // If a child doc exists in partialDoc but not in full doc, it will not be filtered, and therefore List#containsAll will return false
    Stream<Object> nonChildDocElements = partialFieldValues.stream().filter(x -> !(isChildDoc(x) &&
        (fieldValues.stream().anyMatch(y ->
            (isChildDoc(x) &&
                isDerivedFromDoc((SolrInputDocument) y, (SolrInputDocument) x)
            )
        )
        )));
    if (!nonChildDocElements.allMatch(fieldValues::contains)) return false;
  }
  return true;
}
 
Example 2
Source File: ClassificationUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void knnMultiClass_boostFieldsMaxOutputClasses2_shouldAssignMax2Classes() throws Exception {
  UpdateRequestProcessor mockProcessor=mock(UpdateRequestProcessor.class);
  prepareTrainedIndexMultiClass();

  AddUpdateCommand update=new AddUpdateCommand(req());
  SolrInputDocument unseenDocument1 = sdoc(ID, "10",
      TITLE, "word4 word4 word4",
      CONTENT, "word2 word2 ",
      AUTHOR, "unseenAuthor");
  update.solrDoc=unseenDocument1;

  ClassificationUpdateProcessorParams params= initParams(ClassificationUpdateProcessorFactory.Algorithm.KNN);
  params.setInputFieldNames(new String[]{TITLE+"^1.5",CONTENT+"^0.5",AUTHOR+"^2.5"});
  params.setMaxPredictedClasses(2);

  updateProcessorToTest=new ClassificationUpdateProcessor(params,mockProcessor,reader,req().getSchema());

  updateProcessorToTest.processAdd(update);

  @SuppressWarnings({"unchecked"})
  ArrayList<Object> assignedClasses = (ArrayList)unseenDocument1.getFieldValues(TRAINING_CLASS);
  assertThat(assignedClasses.size(),is(2));
  assertThat(assignedClasses.get(0),is("class4"));
  assertThat(assignedClasses.get(1),is("class6"));
}
 
Example 3
Source File: ClassificationUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void bayesMultiClass_maxOutputClasses2_shouldAssignMax2Classes() throws Exception {
  UpdateRequestProcessor mockProcessor=mock(UpdateRequestProcessor.class);
  prepareTrainedIndexMultiClass();

  AddUpdateCommand update=new AddUpdateCommand(req());
  SolrInputDocument unseenDocument1 = sdoc(ID, "10",
      TITLE, "word1 word1 word1",
      CONTENT, "word2 word2 ",
      AUTHOR, "unseenAuthor");
  update.solrDoc=unseenDocument1;

  ClassificationUpdateProcessorParams params= initParams(ClassificationUpdateProcessorFactory.Algorithm.BAYES);
  params.setMaxPredictedClasses(2);

  updateProcessorToTest=new ClassificationUpdateProcessor(params,mockProcessor,reader,req().getSchema());
  updateProcessorToTest.processAdd(update);

  @SuppressWarnings({"unchecked"})
  ArrayList<Object> assignedClasses = (ArrayList)unseenDocument1.getFieldValues(TRAINING_CLASS);
  assertThat(assignedClasses.size(),is(2));
  assertThat(assignedClasses.get(0),is("class2"));
  assertThat(assignedClasses.get(1),is("class1"));
}
 
Example 4
Source File: CloneFieldUpdateProcessorFactoryTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testSelectorClone() throws Exception {
  SolrInputDocument doc = processAdd("clone-selector",
                                     doc(f("id", "1"),
                                         f("source0_s", "nope, not me"),
                                         f("source1_s", "foo"),
                                         f("source2_s", "bar")));
                                         
  assertEquals("source0_s should have stringValue", "nope, not me", doc.getFieldValue("source0_s"));
  assertEquals("source1_s should have stringValue", "foo", doc.getFieldValue("source1_s"));
  assertEquals("source2_s should have stringValue", "bar", doc.getFieldValue("source2_s"));
  Collection<Object> dest_s = doc.getFieldValues("dest_s");
  assertTrue(dest_s.contains("foo"));
  assertTrue(dest_s.contains("bar"));
  assertFalse(dest_s.contains("nope, not me"));
}
 
Example 5
Source File: ClassificationUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void knnMultiClass_maxOutputClasses2_shouldAssignMax2Classes() throws Exception {
  UpdateRequestProcessor mockProcessor=mock(UpdateRequestProcessor.class);
  prepareTrainedIndexMultiClass();

  AddUpdateCommand update=new AddUpdateCommand(req());
  SolrInputDocument unseenDocument1 = sdoc(ID, "10",
      TITLE, "word1 word1 word1",
      CONTENT, "word2 word2 ",
      AUTHOR, "unseenAuthor");
  update.solrDoc=unseenDocument1;

  ClassificationUpdateProcessorParams params= initParams(ClassificationUpdateProcessorFactory.Algorithm.KNN);
  params.setMaxPredictedClasses(2);

  updateProcessorToTest=new ClassificationUpdateProcessor(params,mockProcessor,reader,req().getSchema());
  updateProcessorToTest.processAdd(update);

  @SuppressWarnings({"unchecked"})
  ArrayList<Object> assignedClasses = (ArrayList)unseenDocument1.getFieldValues(TRAINING_CLASS);
  assertThat(assignedClasses.size(),is(2));
  assertThat(assignedClasses.get(0),is("class2"));
  assertThat(assignedClasses.get(1),is("class1"));
}
 
Example 6
Source File: ParsingFieldUpdateProcessorsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testFailedParseMixedDate() throws Exception {
  IndexSchema schema = h.getCore().getLatestSchema();
  assertNull(schema.getFieldOrNull("not_in_schema"));
  Map<Object,Object> mixed = new HashMap<>();
  String[] dateStrings = { "2020-05-13T18:47", "1989-12-14", "1682-07-22T18:33:00.000Z" };
  for (String dateString : dateStrings) {
    mixed.put(parse(isoDateOptionalTimeFormatter, dateString), dateString);
  }
  Double extraDouble = 29.554d;
  mixed.put(extraDouble, extraDouble); // Double-typed field value
  SolrInputDocument d = processAdd("parse-date-no-run-processor", 
                                   doc(f("id", "7201"), f("not_in_schema", mixed.values())));
  assertNotNull(d);
  boolean foundDouble = false;
  for (Object o : d.getFieldValues("not_in_schema")) {
    if (extraDouble == o) {
      foundDouble = true;
    } else {
      assertThat(o, IS_STRING);
    }
    mixed.values().remove(o);
  }
  assertTrue(foundDouble);
  assertTrue(mixed.isEmpty());
}
 
Example 7
Source File: ParsingFieldUpdateProcessorsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testFailedParseMixedInt() throws Exception {
  IndexSchema schema = h.getCore().getLatestSchema();
  assertNull(schema.getFieldOrNull("not_in_schema"));
  Map<Object,Object> mixed = new HashMap<>();
  Float floatVal = 294423.0f;
  mixed.put(85, "85");
  mixed.put(floatVal, floatVal); // Float-typed field value
  mixed.put(-2894518, "-2,894,518");
  mixed.put(1879472193, "1,879,472,193");
  SolrInputDocument d = processAdd("parse-int-no-run-processor",
                                   doc(f("id", "7202"), f("not_in_schema", mixed.values())));
  assertNotNull(d);
  boolean foundFloat = false;
  for (Object o : d.getFieldValues("not_in_schema")) {
    if (floatVal == o) {
      foundFloat = true;
    } else {
      assertThat(o, IS_STRING);
    }
    mixed.values().remove(o);
  }
  assertTrue(foundFloat);
  assertTrue(mixed.isEmpty());
}
 
Example 8
Source File: ParsingFieldUpdateProcessorsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testFailedParseMixedLong() throws Exception {
  IndexSchema schema = h.getCore().getLatestSchema();
  assertNull(schema.getFieldOrNull("not_in_schema"));
  Map<Object,Object> mixed = new HashMap<>();
  Float floatVal = 294423.0f;
  mixed.put(85L, "85");
  mixed.put(floatVal, floatVal); // Float-typed field value
  mixed.put(-2894518L, "-2,894,518");
  mixed.put(1879472193L, "1,879,472,193");
  SolrInputDocument d = processAdd("parse-long-no-run-processor",
                                   doc(f("id", "7204"), f("not_in_schema", mixed.values())));
  assertNotNull(d);
  boolean foundFloat = false;
  for (Object o : d.getFieldValues("not_in_schema")) {
    if (floatVal == o) {
      foundFloat = true;
    } else {
      assertThat(o, IS_STRING);
    }
    mixed.values().remove(o);
  }
  assertTrue(foundFloat);
  assertTrue(mixed.isEmpty());
}
 
Example 9
Source File: ParsingFieldUpdateProcessorsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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 10
Source File: ParsingFieldUpdateProcessorsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testFailedParseMixedFloat() throws Exception {
  IndexSchema schema = h.getCore().getLatestSchema();
  assertNull(schema.getFieldOrNull("not_in_schema"));
  Map<Object,Object> mixed = new HashMap<>();
  Long longVal = 294423L;
  mixed.put(85L, "85");
  mixed.put(longVal, longVal); // Float-typed field value
  mixed.put(-2894518L, "-2,894,518");
  mixed.put(1879472193L, "1,879,472,193");
  SolrInputDocument d = processAdd("parse-float-no-run-processor",
                                   doc(f("id", "7205"), 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 11
Source File: ParsingFieldUpdateProcessorsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testFailedParseMixedDouble() throws Exception {
  IndexSchema schema = h.getCore().getLatestSchema();
  assertNull(schema.getFieldOrNull("not_in_schema"));
  Map<Object,Object> mixed = new HashMap<>();
  Long longVal = 294423L;
  mixed.put(85, "85.0");
  mixed.put(longVal, longVal); // Float-typed field value
  mixed.put(-2894.518, "-2,894.518");
  mixed.put(187947.2193, "187,947.2193");
  SolrInputDocument d = processAdd("parse-double-no-run-processor",
                                   doc(f("id", "7206"), 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 12
Source File: ParsingFieldUpdateProcessorsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
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 13
Source File: UpdateLog.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Add all fields from olderDoc into newerDoc if not already present in newerDoc
 */
private void applyOlderUpdates(@SuppressWarnings({"rawtypes"})SolrDocumentBase newerDoc, SolrInputDocument olderDoc, Set<String> mergeFields) {
  for (String fieldName : olderDoc.getFieldNames()) {
    // if the newerDoc has this field, then this field from olderDoc can be ignored
    if (!newerDoc.containsKey(fieldName) && (mergeFields == null || mergeFields.contains(fieldName))) {
      for (Object val : olderDoc.getFieldValues(fieldName)) {
        newerDoc.addField(fieldName, val);
      }
    }
  }
}
 
Example 14
Source File: TestDocumentBuilder.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeepCopy() throws IOException {
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("field1", "value1");
  doc.addField("field2", "value1");
  doc.addField("field3", "value2");
  doc.addField("field4", 15);
  List<Integer> list = new ArrayList<>();
  list.add(45);
  list.add(33);
  list.add(20);
  doc.addField("field5", list);
  
  SolrInputDocument clone = doc.deepCopy();
  
  System.out.println("doc1: "+ doc);
  System.out.println("clone: "+ clone);
  
  assertNotSame(doc, clone);
  
  Collection<String> fieldNames = doc.getFieldNames();
  for (String name : fieldNames) {
    Collection<Object> values = doc.getFieldValues(name);
    Collection<Object> cloneValues = clone.getFieldValues(name);
    
    assertEquals(values.size(), cloneValues.size());
    assertNotSame(values, cloneValues);
    
    Iterator<Object> cloneIt = cloneValues.iterator();
    for (Object value : values) {
      Object cloneValue = cloneIt.next();
      assertSame(value, cloneValue);
    }
  }
}
 
Example 15
Source File: AbstractSolrLoader.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 
 * 
 * @param d
 * @param field
 * @param val
 * @return true, if value was added to document
 */
public boolean addFieldUnique(SolrInputDocument d, String field, String val) {
	if (val == null)
		return false;
	Collection<Object> vals = d.getFieldValues(field);
	if (vals != null && vals.contains(val))
		return false;
	d.addField(field, val);
	return true;
}
 
Example 16
Source File: CloneFieldUpdateProcessorFactoryTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayClone() throws Exception {
  SolrInputDocument doc = processAdd("clone-array",
                                     doc(f("id", "1"),
                                         f("source1_s", "foo"),
                                         f("source2_s", "bar")));
                                         
  assertEquals("source1_s should have stringValue", "foo", doc.getFieldValue("source1_s"));
  assertEquals("source2_s should have stringValue", "bar", doc.getFieldValue("source2_s"));
  Collection<Object> dest_s = doc.getFieldValues("dest_s");
  assertTrue(dest_s.contains("foo"));
  assertTrue(dest_s.contains("bar"));
}
 
Example 17
Source File: CloneFieldUpdateProcessorFactoryTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiClone() throws Exception {
  SolrInputDocument doc = processAdd("clone-multi",
                                     doc(f("id", "1"),
                                         f("source1_s", "foo"),
                                         f("source2_s", "bar")));
                                         
  assertEquals("source1_s should have stringValue", "foo", doc.getFieldValue("source1_s"));
  assertEquals("source2_s should have stringValue", "bar", doc.getFieldValue("source2_s"));
  Collection<Object> dest_s = doc.getFieldValues("dest_s");
  assertTrue(dest_s.contains("foo"));
  assertTrue(dest_s.contains("bar"));
}
 
Example 18
Source File: MockSolrDocumentCollection.java    From owltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Map<String,Object> convert(SolrInputDocument doc) {
    Map<String,Object> m = new HashMap<>();
    for (String f : doc.getFieldNames()) {
        Collection<Object> vs = doc.getFieldValues(f);
        m.put(f, vs);
    }
    return m;
}
 
Example 19
Source File: CloneFieldUpdateProcessorFactoryTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testMultipleClones() throws Exception {
  SolrInputDocument doc = processAdd("multiple-clones",
                                     doc(f("id", "1"),
                                         f("category", "test"),
                                         f("authors", "author1", "author2"),
                                         f("editors", "ed1", "ed2"),
                                         f("bfriday_price", 4.00),
                                         f("sale_price", 5.00),
                                         f("list_price", 6.00),
                                         f("features", "hill", "valley", "dune")));
                                         
  // the original values should remain
  assertEquals("category should have a value", "test", doc.getFieldValue("category"));

  Collection<Object> auths = doc.getFieldValues("authors");
  assertTrue(auths.size() == 2);
  assertTrue(auths.contains("author1"));
  assertTrue(auths.contains("author2"));
  Collection<Object> eds = doc.getFieldValues("editors");
  assertTrue(eds.size() == 2);
  assertTrue(eds.contains("ed1"));
  assertTrue(eds.contains("ed2"));

  assertEquals("bfriday_price should have a value", 4.0, doc.getFieldValue("bfriday_price"));
  assertEquals("sale_price should have a value", 5.0, doc.getFieldValue("sale_price"));
  assertEquals("list_price should have a value", 6.0, doc.getFieldValue("list_price"));

  Collection<Object> features = doc.getFieldValues("features");
  assertTrue(features.size() == 3);
  assertTrue(features.contains("hill"));
  assertTrue(features.contains("valley"));
  assertTrue(features.contains("dune"));

  // and the copied values shoul be added
  assertEquals("category_s should have a value", "test", doc.getFieldValue("category_s"));

  Collection<Object> contribs = doc.getFieldValues("contributors");
  assertTrue(contribs.size() == 4);
  assertTrue(contribs.contains("author1"));
  assertTrue(contribs.contains("author2"));
  assertTrue(contribs.contains("ed1"));
  assertTrue(contribs.contains("ed2"));

  Collection<Object> prices = doc.getFieldValues("all_prices");
  assertTrue(prices.size() == 2);
  assertTrue(prices.contains(5.0));
  assertTrue(prices.contains(4.0));
  assertFalse(prices.contains(6.0));

  // n.b. the field names below imply singularity but that would be achieved with a subsequent 
  // FirstFieldValueUpdateProcessorFactory (or similar custom class), and not in clone field itself

  Collection<Object> keyf = doc.getFieldValues("key_feature");
  assertTrue(keyf.size() == 3);
  assertTrue(keyf.contains("hill"));
  assertTrue(keyf.contains("valley"));
  assertTrue(keyf.contains("dune"));

  Collection<Object> bestf = doc.getFieldValues("best_feature");
  assertTrue(bestf.size() == 3);
  assertTrue(bestf.contains("hill"));
  assertTrue(bestf.contains("valley"));
  assertTrue(bestf.contains("dune"));
}
 
Example 20
Source File: JsonLoaderTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testChildDocs() throws Exception {
  String str = "{\n" +
      "    \"add\": {\n" +
      "        \"doc\": {\n" +
      "            \"id\": \"1\",\n" +
      "            \"children\": [\n" +
      "                {\n" +
      "                    \"id\": \"2\",\n" +
      "                    \"foo_s\": \"Yaz\"\n" +
      "                },\n" +
      "                {\n" +
      "                    \"id\": \"3\",\n" +
      "                    \"foo_s\": \"Bar\"\n" +
      "                }\n" +
      "            ]\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 one = add.solrDoc;
  assertEquals("1", one.getFieldValue("id"));

  @SuppressWarnings({"unchecked"})
  List<SolrInputDocument> children = (List) one.getFieldValues("children");
  SolrInputDocument two = children.get(0);
  assertEquals("2", two.getFieldValue("id"));
  assertEquals("Yaz", two.getFieldValue("foo_s"));

  SolrInputDocument three = children.get(1);
  assertEquals("3", three.getFieldValue("id"));
  assertEquals("Bar", three.getFieldValue("foo_s"));

  req.close();

}