Skip to content

Commit

Permalink
[SearchBar] Added support for emitting a Query as an Elasticsearch qu…
Browse files Browse the repository at this point in the history
…ery string (#598)

Example:

```
const query = Query.parse(`john group:(eng or "marketing org") -date:'2004-03' -is:active`);
console.log(Query.toEsQueryString(query));
```

the above logs:

```
+john +(group:eng OR group:"marketing org") -date:(>=2004-03 AND <2004-04) +active:false
```
  • Loading branch information
uboness authored Mar 30, 2018
1 parent 1309398 commit a8df982
Show file tree
Hide file tree
Showing 10 changed files with 347 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Relaxed query syntax of `EuiSearchBar` to allow usage of hyphens without escaping ([#581](https://github.com/elastic/eui/pull/581))
- Added support for range queries in `EuiSearchBar` (works for numeric and date values) ([#485](https://github.com/elastic/eui/pull/485))
- Added support for emitting a `EuiSearchBar` query to an Elasticsearch query string ([#598](https://github.com/elastic/eui/pull/598))
- Add support for expandable rows to `EuiBasicTable` ([#585](https://github.com/elastic/eui/pull/585))

# [`0.0.35`](https://github.com/elastic/eui/tree/v0.0.35)
Expand Down
25 changes: 21 additions & 4 deletions src-docs/src/views/search_bar/search_bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ export class SearchBar extends Component {
stars: {
type: 'number'
},
created: {
type: 'date'
},
owner: {
type: 'string'
},
tag: {
type: 'string',
validate: (value) => {
Expand Down Expand Up @@ -251,20 +257,31 @@ export class SearchBar extends Component {
query,
} = this.state;

const esQuery = EuiSearchBar.Query.toESQuery(query);
const esQueryDsl = EuiSearchBar.Query.toESQuery(query);
const esQueryString = EuiSearchBar.Query.toESQueryString(query);

const content = this.renderError() || (
<EuiFlexGroup>
<EuiFlexItem grow={4}>

<EuiTitle size="s">
<h3>Elasticsearch query</h3>
<h3>Elasticsearch Query String</h3>
</EuiTitle>

<EuiSpacer size="s"/>
<EuiCodeBlock language="js">
{esQueryString ? esQueryString : ''}
</EuiCodeBlock>

<EuiSpacer size="l"/>

<EuiTitle size="s">
<h3>Elasticsearch Query DSL</h3>
</EuiTitle>`
<EuiSpacer size="s"/>
<EuiCodeBlock language="js">
{esQuery ? JSON.stringify(esQuery, null, 2) : ''}
{esQueryDsl ? JSON.stringify(esQueryDsl, null, 2) : ''}
</EuiCodeBlock>

</EuiFlexItem>

<EuiFlexItem grow={6}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`astToEs ast - '' 1`] = `
exports[`astToEsQueryDsl ast - '' 1`] = `
Object {
"match_all": Object {},
}
`;

exports[`astToEs ast - '-group:es group:kibana -group:beats group:logstash' 1`] = `
exports[`astToEsQueryDsl ast - '-group:es group:kibana -group:beats group:logstash' 1`] = `
Object {
"bool": Object {
"must": Array [
Expand All @@ -33,7 +33,7 @@ Object {
}
`;

exports[`astToEs ast - 'is:online group:kibana john' 1`] = `
exports[`astToEsQueryDsl ast - 'is:online group:kibana john' 1`] = `
Object {
"bool": Object {
"must": Array [
Expand All @@ -60,7 +60,7 @@ Object {
}
`;

exports[`astToEs ast - 'john -doe is:online group:eng group:es -group:kibana -is:active' 1`] = `
exports[`astToEsQueryDsl ast - 'john -doe is:online group:eng group:es -group:kibana -is:active' 1`] = `
Object {
"bool": Object {
"must": Array [
Expand Down Expand Up @@ -107,7 +107,7 @@ Object {
}
`;

exports[`astToEs ast - 'john -sales' 1`] = `
exports[`astToEsQueryDsl ast - 'john -sales' 1`] = `
Object {
"bool": Object {
"must": Array [
Expand All @@ -128,7 +128,7 @@ Object {
}
`;

exports[`astToEs ast - 'john group:(eng or "marketing org") -group:"kibana team" 1`] = `
exports[`astToEsQueryDsl ast - 'john group:(eng or "marketing org") -group:"kibana team" 1`] = `
Object {
"bool": Object {
"must": Array [
Expand Down Expand Up @@ -168,7 +168,7 @@ Object {
}
`;

exports[`astToEs ast - 'john group:(eng or es) -group:kibana' 1`] = `
exports[`astToEsQueryDsl ast - 'john group:(eng or es) -group:kibana' 1`] = `
Object {
"bool": Object {
"must": Array [
Expand Down Expand Up @@ -200,7 +200,7 @@ Object {
}
`;

exports[`astToEs ast - -count<=4 size<5 age>=3 -number>9 1`] = `
exports[`astToEsQueryDsl ast - -count<=4 size<5 age>=3 -number>9 1`] = `
Object {
"bool": Object {
"must": Array [
Expand Down Expand Up @@ -239,7 +239,7 @@ Object {
}
`;

exports[`astToEs ast - count>3 1`] = `
exports[`astToEsQueryDsl ast - count>3 1`] = `
Object {
"bool": Object {
"must": Array [
Expand All @@ -255,7 +255,7 @@ Object {
}
`;

exports[`astToEs ast - date:'2004-03' -date<'2004-03-10' 1`] = `
exports[`astToEsQueryDsl ast - date:'2004-03' -date<'2004-03-10' 1`] = `
Object {
"bool": Object {
"must": Array [
Expand All @@ -278,7 +278,7 @@ Object {
}
`;

exports[`astToEs ast - date>'2004-02' -otherDate>='2004-03-10' 1`] = `
exports[`astToEsQueryDsl ast - date>'2004-02' -otherDate>='2004-03-10' 1`] = `
Object {
"bool": Object {
"must": Array [
Expand All @@ -303,7 +303,7 @@ Object {
}
`;

exports[`astToEs ast - date>='2004-03-22' 1`] = `
exports[`astToEsQueryDsl ast - date>='2004-03-22' 1`] = `
Object {
"bool": Object {
"must": Array [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`astToEsQueryString ast - '' 1`] = `""`;

exports[`astToEsQueryString ast - '-group:es group:kibana -group:beats group:logstash' 1`] = `"-group:es +group:kibana -group:beats +group:logstash"`;

exports[`astToEsQueryString ast - 'is:online group:kibana john' 1`] = `"+online:true +group:kibana +john"`;

exports[`astToEsQueryString ast - 'john -doe is:online group:eng group:es -group:kibana -is:active' 1`] = `"+john -doe +online:true +group:eng +group:es -group:kibana +active:false"`;

exports[`astToEsQueryString ast - 'john -sales' 1`] = `"+john -sales"`;

exports[`astToEsQueryString ast - 'john group:(eng or "marketing org") -group:"kibana team" 1`] = `"+john +(group:eng OR group:\\"marketing org\\") -group:\\"kibana team\\""`;

exports[`astToEsQueryString ast - 'john group:(eng or es) -group:kibana' 1`] = `"+john +(group:eng OR group:es) -group:kibana"`;

exports[`astToEsQueryString ast - -count<=4 size<5 age>=3 -number>9 1`] = `"-count:<=4 +size:<5 +age:>=3 -number:>9"`;

exports[`astToEsQueryString ast - count>3 1`] = `"+count:>3"`;

exports[`astToEsQueryString ast - date:'2004-03' -date<'2004-03-10' 1`] = `"+date:(>=2004-03 AND <2004-04) -date:<2004-03-10"`;

exports[`astToEsQueryString ast - date>'2004-02' -otherDate>='2004-03-10' 1`] = `"+date:>=2004-03 -date:>=2004-03-10"`;

exports[`astToEsQueryString ast - date>='2004-03-22' 1`] = `"+date:>=2004-03-22"`;
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const collectFields = (ast) => {
});
};

export const astToEs = (ast, options = {}) => {
export const astToEsQueryDsl = (ast, options = {}) => {

if (ast.clauses.length === 0) {
return { match_all: {} };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { astToEs } from './ast_to_es';
import { AST } from './ast';
import moment from 'moment/moment';
import { dateValue } from './date_value';
import { Granularity } from './date_format';
import { astToEsQueryDsl } from './ast_to_es_query_dsl';

describe('astToEs', () => {
describe('astToEsQueryDsl', () => {

test(`ast - ''`, () => {
const query = astToEs(AST.create([]));
const query = astToEsQueryDsl(AST.create([]));
expect(query).toMatchSnapshot();
});

test(`ast - 'john -sales'`, () => {
const query = astToEs(AST.create([
const query = astToEsQueryDsl(AST.create([
AST.Term.must('john'),
AST.Term.mustNot('sales'),
]));
expect(query).toMatchSnapshot();
});

test(`ast - '-group:es group:kibana -group:beats group:logstash'`, () => {
const query = astToEs(AST.create([
const query = astToEsQueryDsl(AST.create([
AST.Field.mustNot.eq('group', 'es'),
AST.Field.must.eq('group', 'kibana'),
AST.Field.mustNot.eq('group', 'beats'),
Expand All @@ -30,7 +30,7 @@ describe('astToEs', () => {
});

test(`ast - 'is:online group:kibana john'`, () => {
const query = astToEs(AST.create([
const query = astToEsQueryDsl(AST.create([
AST.Is.must('online'),
AST.Field.must.eq('group', 'kibana'),
AST.Term.must('john')
Expand All @@ -39,7 +39,7 @@ describe('astToEs', () => {
});

test(`ast - 'john -doe is:online group:eng group:es -group:kibana -is:active'`, () => {
const query = astToEs(AST.create([
const query = astToEsQueryDsl(AST.create([
AST.Term.must('john'),
AST.Term.mustNot('doe'),
AST.Is.must('online'),
Expand All @@ -52,7 +52,7 @@ describe('astToEs', () => {
});

test(`ast - 'john group:(eng or es) -group:kibana'`, () => {
const query = astToEs(AST.create([
const query = astToEsQueryDsl(AST.create([
AST.Term.must('john'),
AST.Field.must.eq('group', ['eng', 'es']),
AST.Field.mustNot.eq('group', 'kibana')
Expand All @@ -61,7 +61,7 @@ describe('astToEs', () => {
});

test(`ast - 'john group:(eng or "marketing org") -group:"kibana team"`, () => {
const query = astToEs(AST.create([
const query = astToEsQueryDsl(AST.create([
AST.Term.must('john'),
AST.Field.must.eq('group', ['eng', 'marketing org']),
AST.Field.mustNot.eq('group', 'kibana team')
Expand All @@ -70,14 +70,14 @@ describe('astToEs', () => {
});

test(`ast - count>3`, () => {
const query = astToEs(AST.create([
const query = astToEsQueryDsl(AST.create([
AST.Field.must.gt('count', 3)
]));
expect(query).toMatchSnapshot();
});

test(`ast - -count<=4 size<5 age>=3 -number>9`, () => {
const query = astToEs(AST.create([
const query = astToEsQueryDsl(AST.create([
AST.Field.mustNot.lte('count', 4),
AST.Field.must.lt('size', 5),
AST.Field.must.gte('age', 3),
Expand All @@ -87,22 +87,22 @@ describe('astToEs', () => {
});

test(`ast - date>='2004-03-22'`, () => {
const query = astToEs(AST.create([
const query = astToEsQueryDsl(AST.create([
AST.Field.must.gte('date', dateValue(moment.utc('2004-03-22'), Granularity.DAY))
]));
expect(query).toMatchSnapshot();
});

test(`ast - date:'2004-03' -date<'2004-03-10'`, () => {
const query = astToEs(AST.create([
const query = astToEsQueryDsl(AST.create([
AST.Field.must.eq('date', dateValue(moment.utc('2004-03'), Granularity.MONTH)),
AST.Field.mustNot.lt('date', dateValue(moment.utc('2004-03-10'), Granularity.DAY))
]));
expect(query).toMatchSnapshot();
});

test(`ast - date>'2004-02' -otherDate>='2004-03-10'`, () => {
const query = astToEs(AST.create([
const query = astToEsQueryDsl(AST.create([
AST.Field.must.gt('date', dateValue(moment.utc('2004-02'), Granularity.MONTH)),
AST.Field.mustNot.gte('date', dateValue(moment.utc('2004-03-10'), Granularity.DAY))
]));
Expand Down
Loading

0 comments on commit a8df982

Please sign in to comment.