0% found this document useful (0 votes)
4 views

sarv

Uploaded by

dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

sarv

Uploaded by

dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

const { Client } = require("@elastic/elasticsearch");

const fs = require("fs").promises;

const client = new Client({


node: "https://api-master-fastdb-prod.sarv.com/",
auth: {
username: "ininc",
password: "g4hXhEQmTmA4",
},
maxRetries: 5,
requestTimeout: 60000,
});

const index = "in_organization";


let filters = [];
let lastFilterGroup = { must: [], should: [] };
let booleanOperator = "AND"; // Track the current boolean operator
let filterGroupStack = []; // Stack to manage nested filter groups

function addFilter(field, condition, values, operator = "AND") {


let filterClause;
switch (condition) {
case "exists":
filterClause = { exists: { field: field } };
break;
case "not exists":
filterClause = { bool: { must_not: { exists: { field: field } } } };
break;
case "is":
filterClause = { terms: { [field]: values } };
break;
case "is not":
filterClause = { bool: { must_not: { terms: { [field]: values } } } };
break;
case "is one of":
filterClause = { terms: { [field]: values } };
break;
case "is not one of":
filterClause = { bool: { must_not: { terms: { [field]: values } } } };
break;
case "is between":
filterClause = { range: { [field]: { gte: values[0], lte: values[1] } } };
break;
case "is not between":
filterClause = {
bool: {
should: [
{ range: { [field]: { lt: values[0] } } },
{ range: { [field]: { gt: values[1] } } },
],
},
};
break;
default:
console.error("Unknown filter condition:", condition);
return;
}

if (operator === "AND") {


lastFilterGroup.must.push(filterClause);
} else if (operator === "OR") {
lastFilterGroup.should.push(filterClause);
}
}

function joiningFunction(operator) {
if (operator === "AND" || operator === "OR") {
if (lastFilterGroup.should.length > 0) {
filters.push({ bool: { should: lastFilterGroup.should } });
}
if (lastFilterGroup.must.length > 0) {
filters.push({ bool: { must: lastFilterGroup.must } });
}
lastFilterGroup = { must: [], should: [] }; // Reset the filter group
booleanOperator = operator;
} else {
console.error("Unknown joining operator:", operator);
}
}

function nestingFunction(operator) {
if (operator === "AND" || operator === "OR") {
// Push the current filter group onto the stack
filterGroupStack.push({ group: lastFilterGroup, operator: booleanOperator });

// Start a new filter group for the nested filters


lastFilterGroup = { must: [], should: [] };
booleanOperator = operator;
} else {
console.error("Unknown nesting operator:", operator);
}
}

function endNesting() {
// Pop the last filter group from the stack
const { group, operator } = filterGroupStack.pop();
const nestedFilter = {
bool: {
[operator === "AND" ? "must" : "should"]: lastFilterGroup[operator ===
"AND" ? "must" : "should"],
},
};

// Return to the previous filter group and add the nested filter
if (booleanOperator === "AND") {
group.must.push(nestedFilter);
} else if (booleanOperator === "OR") {
group.should.push(nestedFilter);
}

// Set the lastFilterGroup to the previous one from the stack


lastFilterGroup = group;
}

async function search() {


const query = {
size: 10000,
query: {
bool: {
must: [...filters]
},
},
};

if (lastFilterGroup.should.length > 0) {
query.query.bool.must.push({ bool: { should: lastFilterGroup.should } });
}
if (lastFilterGroup.must.length > 0) {
query.query.bool.must.push({ bool: { must: lastFilterGroup.must } });
}

// Clear lastFilterGroup and filters array after building the query


lastFilterGroup = { must: [], should: [] };
filters = [];

await fs.writeFile("query.json", JSON.stringify(query, null, 2));


console.log("Written Query!");

try {
const objectArray = [];
const response = await client.search({
index: index,
scroll: "30m",
body: query,
});
let scrollId = response._scroll_id;
let hits = response.hits.hits;
while (hits.length) {
hits.forEach((doc) => {
objectArray.push(doc._source);
});

const scrollResponse = await client.scroll({


scroll_id: scrollId,
scroll: "30m",
});

scrollId = scrollResponse._scroll_id;
hits = scrollResponse.hits.hits;
}
return objectArray;
} catch (error) {
console.error("Error executing search:", error);
return [];
}
}

function clearFilters() {
filters = [];
lastFilterGroup = { must: [], should: [] };
filterGroupStack = [];
}

(async () => {
// Add filters
addFilter('cities', 'is', ['mumbai'], "OR");
addFilter('cities', 'is', ['bangalore'], 'OR');
joiningFunction("AND");
addFilter('followers', 'is', ['7'], "OR");
nestingFunction("OR");
addFilter('followers', 'is', ['10'], "AND");
addFilter('devId', 'is', ['65c30a240d213691fce25eb7'], "AND");
endNesting(); // End nesting
// Execute search
const results = await search();
await fs.writeFile("results.json", JSON.stringify(results, null, 2));
console.log("Search results written!");

// Clear filters
clearFilters();
})();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy