将文档拆分为多个的转换

时间:2018-03-03 11:46:20

标签: javascript node.js marklogic marklogic-8 marklogic-9

我发现通过MLCP执行此操作的唯一方法是,转换必须返回格式数组[{“uri”:“some_uri1”,“value”:“newly_created_doc1”},{“uri”:“some_uri2 “,”value“:”newly_created_doc2“}]。这种方式在使用内容泵进行转换后,将会创建这两个文档,比如说一些原始文档。我的问题是如何通过node.js api或java api实现相同的结果。例如,对于node.js,我可以应用转换,它将此文件创建为具有上述样式数组的单个文档。

我的转型:

const common = require("/ext/home/common.sjs");

function createDocs(content, context) {

    var bets = content.value.xpath('//selections').toArray();
    var documents = [];

    for (var bet in bets) {
        var bookie_name = "Boylesports";
        var sport_name = "Football";
        var event_name = bets[bet].xpath('../../../../name').toString();

        if (/^Over \d+\.\d+$/.test(event_name)) {
            event_name = event_name.replace(/^Over (\d+)\.\d+$/, 1);
        } else {
            event_name;
        }

        var subevent_name = bets[bet].xpath('../../../../name').toString();

        if (/^Over \d+\.\d+$/.test(subevent_name)) {
            subevent_name = subevent_name.replace(/^Over (\d+)\.\d+$/, 1);
        }
        else {
            subevent_name;
        }

        var subevent_id = bets[bet].xpath('../../../../idfoevent');
        var start_date = xdmp.parseDateTime("[Y0001]-[M01]-[D01]T[h01]:[m01]:[s01]", bets[bet].xpath('../../../../tsstart'));
        // var start_date = "2017-10-21T13:00:00Z";

        var market_name = bets[bet].xpath('../../name').toString();

        if (/^Handicap.+$/.test(market_name)) {
            market_name = market_name.replace(/^Handicap.+$/, "Handicaps");
        }
        if (/^Match Betting$/.test(market_name)) {
            market_name = market_name.replace(/^Match Betting$/, "Win Market");
        }
        else {
            market_name;
        }

        var market_id = bets[bet].xpath('../../idfomarket');
        var bet_name = bets[bet].xpath('name').toString();

        if (/^Aston Villa$/.test(bet_name)) {
            bet_name = bet_name.replace(/^Aston (Villa)$/, 1);
        }
        else {
            bet_name;
        }

        var bet_id = bets[bet].xpath('idfoselection');


        //Push to the list of documents
        var document = {};
        document.uri = common.convertToSlug(sport_name) + '/' + common.convertToSlug(event_name) + '/' + common.convertToSlug(subevent_name) + '/' + common.convertToSlug(market_name) + '/' + common.convertToSlug(bet_name);
        document.value = {
            'bookie_name': bookie_name,
            'sport_name': sport_name,
            'sport_slug': common.convertToSlug(sport_name),
            'event_name': event_name,
            'event_slug': common.convertToSlug(event_name),
            'subevent_name': subevent_name,
            'subevent_slug': common.convertToSlug(subevent_name),
            'subevent_id': subevent_id,
            'start_date': start_date,
            'market_name': market_name,
            'market_slug': common.convertToSlug(market_name),
            'market_id': market_id,
            'bet_name': bet_name,
            'bet_slug': common.convertToSlug(bet_name),
            'bet_id': bet_id
        };

        //Checks if objects with the same uri exist before pushing them
        if (documents.findIndex(x => x.uri == document.uri) === -1) documents.push(document);

        // documents.push(document);
    }
    return Sequence.from(documents);
}; 

exports.transform = createDocs;

我在node.js中的用法:

const axios = require("axios");
const db_client = require("../../database/connect").dbWriter;


axios.get("http://cache.boylesports.com/feeds/EUROFOOT2.json")
    .then(function (response) {
        console.log(response.data);

        var documents = [{uri: "http://cache.boylesports.com/feeds/EUROFOOT2.json",
                          content: response.data,
                          contentType: 'application/json',
                          collections: ["test"]}];



        db_client.documents.write({documents: documents, transform: ['js-example']}).result(
        function (response) {
            response.documents.forEach(function (document) {
                console.log('LOADED: ' + document.uri);
            });
        },
        function (error) {
            console.log(error);
        }
    );

    })
    .catch(function (error) {
        console.log(error);
    });

1 个答案:

答案 0 :(得分:0)

Client API依赖于REST转换,它不支持将同一种类型拆分为多个文档。我希望说你可以使用DMSDK,但看起来它依赖于同一种变换。这是一个在上传到数据库后转换文档的示例:

http://docs.marklogic.com/guide/java/data-movement#id_22702

看起来您不得不违反建议,并通过直接执行xdmp:document-insert调用来创建会导致副作用的转换。

HTH!