Java Code Examples for org.apache.solr.common.SolrDocument#getFieldValues()
The following examples show how to use
org.apache.solr.common.SolrDocument#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: MergingSolrSpewer.java From extract with MIT License | 6 votes |
private void mergeField(final String name, final String newValue, final SolrDocument existingDocument, final SolrInputDocument inputDocument) { // Even though the superclass sets the path and parent path fields, we should set them again in case there's // a retry and they need to be overwritten. if (null == existingDocument) { setFieldValue(inputDocument, name, newValue); return; } // Create a HashSet from existing values so that only non-existing (distinct) values are added. // A HashSet gives constant time performance, as opposed to a loop, which is important when dealing with // potentially thousands of values. final Collection<Object> existingValues = existingDocument.getFieldValues(name); if (null != existingValues) { final Set<String> values = existingValues.stream() .map(String::valueOf) .collect(Collectors.toCollection(HashSet::new)); values.add(newValue); setFieldValue(inputDocument, name, values.toArray(new String[values.size()])); } else { setFieldValue(inputDocument, name, newValue); } }
Example 2
Source File: CarrotClusteringEngine.java From lucene-solr with Apache License 2.0 | 5 votes |
private String getConcatenated(SolrDocument sdoc, String fieldsSpec) { StringBuilder result = new StringBuilder(); for (String field : fieldsSpec.split("[, ]")) { Collection<Object> vals = sdoc.getFieldValues(field); if (vals == null) continue; Iterator<Object> ite = vals.iterator(); while(ite.hasNext()){ // Join multiple values with a period so that Carrot2 does not pick up // phrases that cross field value boundaries (in most cases it would // create useless phrases). result.append(Objects.toString(ite.next(), "")).append(" . "); } } return result.toString().trim(); }
Example 3
Source File: RealTimeGetComponent.java From lucene-solr with Apache License 2.0 | 5 votes |
private static SolrInputDocument toSolrInputDocument(SolrDocument doc, IndexSchema schema) { SolrInputDocument out = new SolrInputDocument(); for( String fname : doc.getFieldNames() ) { boolean fieldArrayListCreated = false; SchemaField sf = schema.getFieldOrNull(fname); if (sf != null) { if ((!sf.hasDocValues() && !sf.stored()) || schema.isCopyFieldTarget(sf)) continue; } for (Object val: doc.getFieldValues(fname)) { if (val instanceof Field) { Field f = (Field) val; if (sf != null) { val = sf.getType().toObject(f); // object or external string? } else { val = f.stringValue(); if (val == null) val = f.numericValue(); if (val == null) val = f.binaryValue(); if (val == null) val = f; } } else if(val instanceof SolrDocument) { val = toSolrInputDocument((SolrDocument) val, schema); if(!fieldArrayListCreated && doc.getFieldValue(fname) instanceof Collection) { // previous value was array so we must return as an array even if was a single value array out.setField(fname, Lists.newArrayList(val)); fieldArrayListCreated = true; continue; } } out.addField(fname, val); } } return out; }
Example 4
Source File: DefaultSolrHighlighter.java From lucene-solr with Apache License 2.0 | 5 votes |
/** * Fetches field values to highlight. If the field value should come from an atypical place (or another aliased * field name, then a subclass could override to implement that. */ protected List<String> getFieldValues(SolrDocument doc, String fieldName, int maxValues, int maxCharsToAnalyze, SolrQueryRequest req) { // Collect the Fields we will examine (could be more than one if multi-valued) Collection<Object> fieldValues = doc.getFieldValues(fieldName); if (fieldValues == null) { return Collections.emptyList(); } FieldType fieldType = req.getSchema().getFieldType(fieldName); List<String> result = new ArrayList<>(); for (Object value : fieldValues) { String strValue; if (value instanceof IndexableField) { strValue = fieldType.toExternal((IndexableField) value); } else { strValue = value.toString(); // TODO FieldType needs an API for this, e.g. toExternalFromDv() } result.add(strValue); maxCharsToAnalyze -= strValue.length();//we exit early if we'll never get to analyze the value maxValues--; if (maxValues <= 0 || maxCharsToAnalyze <= 0) { break; } } return result; }
Example 5
Source File: TestRealTimeGet.java From incubator-sentry with Apache License 2.0 | 5 votes |
private void assertConcurrentOnDocList(SolrDocumentList list, String authField, String expectedAuthFieldValue) { for (SolrDocument doc : list) { Collection<Object> authFieldValues = doc.getFieldValues(authField); assertNotNull(authField + " should not be null. Doc: " + doc, authFieldValues); boolean foundAuthFieldValue = false; for (Object obj : authFieldValues) { if (obj.toString().equals(expectedAuthFieldValue)) { foundAuthFieldValue = true; break; } } assertTrue("Did not find: " + expectedAuthFieldValue + " in doc: " + doc, foundAuthFieldValue); } }
Example 6
Source File: AbstractSolrMorphlineTest.java From kite with Apache License 2.0 | 5 votes |
protected void testDocumentContent(HashMap<String, ExpectedResult> expectedResultMap) throws Exception { QueryResponse rsp = solrClient.query(new SolrQuery("*:*").setRows(Integer.MAX_VALUE)); // Check that every expected field/values shows up in the actual query for (Entry<String, ExpectedResult> current : expectedResultMap.entrySet()) { String field = current.getKey(); for (String expectedFieldValue : current.getValue().getFieldValues()) { ExpectedResult.CompareType compareType = current.getValue().getCompareType(); boolean foundField = false; for (SolrDocument doc : rsp.getResults()) { Collection<Object> actualFieldValues = doc.getFieldValues(field); if (compareType == ExpectedResult.CompareType.equals) { if (actualFieldValues != null && actualFieldValues.contains(expectedFieldValue)) { foundField = true; break; } } else { for (Iterator<Object> it = actualFieldValues.iterator(); it.hasNext(); ) { String actualValue = it.next().toString(); // test only supports string comparison if (actualFieldValues != null && actualValue.contains(expectedFieldValue)) { foundField = true; break; } } } } assert(foundField); // didn't find expected field/value in query } } }
Example 7
Source File: SolrTestCaseJ4.java From lucene-solr with Apache License 2.0 | 4 votes |
public boolean compareSolrDocument(Object expected, Object actual) { if (!(expected instanceof SolrDocument) || !(actual instanceof SolrDocument)) { return false; } if (expected == actual) { return true; } SolrDocument solrDocument1 = (SolrDocument) expected; SolrDocument solrDocument2 = (SolrDocument) actual; if(solrDocument1.getFieldNames().size() != solrDocument2.getFieldNames().size()) { return false; } Iterator<String> iter1 = solrDocument1.getFieldNames().iterator(); Iterator<String> iter2 = solrDocument2.getFieldNames().iterator(); if(iter1.hasNext()) { String key1 = iter1.next(); String key2 = iter2.next(); Object val1 = solrDocument1.getFieldValues(key1); Object val2 = solrDocument2.getFieldValues(key2); if(!key1.equals(key2) || !val1.equals(val2)) { return false; } } if(solrDocument1.getChildDocuments() == null && solrDocument2.getChildDocuments() == null) { return true; } if(solrDocument1.getChildDocuments() == null || solrDocument2.getChildDocuments() == null) { return false; } else if(solrDocument1.getChildDocuments().size() != solrDocument2.getChildDocuments().size()) { return false; } else { Iterator<SolrDocument> childDocsIter1 = solrDocument1.getChildDocuments().iterator(); Iterator<SolrDocument> childDocsIter2 = solrDocument2.getChildDocuments().iterator(); while(childDocsIter1.hasNext()) { if(!compareSolrDocument(childDocsIter1.next(), childDocsIter2.next())) { return false; } } return true; } }
Example 8
Source File: NestedShardedAtomicUpdateTest.java From lucene-solr with Apache License 2.0 | 4 votes |
public void doRootShardRoutingTest() throws Exception { assertEquals(4, cloudClient.getZkStateReader().getClusterState().getCollection(DEFAULT_COLLECTION).getSlices().size()); final String[] ids = {"3", "4", "5", "6"}; assertEquals("size of ids to index should be the same as the number of clients", clients.size(), ids.length); // for now, we know how ranges will be distributed to shards. // may have to look it up in clusterstate if that assumption changes. SolrInputDocument doc = sdoc("id", "1", "level_s", "root"); final SolrParams params = params("wt", "json", "_route_", "1"); int which = (params.get("_route_").hashCode() & 0x7fffffff) % clients.size(); SolrClient aClient = clients.get(which); indexDoc(aClient, params, doc); doc = sdoc("id", "1", "children", map("add", sdocs(sdoc("id", "2", "level_s", "child")))); indexDoc(aClient, params, doc); for(int idIndex = 0; idIndex < ids.length; ++idIndex) { doc = sdoc("id", "2", "grandChildren", map("add", sdocs(sdoc("id", ids[idIndex], "level_s", "grand_child")))); indexDocAndRandomlyCommit(getRandomSolrClient(), params, doc); doc = sdoc("id", "3", "inplace_updatable_int", map("inc", "1")); indexDocAndRandomlyCommit(getRandomSolrClient(), params, doc); // assert RTG request respects _route_ param QueryResponse routeRsp = getRandomSolrClient().query(params("qt","/get", "id","2", "_route_", "1")); SolrDocument results = (SolrDocument) routeRsp.getResponse().get("doc"); assertNotNull("RTG should find doc because _route_ was set to the root documents' ID", results); assertEquals("2", results.getFieldValue("id")); // assert all docs are indexed under the same root getRandomSolrClient().commit(); assertEquals(0, getRandomSolrClient().query(params("q", "-_root_:1")).getResults().size()); // assert all docs are indexed inside the same block QueryResponse rsp = getRandomSolrClient().query(params("qt","/get", "id","1", "fl", "*, [child]")); SolrDocument val = (SolrDocument) rsp.getResponse().get("doc"); assertEquals("1", val.getFieldValue("id")); @SuppressWarnings({"unchecked"}) List<SolrDocument> children = (List) val.getFieldValues("children"); assertEquals(1, children.size()); SolrDocument childDoc = children.get(0); assertEquals("2", childDoc.getFieldValue("id")); @SuppressWarnings({"unchecked"}) List<SolrDocument> grandChildren = (List) childDoc.getFieldValues("grandChildren"); assertEquals(idIndex + 1, grandChildren.size()); SolrDocument grandChild = grandChildren.get(0); assertEquals(idIndex + 1, grandChild.getFirstValue("inplace_updatable_int")); assertEquals("3", grandChild.getFieldValue("id")); } }
Example 9
Source File: NestedShardedAtomicUpdateTest.java From lucene-solr with Apache License 2.0 | 4 votes |
public void doNestedInplaceUpdateTest() throws Exception { assertEquals(4, cloudClient.getZkStateReader().getClusterState().getCollection(DEFAULT_COLLECTION).getSlices().size()); final String[] ids = {"3", "4", "5", "6"}; assertEquals("size of ids to index should be the same as the number of clients", clients.size(), ids.length); // for now, we know how ranges will be distributed to shards. // may have to look it up in clusterstate if that assumption changes. SolrInputDocument doc = sdoc("id", "1", "level_s", "root"); final SolrParams params = params("wt", "json", "_route_", "1"); int which = (params.get("_route_").hashCode() & 0x7fffffff) % clients.size(); SolrClient aClient = clients.get(which); indexDocAndRandomlyCommit(aClient, params, doc); doc = sdoc("id", "1", "children", map("add", sdocs(sdoc("id", "2", "level_s", "child")))); indexDocAndRandomlyCommit(aClient, params, doc); doc = sdoc("id", "2", "grandChildren", map("add", sdocs(sdoc("id", ids[0], "level_s", "grand_child")))); indexDocAndRandomlyCommit(aClient, params, doc); for (int fieldValue = 1; fieldValue < 5; ++fieldValue) { doc = sdoc("id", "3", "inplace_updatable_int", map("inc", "1")); indexDocAndRandomlyCommit(getRandomSolrClient(), params, doc); // assert RTG request respects _route_ param QueryResponse routeRsp = getRandomSolrClient().query(params("qt","/get", "id","2", "_route_", "1")); SolrDocument results = (SolrDocument) routeRsp.getResponse().get("doc"); assertNotNull("RTG should find doc because _route_ was set to the root documents' ID", results); assertEquals("2", results.getFieldValue("id")); // assert all docs are indexed under the same root getRandomSolrClient().commit(); assertEquals(0, getRandomSolrClient().query(params("q", "-_root_:1")).getResults().size()); // assert all docs are indexed inside the same block QueryResponse rsp = getRandomSolrClient().query(params("qt","/get", "id","1", "fl", "*, [child]")); SolrDocument val = (SolrDocument) rsp.getResponse().get("doc"); assertEquals("1", val.getFieldValue("id")); @SuppressWarnings({"unchecked"}) List<SolrDocument> children = (List) val.getFieldValues("children"); assertEquals(1, children.size()); SolrDocument childDoc = children.get(0); assertEquals("2", childDoc.getFieldValue("id")); @SuppressWarnings({"unchecked"}) List<SolrDocument> grandChildren = (List) childDoc.getFieldValues("grandChildren"); assertEquals(1, grandChildren.size()); SolrDocument grandChild = grandChildren.get(0); assertEquals(fieldValue, grandChild.getFirstValue("inplace_updatable_int")); assertEquals("3", grandChild.getFieldValue("id")); } }
Example 10
Source File: SolrLookingBlurServerTest.java From incubator-retired-blur with Apache License 2.0 | 4 votes |
@Test public void basicFullTextQuery() throws Exception { String table = "basicFullTextQuery"; SolrServer server = TestTableCreator.newTable(table) .withRowCount(1).withRecordsPerRow(2) .withRecordColumns("fam.value", "fam.mvf", "fam.mvf").create(); SolrQuery query = new SolrQuery("value0-0"); QueryResponse response = server.query(query); assertEquals("We should get our doc back.", 1l, response.getResults().getNumFound()); SolrDocument docResult = response.getResults().get(0); assertEquals("0", docResult.getFieldValue("recordid")); assertEquals("value0-0", docResult.getFieldValue("fam.value")); Collection<Object> mvfVals = docResult.getFieldValues("fam.mvf"); assertTrue("We should get all our values back[" + mvfVals + "]", CollectionUtils.isEqualCollection(mvfVals, Lists.newArrayList("value0-0", "value0-0"))); removeTable(table); }