1.0.7 • Published 4 years ago

alto-tosolr v1.0.7

Weekly downloads
3
License
MIT
Repository
-
Last release
4 years ago

alto-solr converts filter array to solr string

The same fields are taken as OR, the rest are AND.

install

yarn add alto-tosolr

or

npm install alto-tosolr

usage

index.js

const {toSolrQuery} = require("alto-tosolr")

const queryFilter = [
    {
        "field": "locations",
        "value": "kocaeli"
    },
    {
        "field": "locations",
        "value": "bursa"
    },
    {
        "field": "locations",
        "value": "izmir"
    },
    {
        "field": "country",
        "value": "germany"
    },
    {
        "field": "ages",
        "value": "10-18"
    },
    {
        "field": "genders",
        "value": "female"
    },
    {
        "field": "platform",
        "value": "android"
    },
    {
        "field": "platform",
        "value": "ios"
    },
    {
        "field": "blogCount",
        "min": "123",
        "max": "1232"
    },
    {
        "field": "commentCount",
        "min": "123",
    },
    {
        "field": "likeCount",
        "max": "10",
    },
    {
        "field": "surname",
        "text": "aaadd"
    },
    {
        "field": "age",
        "number": "123"
    },
    {
        "field": "age",
        "number": "999"
    }
];

const queryString = toSolrQuery(queryFilter);
console.log(queryString)

result

(
    (content.data.locations:kocaeli OR content.data.locations:bursa OR content.data.locations:izmir)
    AND (content.data.country:germany)
    AND (content.data.ages:10-18)
    AND (content.data.genders:female)
    AND (content.data.platform:android OR content.data.platform:ios)
    AND (content.data.blogCount:[123 TO 1232])
    AND (content.data.commentCount:[123 TO *])
    AND (content.data.likeCount:[* TO 10])
    AND (content.data.surname:aaadd)
    AND (content.data.age:123 OR content.data.age:999)
)

middleware

if middleware function returns null; nothing happens.

const query = [
    {
        "field": "ages",
        "value": "10-18"
    },
    {
        "field": "platform",
        "value": "ios"
    },
    {
        "field": "game",
        "value": "fps"
    }
];

let res = toSolrQuery(query, (contentName, value) => {
    if (contentName === "ages") {
        const year2ms = y => y * 1000 * 60 * 60 * 8760;
        const [st, end] = value.split("-");
        return `content.data.birthdate : [${(new Date("2011-1-1").getTime() - year2ms(end))} TO ${(new Date("2011-1-1").getTime() - year2ms(st))}]`;
    }
    if (contentName === "game") {
        return `content.data.type:${value}`
    }
    return null;
});


console.log(res);

result

((content.data.birthdate : [726184800000 TO 978472800000]) AND (content.data.platform:ios) AND (content.data.type:fps))

usage of toSolrQueryArray

const complicated = [
    {"field": "os", "value": "ios"},
    {"field": "os", "value": "android"},
    {"field": "age", "value": "21-30"},
    {"field": "gender", "value": "female"},
    {"field": "gender", "value": "male"},
    {"field": "game", "value": "RPG"},
    {"field": "game", "value": "FPS"},
    {"field": "hashtag", "value": "#corona"},
    {"field": "age", "value": "41-50"}
];

const mapping = [
    {prefix: "relationName:device", q: "content.os"},
    {prefix: "collectionName:users", q: "content.gender"},
    {prefix: "collectionName:users", q: "content.birthdate"},
    {prefix: "relationName:swiped", q: "content.type"},
    {prefix: "relationName:userhashtag", q: "content.hashtag"},
];

let res = toSolrQueryArray(complicated, mapping, (contentName, value) => {
    if (contentName === "age") {
        const year2ms = y => y * 1000 * 60 * 60 * 8760;
        const [st, end] = value.split("-");
        return `content.birthdate : [${(new Date("2011-1-1").getTime() - year2ms(end))} TO ${(new Date("2011-1-1").getTime() - year2ms(st))}]`;
    }
    if (contentName === "game") {
        return `content.type:${value}`
    }
    if (contentName === "os") {
        return `content.os:${value}`
    }
    if (contentName === "gender") {
        return `content.gender:${value}`
    }
    if (contentName === "hashtag") {
        return `content.hashtag:${value}`
    }
    return null;
});

console.log(res);

result

[
    '(relationName:device AND (content.os:ios OR content.os:android))',
    '(collectionName:users AND (content.birthdate : [347752800000 TO 631576800000] OR content.birthdate : [-282967200000 TO 856800000]))',
    '(collectionName:users AND (content.gender:female OR content.gender:male))',
    '(relationName:swiped AND (content.type:RPG OR content.type:FPS))',
    '(relationName:userhashtag AND (content.hashtag:#corona))'
]
1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago