Example usage for org.apache.solr.common SolrInputDocument addChildDocument

List of usage examples for org.apache.solr.common SolrInputDocument addChildDocument

Introduction

In this page you can find the example usage for org.apache.solr.common SolrInputDocument addChildDocument.

Prototype

@Override
    public void addChildDocument(SolrInputDocument child) 

Source Link

Usage

From source file:com.ifactory.press.db.solr.processor.UpdateDocValuesTest.java

License:Apache License

@Test
/** Child documents' values should be set when indexed */
public void testBlockIndex() throws Exception {
    insertTestDocuments(1, 1, false);//from w ww  .j a  va 2 s.  c  om

    // no doc values yet
    assertDocValue(uri(1), 0);
    assertDocValue(uriChild(1, 1), null);

    UpdateRequest req = updateDocValues();
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField(URI, uri(1));
    doc.addField(WEIGHT_DV, 3);
    req.add(doc);
    solr.request(req);
    solr.commit(false, true, true);

    // child doc dv still not set
    assertDocValue(uri(1), 3);
    assertDocValue(uriChild(1, 1), null);

    SolrInputDocument child = new SolrInputDocument();
    child.addField(URI, uriChild(1, 1));
    child.addField(WEIGHT_DV, 7);
    doc.addChildDocument(child);
    solr.request(req);
    solr.commit(false, true, true);

    // child doc dv updated
    assertDocValue(uri(1), 3);
    assertDocValue(uriChild(1, 1), 7);

    // re-add docs, preserving existing docvalues:
    insertTestDocuments(1, 1, true);
    assertDocValue(uri(1), 3);
    assertDocValue(uriChild(1, 1), 7);

}

From source file:com.ifactory.press.db.solr.processor.UpdateDocValuesTest.java

License:Apache License

private void insertTestDocuments(int n, int nkids, boolean preserveDV) throws Exception {
    UpdateRequest req = new UpdateRequest();
    req.setPath(UPDATE_DOCVALUES);// w ww . j  a va  2s .  c o  m
    if (preserveDV) {
        req.setParam(UpdateDocValuesProcessor.UPDATEDV_VALUE_FIELD, WEIGHT_DV);
    }
    for (int i = 1; i <= n; i++) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField(URI, uri(i));
        doc.addField(TEXT_FIELD, "This is document " + i);
        // NOTE: must provide a value for at least one document in order to create the field:
        // it's not enough to just put it in the solr schema
        if (!preserveDV) {
            doc.addField(WEIGHT_DV, 0);
        }
        for (int j = 1; j <= nkids; j++) {
            SolrInputDocument kid = new SolrInputDocument();
            kid.addField(URI, uriChild(i, j));
            kid.addField(TEXT_FIELD, "This is child document " + i + "/" + j);
            doc.addChildDocument(kid);
        }
        req.add(doc);
    }
    solr.request(req);
    solr.commit(false, true, true);
}

From source file:com.inqool.dcap.office.indexer.indexer.SolrBulkIndexer.java

License:Apache License

private SolrInputDocument recursivelyIndex(final ModelTreeNode data) throws IOException {
    ZdoModel model;// ww w . j a  v a  2s . c  o m

    model = data.getModel();

    if (model == null) {
        return null;
    }

    //        if (!model.isIndexable()) {
    //            logger.debug("Resource: {} retrieved without indexable type.", uri);
    //            return null;
    //        }
    logger.debug("Resource: {} retrieved with indexable type.", store.removeTransactionFromUrl(model.getUrl()));

    if (!allowableTypes.contains(model.get(ZdoTerms.zdoType))) {
        return null;
    }

    if (!ZdoGroup.ZDO.name().equals(model.get(ZdoTerms.group))) {
        logger.info("Not indexing this document as it is not published.");
        return null;
    }

    final SolrInputDocument inputDoc = modelToSolrInputDoc(model);

    //        inputDoc.addField("datePublished", OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.UTC).format(DateTimeFormatter.ISO_DATE_TIME));
    String datePublished = model.get(ZdoTerms.datePublished);
    if (datePublished != null) { //If reindexing, we just read data about when it was originally published from Fedora
        inputDoc.addField("datePublished", datePublished);
    } else {
        datePublished = LocalDateTime.now().atZone(ZoneOffset.systemDefault())
                .withZoneSameInstant(ZoneOffset.UTC).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        inputDoc.addField("datePublished", datePublished); //solr needs UTC time
        ZdoModel patchmodel = new ZdoModel();
        patchmodel.setUrl(model.getUrl());
        patchmodel.add(ZdoTerms.datePublished, datePublished);
        store.patchMetadata(patchmodel);
    }
    //Get all children's uris, parse them recursively, and add them to result
    //If we are an almost-leaf node, also search for children bound on the original object
    String originalObjectUrl = model.get(ZdoTerms.kdrObject);
    if (!ZdoType.isBranchEndCategory(model.get(ZdoTerms.zdoType))) {
        for (ModelTreeNode child : data.getChildren()) {
            SolrInputDocument childDoc = recursivelyIndex(child);
            if (childDoc != null) {
                inputDoc.addChildDocument(childDoc);
            }
        }
    } else { //we are end branch category
        //Treat born digital documents differently as they don't have pages but whole PDF
        if (ZdoType.bornDigital.name().equals(model.get(ZdoTerms.zdoType))) {
            //Retrieve the usercopy - PDF
            String queryString = "SELECT ?userCopy ?thumb WHERE {\n"
                    + "?userCopy <http://purl.org/dc/terms/isPartOf> <" + originalObjectUrl + ">.\n"
                    + "?userCopy <" + ZdoTerms.zdoType.getURI() + "> \"" + ZdoType.binary.name()
                    + "\"^^<http://www.w3.org/2001/XMLSchema#string>.\n" + "?userCopy <"
                    + ZdoTerms.fileType.getURI() + "> \"" + ZdoFileType.userCopy.name()
                    + "\"^^<http://www.w3.org/2001/XMLSchema#string>.\n" + "}";
            QueryExecution queryExecution = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, queryString);
            ResultSet resultSet = queryExecution.execSelect();
            if (resultSet.hasNext()) {
                QuerySolution querySolution = resultSet.next();
                String userCopyUrl = querySolution.getResource("userCopy").getURI();
                inputDoc.addField("pdfId", store.getOnlyIdFromUrl(userCopyUrl));
            } else {
                throw new RuntimeException("Damn this pdf has no pdf or thumbnail.");
            }
        } else { //Other than born-digital branch end node
            //These are to sort pages based on their index
            SortedMap<Integer, String> imageMap = new TreeMap<>();
            SortedMap<Integer, String> thumbMap = new TreeMap<>();
            SortedMap<Integer, String> txtMap = new TreeMap<>();
            SortedMap<Integer, String> altoMap = new TreeMap<>();

            String videoUrl = null;

            //Retrieve image, thumbnail and ocr text info
            String queryString = "SELECT ?pageIndex ?userCopy ?ucMime ?thumb ?txt ?alto WHERE {\n" +
            //first find pages - children of the node
                    "?page <" + ZdoTerms.zdoType.getURI() + "> \"" + ZdoType.page.name()
                    + "\"^^<http://www.w3.org/2001/XMLSchema#string>.\n"
                    + "?page <http://purl.org/dc/terms/isPartOf> <" + originalObjectUrl + ">.\n" + "?page <"
                    + ZdoTerms.pageIndex.getURI() + "> ?pageIndex.\n" + "OPTIONAL {\n" +
                    //then children of those pages that are binary usercopy images
                    "?userCopy <http://purl.org/dc/terms/isPartOf> ?page.\n" + "?userCopy <"
                    + ZdoTerms.zdoType.getURI() + "> \"" + ZdoType.binary.name()
                    + "\"^^<http://www.w3.org/2001/XMLSchema#string>.\n" + "?userCopy <"
                    + ZdoTerms.fileType.getURI() + "> \"" + ZdoFileType.userCopy.name()
                    + "\"^^<http://www.w3.org/2001/XMLSchema#string>.\n" + "?userCopy <"
                    + ZdoTerms.mimeType.getURI() + "> ?ucMime.\n" + "}\nOPTIONAL {\n" +
                    //and their thumbnails
                    "?thumb <http://purl.org/dc/terms/isPartOf> ?page.\n" + "?thumb <"
                    + ZdoTerms.zdoType.getURI() + "> \"" + ZdoType.binary.name()
                    + "\"^^<http://www.w3.org/2001/XMLSchema#string>.\n" + "?thumb <"
                    + ZdoTerms.fileType.getURI() + "> \"" + ZdoFileType.thumb.name()
                    + "\"^^<http://www.w3.org/2001/XMLSchema#string>.\n" + "}\nOPTIONAL {\n" +
                    //and also children of those pages that are binary text
                    "?txt <http://purl.org/dc/terms/isPartOf> ?page.\n" + "?txt <" + ZdoTerms.zdoType.getURI()
                    + "> \"" + ZdoType.binary.name() + "\"^^<http://www.w3.org/2001/XMLSchema#string>.\n"
                    + "?txt <" + ZdoTerms.fileType.getURI() + "> \"" + ZdoFileType.txt.name()
                    + "\"^^<http://www.w3.org/2001/XMLSchema#string>.\n" + "}\nOPTIONAL {\n" +
                    //and also alto children with ocr text
                    "?alto <http://purl.org/dc/terms/isPartOf> ?page.\n" + "?alto <" + ZdoTerms.zdoType.getURI()
                    + "> \"" + ZdoType.binary.name() + "\"^^<http://www.w3.org/2001/XMLSchema#string>.\n"
                    + "?alto <" + ZdoTerms.fileType.getURI() + "> \"" + ZdoFileType.alto.name()
                    + "\"^^<http://www.w3.org/2001/XMLSchema#string>.\n" + "}\n}";
            QueryExecution queryExecution = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, queryString);
            ResultSet resultSet = queryExecution.execSelect();
            while (resultSet.hasNext()) {
                QuerySolution querySolution = resultSet.next();
                Integer pageIndex = Integer.valueOf(querySolution.getLiteral("pageIndex").getString());
                Resource userCopyResource = querySolution.getResource("userCopy");
                if (userCopyResource != null) {
                    String userCopyUrl = userCopyResource.getURI();
                    if (userCopyUrl != null) {
                        if ("video/mp4".equals(querySolution.getLiteral("ucMime").getString())) {
                            if (videoUrl != null) {
                                logger.error(
                                        "More than one video per document encountered. There can only be one.");
                            }
                            videoUrl = userCopyUrl;
                        } else {
                            imageMap.put(pageIndex, userCopyUrl);
                        }
                    }
                }
                Resource thumbnailResource = querySolution.getResource("thumb");
                if (thumbnailResource != null) {
                    String thumbUrl = thumbnailResource.getURI();
                    if (thumbUrl != null) {
                        thumbMap.put(pageIndex, thumbUrl);
                    }
                }
                Resource txtResource = querySolution.getResource("txt");
                if (txtResource != null) {
                    String txtUrl = txtResource.getURI();
                    if (txtUrl != null) {
                        txtMap.put(pageIndex, txtUrl);
                    }
                }
                Resource altoResource = querySolution.getResource("alto");
                if (altoResource != null) {
                    String altoUrl = altoResource.getURI();
                    if (altoUrl != null) {
                        altoMap.put(pageIndex, altoUrl);
                    }
                }
            }

            if (videoUrl != null) {
                inputDoc.addField("videoId", store.getOnlyIdFromUrl(videoUrl));
            }

            List<String> imageIds = new ArrayList<>();
            if (!imageMap.isEmpty()) {
                for (String userCopyUrl : imageMap.values()) {
                    imageIds.add(store.getOnlyIdFromUrl(userCopyUrl));
                }
                inputDoc.addField("imageIds", imageIds);
            }

            if (!thumbMap.isEmpty()) {
                List<String> thumbIds = new ArrayList<>();
                for (String thumbUrl : thumbMap.values()) {
                    thumbIds.add(store.getOnlyIdFromUrl(thumbUrl));
                }
                inputDoc.addField("thumbIds", thumbIds);
            }

            List<String> txtIds = new ArrayList<>();
            if (!txtMap.isEmpty()) {
                String fulltext = "";
                for (String txtUrl : txtMap.values()) {
                    txtIds.add(store.getOnlyIdFromUrl(txtUrl));
                    InputStream in = new URL(txtUrl).openStream();
                    StringWriter writer = new StringWriter();
                    IOUtils.copy(in, writer, "utf-8");
                    String text = writer.toString();
                    fulltext += text + " ";
                }
                inputDoc.addField("fullText", fulltext.trim());
            }

            List<String> altoIds = new ArrayList<>();
            if (!altoMap.isEmpty()) {
                for (String altoUrl : altoMap.values()) {
                    altoIds.add(store.getOnlyIdFromUrl(altoUrl));
                }
            }

            ZdoModel kdrObject = store.get(model.get(ZdoTerms.kdrObject));
            String origPdfUrl = kdrObject.get(ZdoTerms.pdfUrl);
            String origEpubUrl = kdrObject.get(ZdoTerms.epubUrl);
            ZdoModel patchModel = new ZdoModel(); //Used to add new pdf and epub data to Fedora
            patchModel.setUrl(model.get(ZdoTerms.kdrObject));
            if ("true".equals(model.get(ZdoTerms.allowPdfExport)) && !imageIds.isEmpty()) {
                if (origPdfUrl == null) {
                    String pdfId = UUID.randomUUID().toString();
                    patchModel.add(ZdoTerms.pdfUrl, store.createUrl(pdfId));
                    String orgId = model.get(ZdoTerms.organization);

                    String watermarkId = null;
                    if ("true".equals(model.get(ZdoTerms.watermark))) {
                        watermarkId = organizationSettingsAccess.fetchOrgWatermark(orgId);
                        if (watermarkId == null) {
                            watermarkId = portalSettingsAccess.fetchPortalSettings().getWatermarkId();
                        }
                    }

                    PdfCreatorDto pdfCreatorDto = new PdfCreatorDto(pdfId, imageIds, altoIds, watermarkId,
                            model.get(ZdoTerms.watermarkPosition));
                    Response response = ClientBuilder.newClient().target(IP_ENDPOINT + "pdf").request()
                            .post(Entity.json(pdfCreatorDto));
                    if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
                        throw new RuntimeException("Failed to call pdf creator in image processing war.");
                    }
                    inputDoc.addField("pdfId", pdfId);
                } else { //When reindexing, pdf already exists
                    inputDoc.addField("pdfId", store.getOnlyIdFromUrl(origPdfUrl));
                }
            }
            if ("true".equals(model.get(ZdoTerms.allowEpubExport)) && !txtIds.isEmpty()) {
                if (origEpubUrl == null) {
                    String epubId = UUID.randomUUID().toString();
                    patchModel.add(ZdoTerms.epubUrl, store.createUrl(epubId));
                    epubCreator.createBook(epubId, model.get(DCTerms.title), model.get(DCTerms.creator),
                            txtIds);
                    inputDoc.addField("epubId", epubId);
                } else {
                    inputDoc.addField("epubId", store.getOnlyIdFromUrl(origEpubUrl));
                }
            }
            store.patchMetadata(patchModel); //warning, this does not go to triplestore
        }
    }

    logger.debug("Executing update of: {}...", store.removeTransactionFromUrl(model.getUrl()));

    return inputDoc;
}

From source file:com.nridge.ds.solr.SolrDS.java

License:Open Source License

private SolrInputDocument toSolrInputDocument(Document aDocument) throws NSException {
    Logger appLogger = mAppMgr.getLogger(this, "toSolrInputDocument");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    SolrInputDocument solrInputDocument = toSolrInputDocument(aDocument.getBag());
    if (mIncludeChildren) {
        ArrayList<Relationship> relationshipList = aDocument.getRelationships();
        for (Relationship relationship : relationshipList)
            solrInputDocument.addChildDocument(toSolrInputDocument(relationship.getBag()));
    }/*from  w  w  w  .j a  va 2 s  . com*/

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);

    return solrInputDocument;
}

From source file:com.riemersebastian.solrduplicate.SolrDuplicateTest.java

private void updateExistingParentDocWithChild() {
    LOGGER.info("Then, update the existing parent document, this time with a child document assigned ... ");
    SolrInputDocument parentDocUpdateing = new SolrInputDocument();
    parentDocUpdateing.addField("id", "parent_1");
    parentDocUpdateing.addField("name_s", "Sarah Connor");
    parentDocUpdateing.addField("blockJoinId", "1");

    SolrInputDocument childDoc = new SolrInputDocument();
    childDoc.addField("id", "child_1");
    childDoc.addField("name_s", "John Connor");
    childDoc.addField("blockJoinId", "1");

    parentDocUpdateing.addChildDocument(childDoc);

    try {/*  w  w w.j av a 2  s .  c o  m*/
        solrClient.add(parentDocUpdateing);
        solrClient.commit();
        LOGGER.info("... and commit it on the index.");
    } catch (SolrServerException | IOException ex) {
        LOGGER.log(Level.SEVERE, "Exception while updating parent doc!", ex);
    }
}

From source file:net.hasor.search.server.rsf.service.AbstractSearchService.java

License:Apache License

protected SolrInputDocument convetTo(SearchDocument searchDocument) {
    Set<Map.Entry<String, Object>> docDataEntrySet = searchDocument.entrySet();
    SolrInputDocument document = new SolrInputDocument();
    for (Map.Entry<String, Object> entry : docDataEntrySet) {
        document.setField(entry.getKey(), entry.getValue());
    }/*from w  w  w  . j av a 2 s .co m*/
    List<SearchDocument> searchList = searchDocument.getChildDocuments();
    if (searchList != null) {
        for (SearchDocument searchDoc : searchList) {
            document.addChildDocument(convetTo(searchDoc));
        }
    }
    return document;
}

From source file:org.apache.blur.slur.RowMutationHelperTest.java

License:Apache License

@Test
public void basicOneForOneConversion() {
    SolrInputDocument parent = new SolrInputDocument();
    parent.addField("rowid", "123");

    SolrInputDocument child = new SolrInputDocument();
    child.addField("recordid", "1");
    child.addField("fam.key", "value");

    parent.addChildDocument(child);

    RowMutation mutate = RowMutationHelper.from(parent, "foo");

    assertEquals("Should get our rowid back.", "123", mutate.getRowId());
    assertEquals("Should get a single record.", 1, mutate.getRecordMutationsSize());
    assertEquals("Tablename should be set", "foo", mutate.getTable());
}

From source file:org.apache.blur.slur.RowMutationHelperTest.java

License:Apache License

@Test
public void multivalueFieldsShouldTranslate() {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("rowid", "123");

    SolrInputDocument child = new SolrInputDocument();
    child.addField("recordid", "1");
    child.addField("fam.key", "value1");
    child.addField("fam.key", "value2");
    doc.addChildDocument(child);
    RowMutation mutate = RowMutationHelper.from(doc, "foo");

    List<Object> vals = getRecordValues("key", mutate);
    assertEquals("Should get both values back.", 2, vals.size());

    assertEquals("value1", vals.get(0).toString());
    assertEquals("value2", vals.get(1).toString());
}

From source file:org.apache.blur.slur.RowMutationHelperTest.java

License:Apache License

@Test(expected = IllegalArgumentException.class)
public void docWithChildrenCantItselfHaveFieldValues() {
    SolrInputDocument parent = new SolrInputDocument();
    parent.addField("id", "1");
    parent.addField("fam.key1", "123");
    SolrInputDocument child = new SolrInputDocument();
    parent.addChildDocument(child);

    RowMutationHelper.from(parent, "foo");
}

From source file:org.apache.blur.slur.RowMutationHelperTest.java

License:Apache License

@Test(expected = IllegalArgumentException.class)
public void docsMustUseTheNormalBlurFamilyColumnFormat() {
    SolrInputDocument parent = new SolrInputDocument();
    parent.addField("columnWithoutFamily", "123");
    SolrInputDocument child = new SolrInputDocument();
    parent.addChildDocument(child);

    RowMutationHelper.from(parent, "foo");
}