Skip to content

Improve query rule logic #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: feature/rule-based-filtering
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/queryrules.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Query = { "Life Cycle Flags": { "$in": ["Biennial"] } }
[MonarchsCheck]
Chip = Supports Monarchs
Keywords = Monarch Butterfly, Monarchs, Monarch, Monarch Waystation
Query = { "Pollinator Flags": { "$in": ["Monarch Butterfly"] } }
Query = { "Pollinator Flags": { "$in": ["Monarchs", "Larval Host (Monarch)"] } }

[SunExposure_PartShade]
Chip = Part Shade
Expand All @@ -39,8 +39,8 @@ Query = { "Sun Exposure Flags": { "$in": ["Part Shade"] } }

[SunExposure_FullSun]
Chip = Full Sun
Keywords = Full Sun, All day sun, sun
Query = { "Sun Exposure Flags": { "$in": ["Full Sun"] } }
Keywords = Full Sun, Sun, Direct Sun
Query = { "Sun Exposure Flags": { "$in": ["Sun"] } }

[SunExposure_Shade]
Chip = Shade
Expand Down Expand Up @@ -75,12 +75,12 @@ Query = { "Soil Moisture Flags": { "$in": ["Moist"] } }
[DroughtTolerance]
Chip = Drought Tolerant
Keywords = Drought Tolerant, Drought, tolerance of drought, droughts
Query = { "Plant Type Flags": { "$in": ["Drought Tolerant"] } }
Query = { "Drought Tolerance": { "$in": ["Medium", "High"] } }

[EvergreenCheck]
Chip = Evergreen Plants
Keywords = Evergreen, Evergreen Trees, Trees that are evergreen
Query = { "Plant Type Flags": { "$in": ["Evergreen"] } }
Query = { "Leaf Retention": "Evergreen" }

[Tree]
Chip = Tree
Expand All @@ -100,12 +100,12 @@ Query = { "Plant Type Flags": { "$in": ["Vine"] } }
[Fern]
Chip = Fern
Keywords = Fern
Query = { "Plant Type Flags": { "$in": ["Fern"] } }
Query = { "$or": [{ "Common Name": { "$regex": "fern", "$options": "i" } }, { "Scientific Name": { "$regex": "fern", "$options": "i" } }] }

[Grass]
Chip = Grass
Keywords = Grass, Grasses, Sedge, Sedges, Rush, Rushes
Query = { "Plant Type Flags": { "$in": ["Grass", "Sedge", "Rush"] } }
Query = { "Plant Family": "Poaceae" }

[Hummingbirds]
Chip = Attracts Hummingbirds
Expand All @@ -120,7 +120,7 @@ Query = { "Pollinator Flags": { "$in": ["Butterflies"] } }
[Bees]
Chip = Attracts Bees
Keywords = Bee, Bees, Bumblebee, Bumblebees, Honey Bee
Query = { "Pollinator Flags": { "$in": ["Bees"] } }
Query = { "Pollinator Flags": { "$in": ["Native Bees", "Honey Bees", "Bombus"] } }

[RedFlower]
Chip = Red Flowers
Expand Down
48 changes: 47 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ const version = fs.existsSync(versionFile)
? fs.readFileSync(versionFile, "utf8")
: "0";
const axios = require("axios");
const ini = require("ini");

// Load query rules from queryrules.ini at startup
const queryRulesPath = path.join(__dirname, "queryrules.ini");
let queryRules = {};
if (fs.existsSync(queryRulesPath)) {
const parsed = ini.parse(fs.readFileSync(queryRulesPath, "utf8"));
for (const [name, rule] of Object.entries(parsed)) {
try {
queryRules[name] = {
chip: rule.Chip,
keywords: String(rule.Keywords || "")
.split(/,\s*/)
.filter(Boolean),
query: JSON.parse(rule.Query),
};
} catch (e) {
console.error(`Failed to parse query rule ${name}:`, e);
}
}
}

// Disabled for now because it causes confusion when we update the data
// const cache = {};
Expand Down Expand Up @@ -59,11 +80,33 @@ module.exports = async function ({ plants, nurseries }) {
});
return res.send(response.data);
});

// Provide query rules to the frontend
app.get("/api/v1/queryrules", (req, res) => {
res.json(queryRules);
});
app.get("/api/v1/plants", async (req, res) => {
try {
const fetchResults = req.query.results !== "0";
const fetchTotal = req.query.total !== "0";
const query = {};
const ruleChips = [];
const $and = [];
// Accumulate filter and rule clauses
const rulesParam = req.query.rules;
if (rulesParam) {
const names = Array.isArray(rulesParam)
? rulesParam
: String(rulesParam).split(/,/);
const uniqueNames = [...new Set(names)];
for (const name of uniqueNames) {
const rule = queryRules[name];
if (rule) {
$and.push(rule.query);
ruleChips.push({ name, chip: rule.chip });
}
}
}
const sorts = {
"Sort by Common Name (A-Z)": {
"Common Name": 1,
Expand Down Expand Up @@ -231,7 +274,7 @@ module.exports = async function ({ plants, nurseries }) {
boolean: true,
},
];
const $and = [];
// $and array already initialized above
for (const filter of filters) {
if (filter.range) {
const min = parseInt(req.query?.[filter.name]?.min);
Expand Down Expand Up @@ -437,6 +480,9 @@ module.exports = async function ({ plants, nurseries }) {
);
}
}
if (ruleChips.length) {
response.ruleChips = ruleChips;
}
// setCache(req, response);
return res.send(response);
} catch (e) {
Expand Down
81 changes: 70 additions & 11 deletions src/components/Explorer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,9 @@ export default {
total: 0,
q: "",
activeSearch: "",
queryRules: {},
appliedRules: [],
ruleChips: [],
sort: "Sort by Recommendation Score",
filters,
componentKey: 0, // Add a key for forcing re-renders
Expand Down Expand Up @@ -913,7 +916,21 @@ export default {
return extras;
},
chips() {
return this.getChips(true);
const chips = this.getChips(true);
if (this.ruleChips.length) {
const seen = new Set();
for (const rc of this.ruleChips) {
if (seen.has(rc.name)) continue;
seen.add(rc.name);
chips.push({
name: rc.name,
label: rc.chip,
key: `rule:${rc.name}`,
svg: 'Search',
});
}
}
return chips;
},
flags() {
return this.getChips(false);
Expand Down Expand Up @@ -1024,6 +1041,15 @@ export default {
// Pick a random hero image after hydration to avoid SSR hydration mismatch
this.twoUpIndex = Math.floor(Math.random() * twoUpImageCredits.length);

// Fetch query rules used for keyword searches
try {
const resp = await fetch('/api/v1/queryrules');
this.queryRules = await resp.json();
} catch (e) {
console.error('Failed to fetch query rules', e);
this.queryRules = {};
}

this.displayLocation = localStorage.getItem("displayLocation") || "";
this.zipCode = localStorage.getItem("zipCode") || "";
this.manualZip = localStorage.getItem("manualZip") === "true";
Expand Down Expand Up @@ -1176,6 +1202,21 @@ export default {
this.manualZip = true;
localStorage.setItem("manualZip", "true")
},

detectRules() {
const matches = new Set();
if (!this.q || !this.queryRules) return [];
const qLower = this.q.toLowerCase();
for (const [name, rule] of Object.entries(this.queryRules)) {
for (const kw of rule.keywords) {
if (qLower.includes(kw.toLowerCase())) {
matches.add(name);
break;
}
}
}
return [...matches];
},
async getVendors() {
if (!this.selected) return [];
const data = {
Expand Down Expand Up @@ -1330,6 +1371,12 @@ export default {
clearTimeout(this.submitTimeout);
this.submitTimeout = null;
}
const detected = this.detectRules();
if (detected.length) {
this.appliedRules = detected;
} else {
this.appliedRules = [];
}
this.submitTimeout = setTimeout(submit.bind(this), 50); // Reduced timeout for faster response

function submit() {
Expand Down Expand Up @@ -1370,19 +1417,23 @@ export default {
this.updatingCounts = true;
const doUpdate = async () => {
try {
const params = {
...this.filterValues,
q: this.q,
sort: this.sort,
};
const params = {
...this.filterValues,
...(this.appliedRules.length ? {} : { q: this.q }),
sort: this.sort,
};
if (this.initializing) {
resolve();
return;
}
if (this.appliedRules.length) {
params.rules = this.appliedRules;
}
const response = await fetch("/api/v1/plants?" + qs.stringify(params));
const data = await response.json();
this.filterCounts = data.counts;
this.activeSearch = this.q;
this.activeSearch = this.appliedRules.length ? "" : this.q;
this.ruleChips = data.ruleChips || [];
} finally {
this.updatingCounts = false;
resolve();
Expand Down Expand Up @@ -1412,17 +1463,21 @@ export default {
}
: {
...this.filterValues,
q: this.q,
...(this.appliedRules.length ? {} : { q: this.q }),
sort: this.sort,
page: this.page,
};
this.activeSearch = this.q;
if (this.appliedRules.length) {
params.rules = this.appliedRules;
}
this.activeSearch = this.appliedRules.length ? "" : this.q;
if (this.initializing) {
// Don't send a bogus query for min 0 max 0
delete params["Height (feet)"];
}
const response = await fetch("/api/v1/plants?" + qs.stringify(params));
const data = await response.json();
this.ruleChips = data.ruleChips || [];
if (!this.favorites) {
this.filterCounts = data.counts;
for (const filter of this.filters) {
Expand Down Expand Up @@ -1476,12 +1531,15 @@ export default {
if (chip.name === "Search") {
this.q = "";
} else {
if (chip.key && chip.key.startsWith('rule:')) {
this.appliedRules = this.appliedRules.filter((n) => n !== chip.name);
}
const filter = this.filters.find((filter) => filter.name === chip.name);
if (filter.array) {
if (filter && filter.array) {
this.filterValues[chip.name] = this.filterValues[chip.name].filter(
(value) => value !== chip.label
);
} else {
} else if (filter) {
this.filterValues[chip.name] = filter.default;
}
}
Expand All @@ -1492,6 +1550,7 @@ export default {
this.filterValues[filter.name] = filter.default;
}
this.q = "";
this.appliedRules = [];
this.submit();
},
toggleSort() {
Expand Down