Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
alto-tosolr
Advanced tools
Readme
The same fields are taken as OR, the rest are AND.
yarn add alto-tosolr
or
npm install alto-tosolr
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
converts json array to solr query
The npm package alto-tosolr receives a total of 7 weekly downloads. As such, alto-tosolr popularity was classified as not popular.
We found that alto-tosolr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.