Socket
Socket
Sign inDemoInstall

alto-tosolr

Package Overview
Dependencies
68
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    alto-tosolr

converts json array to solr query


Version published
Weekly downloads
7
increased by133.33%
Maintainers
1
Install size
2.27 MB
Created
Weekly downloads
 

Readme

Source

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))'
]

FAQs

Last updated on 14 May 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc