Java Code Examples for org.apache.solr.common.SolrDocument#getFieldNames()

The following examples show how to use org.apache.solr.common.SolrDocument#getFieldNames() . 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: SolrController.java    From Spring-Boot-Book with Apache License 2.0 6 votes vote down vote up
@RequestMapping("/getById/{id}")
    public String getByIdFromSolr(@PathVariable("id") String id) throws IOException, SolrServerException {

        //根据id查询内容
        SolrDocument solrDocument = solrClient.getById(id);
        //获取filedName
        Collection<String> fieldNames = solrDocument.getFieldNames();
        //获取file名和内容
        Map<String, Object> fieldValueMap = solrDocument.getFieldValueMap();

//            int childDocumentCount = solrDocument.getChildDocumentCount();

        List<SolrDocument> childDocuments = solrDocument.getChildDocuments();
        String results = solrDocument.toString();
        //fieldNames
        // fieldValueMap
        // childDocuments;
        return results;

    }
 
Example 2
Source File: SolrUtils.java    From vind with Apache License 2.0 6 votes vote down vote up
public static SolrInputDocument toSolrInputDocument(SolrDocument solrDocument) {
    SolrInputDocument solrInputDocument = new SolrInputDocument();

    for (String name : solrDocument.getFieldNames()) {
        solrInputDocument.addField(name, solrDocument.getFieldValue(name));
    }

    //Don't forget children documents
    if(solrDocument.getChildDocuments() != null) {
        for(SolrDocument childDocument : solrDocument.getChildDocuments()) {
            //You can add paranoic check against infinite loop childDocument == solrDocument
            solrInputDocument.addChildDocument(toSolrInputDocument(childDocument));
        }
    }
    return solrInputDocument;
}
 
Example 3
Source File: TestDynamicFieldNamesIndexCorrectly.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void assertThatDocsHaveCorrectFields(final Collection<SolrInputDocument> solrDocs,
    final SolrDocumentList resultDocs) {
  assertEquals("Wrong number of docs found", resultDocs.getNumFound(), solrDocs.size());
  final Map<Object,SolrDocument> resultMap = resultDocs.stream()
      .collect(Collectors.toMap(doc -> doc.getFieldValue("id"), doc -> doc));
  Iterator<SolrInputDocument> it = solrDocs.iterator();
  while (it.hasNext()) {
    final SolrInputDocument inDoc = it.next();
    final String id = inDoc.getField("id").getValue().toString();
    final SolrDocument resultDoc = resultMap.get(id);
    final Collection<String> resultFieldNames = resultDoc.getFieldNames();
    inDoc
        .getFieldNames()
        .forEach(
            fieldName -> {
              assertThat(
                  String.format(Locale.ROOT, "Doc %s does not have field %s, it has %s", id, fieldName,
                      resultFieldNames),
                  resultFieldNames, new IsCollectionContaining<>(new IsEqual<>(fieldName)));
            });
  }
}
 
Example 4
Source File: SolrUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Converts a {@link SolrDocument} to a {@link SolrInputDocument}
 *
 * @param solrDocument
 * @return input document
 */
public static SolrInputDocument toSolrInputDocument(SolrDocument solrDocument) {

	/*
	 * Note: ClientUtils.toSolrInputDocument was removed in solr 6 Replacement found on
	 * https://stackoverflow.com/questions/38266684/
	 * substitute-of-org-apache-solr-client-solrj-util-clientutils-tosolrinputdocument
	 */

	SolrInputDocument solrInputDocument = new SolrInputDocument();

	for (String name : solrDocument.getFieldNames()) {
		solrInputDocument.addField(name, solrDocument.getFieldValue(name));
	}

	// Don't forget children documents
	if (solrDocument.getChildDocuments() != null) {
		for (SolrDocument childDocument : solrDocument.getChildDocuments()) {
			// You can add paranoic check against infinite loop childDocument == solrDocument
			solrInputDocument.addChildDocument(toSolrInputDocument(childDocument));
		}
	}
	return solrInputDocument;
}
 
Example 5
Source File: JsonQueryRequestIntegrationTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void canSpecifyFieldsToBeReturned() throws Exception {
  final JsonQueryRequest simpleQuery = new JsonQueryRequest()
      .setQuery("*:*")
      .returnFields("id", "name");
  QueryResponse queryResponse = simpleQuery.process(cluster.getSolrClient(), COLLECTION_NAME);
  assertEquals(0, queryResponse.getStatus());
  final SolrDocumentList docs = queryResponse.getResults();
  assertEquals(NUM_BOOKS_TOTAL, docs.getNumFound());
  for (SolrDocument returnedDoc : docs) {
    final Collection<String> fields = returnedDoc.getFieldNames();
    assertEquals(2, fields.size());
    assertTrue("Expected field list to contain 'id'", fields.contains("id"));
    assertTrue("Expected field list to contain 'name'", fields.contains("name"));
  }
}
 
Example 6
Source File: JsonRequestApiTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonQueryUsingParamsBlock() throws Exception {
  SolrClient solrClient = cluster.getSolrClient();

  //tag::solrj-json-query-params-block[]
  final ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("fl", "name", "price");
  final JsonQueryRequest simpleQuery = new JsonQueryRequest(params)
      .withParam("q", "memory")
      .withParam("rows", 1);
  QueryResponse queryResponse = simpleQuery.process(solrClient, COLLECTION_NAME);
  // end::solrj-json-query-params-block[]

  assertEquals(0, queryResponse.getStatus());
  assertEquals(1, queryResponse.getResults().size());
  final SolrDocument doc = queryResponse.getResults().get(0);
  final Collection<String> returnedFields = doc.getFieldNames();
  assertEquals(2, doc.getFieldNames().size());
  assertTrue("Expected returned field list to include 'name'", returnedFields.contains("name"));
  assertTrue("Expected returned field list to include 'price'", returnedFields.contains("price"));
}
 
Example 7
Source File: TestPutSolrRecord.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that given SolrServer contains the expected SolrDocuments.
 */
private static void verifySolrDocuments(SolrClient solrServer, Collection<SolrDocument> expectedDocuments)
        throws IOException, SolrServerException {

    solrServer.commit();

    SolrQuery query = new SolrQuery("*:*");
    QueryResponse qResponse = solrServer.query(query);
    Assert.assertEquals(expectedDocuments.size(), qResponse.getResults().getNumFound());

    // verify documents have expected fields and values
    for (SolrDocument expectedDoc : expectedDocuments) {
        boolean found = false;
        for (SolrDocument solrDocument : qResponse.getResults()) {
            boolean foundAllFields = true;
            for (String expectedField : expectedDoc.getFieldNames()) {
                Object expectedVal = expectedDoc.getFirstValue(expectedField);
                Object actualVal = solrDocument.getFirstValue(expectedField);
                foundAllFields = expectedVal.equals(actualVal);
            }

            if (foundAllFields) {
                found = true;
                break;
            }
        }
        Assert.assertTrue("Could not find " + expectedDoc, found);
    }
}
 
Example 8
Source File: ConverterService.java    From chronix.server with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a lucene document to a solr document.
 *
 * @param schema    the index schema
 * @param luceneDoc the lucene document
 * @return solr document
 */
public SolrDocument toSolrDoc(IndexSchema schema, Document luceneDoc) {
    SolrDocument solrDoc = new SolrDocument();
    luceneDoc.forEach(it -> solrDoc.addField(it.name(), schema.getField(it.name()).getType().toObject(it)));
    for (String field : solrDoc.getFieldNames()) {
        Object value = solrDoc.getFieldValue(field);
        if (value instanceof ByteBuffer) {
            solrDoc.setField(field, ((ByteBuffer) value).array());
        }
    }
    return solrDoc;
}
 
Example 9
Source File: SolrRehashConsumer.java    From extract with MIT License 5 votes vote down vote up
@Override
protected void consume(final SolrDocument input) throws SolrServerException, IOException, NoSuchAlgorithmException {
	final String inputPath = (String) input.getFieldValue(pathField);
	final String outputPath;

	if (null != pattern && null != replacement) {
		outputPath = pattern.matcher(inputPath).replaceAll(replacement);
	} else {
		outputPath = inputPath;
	}

	final String inputId = (String) input.getFieldValue(idField);
	final String outputId = DatatypeConverter.printHexBinary(MessageDigest.getInstance(idAlgorithm)
		.digest(outputPath.getBytes(outputEncoding)));
	final String outputPathParent = Objects.toString(Paths.get(outputPath).getParent(), "");

	// If the hash hasn't changed, skip.
	// Skip by comparing the hash values and not the paths because the algorithm might have been changed.
	if (inputId.equals(outputId)) {
		return;
	}

	final SolrInputDocument output = new SolrInputDocument();

	for (String name: input.getFieldNames()) {
		output.addField(name, input.getFieldValue(name));
	}

	output.setField("_version_", "-1"); // The document must not exist.
	output.setField(idField, outputId);
	output.setField(pathField, outputPath);
	output.setField(FieldNames.DEFAULT_PARENT_PATH_FIELD, outputPathParent);

	logger.info(String.format("Replacing path \"%s\" with \"%s\" and rehashing ID from \"%s\" to \"%s\".",
			inputPath, outputPath, inputId, outputId));
	client.add(output);
	client.deleteById(inputId);
}
 
Example 10
Source File: RealTimeGetComponent.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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 11
Source File: XMLWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * The SolrDocument should already have multivalued fields implemented as
 * Collections -- this will not rewrite to &lt;arr&gt;
 */
@Override
public void writeSolrDocument(String name, SolrDocument doc, ReturnFields returnFields, int idx ) throws IOException {
  startTag("doc", name, false);
  incLevel();

  for (String fname : doc.getFieldNames()) {
    if (returnFields!= null && !returnFields.wantsField(fname)) {
      continue;
    }

    Object val = doc.getFieldValue(fname);
    if( "_explain_".equals( fname ) ) {
      if (log.isDebugEnabled()) {
        log.debug(String.valueOf(val));
      }
    }
    writeVal(fname, val);
  }

  if(doc.hasChildDocuments()) {
    for(SolrDocument childDoc : doc.getChildDocuments()) {
      writeSolrDocument(null, childDoc, new SolrReturnFields(), idx);
    }
  }

  decLevel();
  writer.write("</doc>");
}
 
Example 12
Source File: SolrEntityProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String,Object> next() {
  SolrDocument solrDocument = solrDocumentIterator.next();
  
  HashMap<String,Object> map = new HashMap<>();
  Collection<String> fields = solrDocument.getFieldNames();
  for (String field : fields) {
    Object fieldValue = solrDocument.getFieldValue(field);
    map.put(field, fieldValue);
  }
  return map;
}
 
Example 13
Source File: CustomIndexLoader.java    From solr-autocomplete with Apache License 2.0 5 votes vote down vote up
private static SolrInputDocument fetchExistingOrCreateNewSolrDoc(SolrClient solr, String id) throws SolrServerException, IOException {
  countCreatedAcDocs++;

  if (!mergingWithOldDocsEnabled) {
    // if disabled, always use fresh document and override older docs with the same phrase
    return new SolrInputDocument();
  }
  if (id.equals("")) {
    return new SolrInputDocument();
  }
  
  Map<String, String> p = new HashMap<String, String>();
  p.put("q", PHRASE + ":\"" + ClientUtils.escapeQueryChars(id) + "\"");
 
  long t1 = System.currentTimeMillis();
  
  SolrParams params = new MapSolrParams(p);
  QueryResponse res = solr.query(params);
  
  totalSearchTime += (System.currentTimeMillis() - t1);
  
  if (res.getResults().size() == 0) {
    // System.out.println("Document for phrase " + id + " NOT FOUND");
    countDistinctNewDocs++;
    return new SolrInputDocument();
  } else if (res.getResults().size() == 1) {
    SolrDocument doc = res.getResults().get(0);
    SolrInputDocument tmp = new SolrInputDocument();
    
    // System.out.println("Document for phrase " + id + " found");
    
    for (String fieldName : doc.getFieldNames()) {
      tmp.addField(fieldName, doc.getFieldValue(fieldName));
    }
    return tmp;
  } else {
    throw new IllegalStateException("Query with params : " + p + " returned more than 1 hit!");
  }
}
 
Example 14
Source File: TestPutSolrContentStream.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that given SolrServer contains the expected SolrDocuments.
 */
private static void verifySolrDocuments(SolrClient solrServer, Collection<SolrDocument> expectedDocuments)
        throws IOException, SolrServerException {

    solrServer.commit();

    SolrQuery query = new SolrQuery("*:*");
    QueryResponse qResponse = solrServer.query(query);
    Assert.assertEquals(expectedDocuments.size(), qResponse.getResults().getNumFound());

    // verify documents have expected fields and values
    for (SolrDocument expectedDoc : expectedDocuments) {
        boolean found = false;
        for (SolrDocument solrDocument : qResponse.getResults()) {
            boolean foundAllFields = true;
            for (String expectedField : expectedDoc.getFieldNames()) {
                Object expectedVal = expectedDoc.getFirstValue(expectedField);
                Object actualVal = solrDocument.getFirstValue(expectedField);
                foundAllFields = expectedVal.equals(actualVal);
            }

            if (foundAllFields) {
                found = true;
                break;
            }
        }
        Assert.assertTrue("Could not find " + expectedDoc, found);
    }
}
 
Example 15
Source File: AlfrescoFieldMapperTransformer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void transform(SolrDocument doc, int docid, float score)
{
    Collection<String> fieldNames = new ArrayList<>(doc.getFieldNames());
    solrReturnFields = new SolrReturnFields(context.getRequest().getParams().get("originalFl"), context.getRequest());

    for (String fieldName : fieldNames)
    {
       SchemaField schemaField = context.getSearcher().getSchema().getFieldOrNull(fieldName);
       if(schemaField != null)
       {
           String alfrescoFieldName = AlfrescoSolrDataModel.getInstance().getAlfrescoPropertyFromSchemaField(fieldName);
           if (isRequestedField(alfrescoFieldName) || alfrescoFieldName.equals("id"))
           {
               Object value = doc.getFieldValue(fieldName);
               doc.removeFields(fieldName);
               if (schemaField.multiValued())
               {
                   Object collectionValue =
                           ((Collection<Object>) value).stream()
                                .map(elem -> getFieldValue(schemaField, elem))
                                .collect(Collectors.toSet());
                   doc.setField(alfrescoFieldName, collectionValue);
               }
               else
               {
                   doc.setField(transformToUnderscoreNotation(alfrescoFieldName), getFieldValue(schemaField, value));
               }
           }
           else
           {
               doc.removeFields(alfrescoFieldName);
               doc.removeFields(fieldName);
           }
       }
    }
}
 
Example 16
Source File: TestPutSolrContentStream.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that given SolrServer contains the expected SolrDocuments.
 */
private static void verifySolrDocuments(SolrClient solrServer, Collection<SolrDocument> expectedDocuments)
        throws IOException, SolrServerException {

    solrServer.commit();

    SolrQuery query = new SolrQuery("*:*");
    QueryResponse qResponse = solrServer.query(query);
    Assert.assertEquals(expectedDocuments.size(), qResponse.getResults().getNumFound());

    // verify documents have expected fields and values
    for (SolrDocument expectedDoc : expectedDocuments) {
        boolean found = false;
        for (SolrDocument solrDocument : qResponse.getResults()) {
            boolean foundAllFields = true;
            for (String expectedField : expectedDoc.getFieldNames()) {
                Object expectedVal = expectedDoc.getFirstValue(expectedField);
                Object actualVal = solrDocument.getFirstValue(expectedField);
                foundAllFields = expectedVal.equals(actualVal);
            }

            if (foundAllFields) {
                found = true;
                break;
            }
        }
        Assert.assertTrue("Could not find " + expectedDoc, found);
    }
}
 
Example 17
Source File: GetSolr.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public SolrInputDocument toSolrInputDocument(SolrDocument d) {
    SolrInputDocument doc = new SolrInputDocument();

    for (String name : d.getFieldNames()) {
        doc.addField(name, d.getFieldValue(name));
    }

    return doc;
}
 
Example 18
Source File: SolrControllerTest.java    From Spring-Boot-Book with Apache License 2.0 5 votes vote down vote up
@Test
public void getByIdFromSolr() throws IOException, SolrServerException {
    //根据id查询内容
    String id="8888888";
    SolrDocument solrDocument = solrClient.getById(id);
    //获取filedName
    Collection<String> fieldNames = solrDocument.getFieldNames();
    //获取file名和内容
    Map<String, Object> fieldValueMap = solrDocument.getFieldValueMap();
    List<SolrDocument> childDocuments = solrDocument.getChildDocuments();
    String results = solrDocument.toString();
    System.out.println(results);

}
 
Example 19
Source File: JSONWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void writeSolrDocument(String name, SolrDocument doc, ReturnFields returnFields, int idx) throws IOException {
  if( idx > 0 ) {
    writeArraySeparator();
  }

  indent();
  writeMapOpener(doc.size());
  incLevel();

  boolean first=true;
  for (String fname : doc.getFieldNames()) {
    if (returnFields!= null && !returnFields.wantsField(fname)) {
      continue;
    }

    if (first) {
      first=false;
    }
    else {
      writeMapSeparator();
    }

    indent();
    writeKey(fname, true);
    Object val = doc.getFieldValue(fname);
    writeVal(fname, val);
  }

  if(doc.hasChildDocuments()) {
    if(first == false) {
      writeMapSeparator();
      indent();
    }
    writeKey("_childDocuments_", true);
    writeArrayOpener(doc.getChildDocumentCount());
    List<SolrDocument> childDocs = doc.getChildDocuments();
    for(int i=0; i<childDocs.size(); i++) {
      writeSolrDocument(null, childDocs.get(i), null, i);
    }
    writeArrayCloser();
  }

  decLevel();
  writeMapCloser();
}
 
Example 20
Source File: GeoJSONResponseWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void writeSolrDocument(String name, SolrDocument doc, ReturnFields returnFields, int idx) throws IOException {
  if( idx > 0 ) {
    writeArraySeparator();
  }

  indent();
  writeMapOpener(-1); 
  incLevel();

  writeKey("type", false);
  writeVal(null, "Feature");
  
  Object val = doc.getFieldValue(geofield);
  if(val != null) {  
    writeFeatureGeometry(val);
  }
  
  boolean first=true;
  for (String fname : doc.getFieldNames()) {
    if (fname.equals(geofield) || ((returnFields!= null && !returnFields.wantsField(fname)))) {
      continue;
    }
    writeMapSeparator();
    if (first) {
      indent();
      writeKey("properties", false);
      writeMapOpener(-1); 
      incLevel();
      
      first=false;
    }

    indent();
    writeKey(fname, true);
    val = doc.getFieldValue(fname);
    writeVal(fname, val);
  }

  // GeoJSON does not really support nested FeatureCollections
  if(doc.hasChildDocuments()) {
    if(first == false) {
      writeMapSeparator();
      indent();
    }
    writeKey("_childDocuments_", true);
    writeArrayOpener(doc.getChildDocumentCount());
    List<SolrDocument> childDocs = doc.getChildDocuments();
    for(int i=0; i<childDocs.size(); i++) {
      writeSolrDocument(null, childDocs.get(i), null, i);
    }
    writeArrayCloser();
  }

  // check that we added any properties
  if(!first) {
    decLevel();
    writeMapCloser();
  }
  
  decLevel();
  writeMapCloser();
}