Skip to content
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

support AND, OR () in query #83

Merged
merged 2 commits into from
Apr 14, 2023
Merged
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
25 changes: 23 additions & 2 deletions redash-searcher-web/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { EuiHorizontalRule, EuiPageTemplate, EuiTitle } from "@elastic/eui";
import {
EuiCode,
EuiFlexGroup,
EuiFlexItem,
EuiHorizontalRule,
EuiPageTemplate,
EuiTitle,
EuiToolTip,
} from "@elastic/eui";
import { DateRangePicker } from "@algolia/react-instantsearch-widget-date-range-picker";
import React from "react";
import {
Expand Down Expand Up @@ -98,7 +106,20 @@ export default function App(props: InstantSearchProps) {
<RefinementList attribute="tags" searchable={true} limit={10} />
</EuiPageTemplate.Sidebar>
<EuiPageTemplate.Section>
<SearchBox />
<EuiFlexGroup gutterSize="s" alignItems="center">
<EuiFlexItem grow={true}>
<EuiToolTip
content=<span>
You can use <EuiCode>AND</EuiCode>, <EuiCode>OR</EuiCode>,
and parentheses <EuiCode>()</EuiCode> in your search query
to refine your search results. Separated by space,
<EuiCode>AND</EuiCode> is the default.
</span>
>
<SearchBox />
</EuiToolTip>
</EuiFlexItem>
</EuiFlexGroup>
<Stats />
<div style={{ margin: "1rem 0" }}>
<CurrentRefinements transformItems={reduceDuplicateRefinement} />
Expand Down
19 changes: 19 additions & 0 deletions redash-searcher-web/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// jest.config.mjs
import nextJest from "next/jest.js";

const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: "./",
});

// Add any custom config to be passed to Jest
/** @type {import('jest').Config} */
const config = {
// Add more setup options before each test is run
// setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],

testEnvironment: "jest-environment-jsdom",
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
export default createJestConfig(config);
13 changes: 10 additions & 3 deletions redash-searcher-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"test": "jest"
},
"dependencies": {
"@algolia/react-instantsearch-widget-date-range-picker": "1.1.2",
"@duetds/date-picker": "^1.4.0",
"@duetds/date-picker": "1.4.0",
"@elastic/datemath": "5.0.3",
"@elastic/eui": "76.4.0",
"@emotion/css": "11.10.6",
Expand All @@ -34,10 +35,16 @@
"typescript": "4.9.5"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@types/jest": "29.5.0",
"@types/node": "18.15.11",
"@types/qs": "6.9.7",
"@types/react": "18.0.35",
"@types/react-dom": "18.0.11",
"@types/react-instantsearch-dom": "6.12.3"
"@types/react-instantsearch-dom": "6.12.3",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"ts-jest": "29.1.0"
}
}
175 changes: 175 additions & 0 deletions redash-searcher-web/pages/api/search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// utils.test.ts
import { ElasticsearchQuery, SearchSettingsConfig } from "searchkit";
import { tokenizeSearchQuery, getQuery } from "./search";

describe("tokenizeSearchQuery", () => {
it("should tokenize a search query with operators and parentheses", () => {
const query = "foo AND (bar OR baz) AND qux";
const tokens = tokenizeSearchQuery(query);
expect(tokens).toEqual([
"foo",
"AND",
"(",
"bar",
"OR",
"baz",
")",
"AND",
"qux",
]);
});
it("should tokenize a search query without operators", () => {
const query = "foo bar baz";
const tokens = tokenizeSearchQuery(query);
expect(tokens).toEqual(["foo", "bar", "baz"]);
});
});

describe("getQuery", () => {
const searchAttributes = [
{ field: "field1", weight: 1 },
{ field: "field2", weight: 1 },
];

const config: SearchSettingsConfig = {
search_attributes: searchAttributes,
result_attributes: ["field1", "field2"],
};

test("foo AND bar OR baz", () => {
const query = "foo AND bar OR baz";
const result = getQuery(query, searchAttributes, config);

const expectedResult: ElasticsearchQuery = {
bool: {
should: [
{
bool: {
must: [
{
multi_match: {
query: "foo",
fields: ["field1", "field2"],
fuzziness: "AUTO:4,8",
},
},
{
multi_match: {
query: "bar",
fields: ["field1", "field2"],
fuzziness: "AUTO:4,8",
},
},
],
},
},
{
bool: {
must: [
{
multi_match: {
query: "baz",
fields: ["field1", "field2"],
fuzziness: "AUTO:4,8",
},
},
],
},
},
],
minimum_should_match: 1,
},
};

expect(result).toEqual(expectedResult);
});

test("foo AND (bar OR baz)", () => {
const query = "foo AND (bar OR baz)";
const result = getQuery(query, searchAttributes, config);

const expectedResult: ElasticsearchQuery = {
bool: {
must: [
{
multi_match: {
query: "foo",
fields: ["field1", "field2"],
fuzziness: "AUTO:4,8",
},
},
{
bool: {
should: [
{
bool: {
must: [
{
multi_match: {
query: "bar",
fields: ["field1", "field2"],
fuzziness: "AUTO:4,8",
},
},
],
},
},
{
bool: {
must: [
{
multi_match: {
query: "baz",
fields: ["field1", "field2"],
fuzziness: "AUTO:4,8",
},
},
],
},
},
],
minimum_should_match: 1,
},
},
],
},
};

expect(result).toEqual(expectedResult);
});

test("foo bar baz", () => {
const query = "foo bar baz";
const result = getQuery(query, searchAttributes, config);

const expectedResult: ElasticsearchQuery = {
bool: {
must: [
{
multi_match: {
query: "foo",
fields: ["field1", "field2"],
fuzziness: "AUTO:4,8",
},
},
{
multi_match: {
query: "bar",
fields: ["field1", "field2"],
fuzziness: "AUTO:4,8",
},
},
{
multi_match: {
query: "baz",
fields: ["field1", "field2"],
fuzziness: "AUTO:4,8",
},
},
],
},
};

expect(result).toEqual(expectedResult);
});
});
Loading