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

The following examples show how to use org.apache.solr.common.SolrInputDocument#removeField() . 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: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a basic SolrInputDocument from the input metadata and with the given type (docType).
 * Note the supplier argument could provide a full or {@link PartialSolrInputDocument} document instance.
 *
 * @param metadata the source node metadata.
 * @param docType the document type.
 * @param initialEmptyDocumentSupplier a factory for creating the initial {@link SolrInputDocument} instance.
 * @return a basic {@link SolrInputDocument} instance populated with the minimal set of information.
 */
private SolrInputDocument basicDocument(NodeMetaData metadata, String docType, Supplier<SolrInputDocument> initialEmptyDocumentSupplier)
{
    SolrInputDocument doc = initialEmptyDocumentSupplier.get();
    doc.setField(FIELD_SOLR4_ID,
            AlfrescoSolrDataModel.getNodeDocumentId(
                    metadata.getTenantDomain(),
                    metadata.getId()));
    doc.setField(FIELD_VERSION, 0);

    // Here is used add in order to make sure that the atomic update happens
    doc.removeField(FIELD_DBID);
    doc.addField(FIELD_DBID, metadata.getId());

    doc.setField(FIELD_LID, metadata.getNodeRef().toString());
    doc.setField(FIELD_INTXID, metadata.getTxnId());
    doc.setField(FIELD_DOC_TYPE, docType);
    doc.setField(FIELD_ACLID, metadata.getAclId());
    return doc;
}
 
Example 2
Source File: AlfrescoSolrUtils.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void addContentPropertyToDoc(SolrInputDocument cachedDoc,
        QName propertyQName,
        String locale,
        Map<QName, String> content)
{
    StringBuilder builder = new StringBuilder();
    builder.append("\u0000").append(locale).append("\u0000");
    builder.append(content.get(propertyQName));

    for (AlfrescoSolrDataModel.FieldInstance field : AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(propertyQName).getFields())
    {
        cachedDoc.removeField(field.getField());
        if(field.isLocalised())
        {
            cachedDoc.addField(field.getField(), builder.toString());
        }
        else
        {
            cachedDoc.addField(field.getField(), content.get(propertyQName));
        }
    }
}
 
Example 3
Source File: SolrExampleTests.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetNullUpdates() throws Exception {
  SolrClient solrClient = getSolrClient();
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", "testSetNullUpdates");
  doc.addField("single_s", "test-value");
  doc.addField("multi_ss", Arrays.asList("first", "second"));
  solrClient.add(doc);
  solrClient.commit(true, true);
  doc.removeField("single_s");
  doc.removeField("multi_ss");
  Map<String, Object> map = Maps.newHashMap();
  map.put("set", null);
  doc.addField("multi_ss", map);
  solrClient.add(doc);
  solrClient.commit(true, true);
  QueryResponse response = solrClient.query(new SolrQuery("id:testSetNullUpdates"));
  assertNotNull("Entire doc was replaced because null update was not written", response.getResults().get(0).getFieldValue("single_s"));
  assertNull("Null update failed. Value still exists in document", response.getResults().get(0).getFieldValue("multi_ss"));
}
 
Example 4
Source File: NestedAtomicUpdateTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testMergeChildDoc() throws Exception {
  SolrInputDocument newChildDoc = sdoc("id", "3", "cat_ss", "child");
  SolrInputDocument addedDoc = sdoc("id", "1",
      "cat_ss", Collections.singletonMap("add", "bbb"),
      "child", Collections.singletonMap("add", sdocs(newChildDoc)));

  SolrInputDocument dummyBlock = sdoc("id", "1",
      "cat_ss", new ArrayList<>(Arrays.asList("aaa", "ccc")),
      "_root_", "1", "child", new ArrayList<>(sdocs(addedDoc)));
  dummyBlock.removeField(VERSION);

  SolrInputDocument preMergeDoc = new SolrInputDocument(dummyBlock);
  AtomicUpdateDocumentMerger docMerger = new AtomicUpdateDocumentMerger(req());
  docMerger.merge(addedDoc, dummyBlock);
  assertEquals("merged document should have the same id", preMergeDoc.getFieldValue("id"), dummyBlock.getFieldValue("id"));
  assertDocContainsSubset(preMergeDoc, dummyBlock);
  assertDocContainsSubset(addedDoc, dummyBlock);
  assertDocContainsSubset(newChildDoc, (SolrInputDocument) ((List) dummyBlock.getFieldValues("child")).get(1));
  assertEquals(dummyBlock.getFieldValue("id"), dummyBlock.getFieldValue("id"));
}
 
Example 5
Source File: DocumentShrinker.java    From thoth with BSD 3-Clause Clear License 6 votes vote down vote up
/**
 * Tag slower documents and add them to the shrank core
 */
private void tagAndAddSlowThothDocuments() throws IOException, SolrServerException {
  // Query to return top MAX_NUMBER_SLOW_THOTH_DOCS slower thoth documents
  QueryResponse qr = realTimeServer.query(
      new SolrQuery()
          .setQuery(createThothDocsAggregationQuery())
          .addSort(QTIME, SolrQuery.ORDER.desc)
          .setRows(MAX_NUMBER_SLOW_THOTH_DOCS)
  );

  for (SolrDocument solrDocument: qr.getResults()){
    SolrInputDocument si = ClientUtils.toSolrInputDocument(solrDocument);
    // Remove old ID and version
    si.removeField(ID);
    si.removeField("_version_");
    // Tag document as slow
    si.addField(SLOW_QUERY_DOCUMENT, true);
    LOG.debug("Adding slow query document for server " + serverDetail.getName());
    shrankServer.add(si);
  }
}
 
Example 6
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void updateAncestorRelatedFields(NodeMetaData nodeMetaData, SolrInputDocument doc)
{
    doc.removeField(FIELD_ANCESTOR);
    notNullOrEmpty(nodeMetaData.getAncestors())
            .stream()
            .map(Object::toString)
            .forEach(ancestor -> doc.addField(FIELD_ANCESTOR, ancestor));
}
 
Example 7
Source File: SolrExampleTests.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked"})
public void testUpdateMultiValuedField() throws Exception {
  SolrClient solrClient = getSolrClient();
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", "123");
  solrClient.add(doc);
  solrClient.commit(true, true);
  QueryResponse response = solrClient.query(new SolrQuery("id:123"));
  assertEquals("Failed to add doc to cloud server", 1, response.getResults().getNumFound());

  Map<String, List<String>> operation = new HashMap<>();
  operation.put("set", Arrays.asList("first", "second", "third"));
  doc.addField("multi_ss", operation);
  solrClient.add(doc);
  solrClient.commit(true, true);
  response = solrClient.query(new SolrQuery("id:123"));
  assertTrue("Multi-valued field did not return a collection", response.getResults().get(0).get("multi_ss") instanceof List);
  List<String> values = (List<String>) response.getResults().get(0).get("multi_ss");
  assertEquals("Field values was not updated with all values via atomic update", 3, values.size());

  operation.clear();
  operation.put("add", Arrays.asList("fourth", "fifth"));
  doc.removeField("multi_ss");
  doc.addField("multi_ss", operation);
  solrClient.add(doc);
  solrClient.commit(true, true);
  response = solrClient.query(new SolrQuery("id:123"));
  values = (List<String>) response.getResults().get(0).get("multi_ss");
  assertEquals("Field values was not updated with all values via atomic update", 5, values.size());
}
 
Example 8
Source File: MtasUpdateRequestProcessorFactory.java    From mtas with Apache License 2.0 5 votes vote down vote up
private void removeFields(SolrInputDocument doc, String fieldNames) {
  if (fieldNames != null) {
    String[] tmpFields = fieldNames.split(",");
    for (int i = 0; i < tmpFields.length; i++) {
      doc.removeField(tmpFields[i]);
    }
  }
}
 
Example 9
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void updateContent(TenantDbId docRef) throws Exception
{
    LOGGER.debug("Text content of Document DBID={} is going to be updated.", docRef.dbId);

    UpdateRequestProcessor processor = null;
    try (SolrQueryRequest request = newSolrQueryRequest())
    {
        processor = this.core.getUpdateProcessingChain(null).createProcessor(request, newSolrQueryResponse());

        SolrInputDocument doc = new PartialSolrInputDocument();
        doc.removeField(FIELD_DBID);
        doc.addField(FIELD_DBID, docRef.dbId);
        doc.setField(FIELD_SOLR4_ID,
                AlfrescoSolrDataModel.getNodeDocumentId(
                        docRef.tenant,
                        docRef.dbId));

        if (docRef.optionalBag.containsKey(CONTENT_LOCALE_FIELD))
        {
            addContentToDoc(docRef, doc, docRef.dbId);
        }

        LOGGER.debug("Text content of Document DBID={} has been updated (not yet indexed)", docRef.dbId);

        final Long latestAppliedVersionId =
                    ofNullable(docRef.optionalBag.get(LATEST_APPLIED_CONTENT_VERSION_ID))
                            .map(String.class::cast)
                            .map(Long::parseLong)
                            .orElse(CONTENT_UPDATED_MARKER);

        markAsContentInSynch(doc, latestAppliedVersionId);

        // Add to index
        AddUpdateCommand addDocCmd = new AddUpdateCommand(request);
        addDocCmd.overwrite = true;
        addDocCmd.solrDoc = doc;

        processor.processAdd(addDocCmd);

        LOGGER.debug(
                "Text content of Document DBID={} has been marked as updated (latest content version ID = {})",
                docRef.dbId,
                (latestAppliedVersionId == CONTENT_UPDATED_MARKER ? "N.A." : latestAppliedVersionId));
    }
    catch (Exception exception)
    {
        LOGGER.error("Unable to update the text content of node {}. See the stacktrace below for further details.", docRef.dbId, exception);
    }
    finally
    {
        if(processor != null) {processor.finish();}
    }
}
 
Example 10
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);

}