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

[Maps] create NOT EXISTS filter for tooltip property with no value #62849

Merged
merged 3 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
105 changes: 105 additions & 0 deletions x-pack/plugins/maps/public/layers/tooltips/es_tooltip_property.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
nreese marked this conversation as resolved.
Show resolved Hide resolved
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { IFieldType, IndexPattern } from '../../../../../../src/plugins/data/public';
import { ESTooltipProperty } from './es_tooltip_property';
import { TooltipProperty } from './tooltip_property';
import { AbstractField, IField } from '../fields/field';
import { FIELD_ORIGIN } from '../../../common/constants';

class MockField extends AbstractField implements IField {}
nreese marked this conversation as resolved.
Show resolved Hide resolved

const indexPatternField = {
name: 'machine.os',
type: 'string',
esTypes: ['text'],
count: 0,
scripted: false,
searchable: true,
aggregatable: true,
readFromDocValues: false,
} as IFieldType;

const featurePropertyField = new MockField({
fieldName: 'machine.os',
origin: FIELD_ORIGIN.SOURCE,
});

const indexPattern = {
id: 'indexPatternId',
fields: {
getByName: (name: string): IFieldType | null => {
return name === 'machine.os' ? indexPatternField : null;
},
},
title: 'my index pattern',
} as IndexPattern;

describe('getESFilters', () => {
test('Should return empty array when field does not exist in index pattern', async () => {
const notFoundFeaturePropertyField = new MockField({
fieldName: 'field name that is not in index pattern',
origin: FIELD_ORIGIN.SOURCE,
});
const esTooltipProperty = new ESTooltipProperty(
new TooltipProperty(
notFoundFeaturePropertyField.getName(),
await notFoundFeaturePropertyField.getLabel(),
'my value'
),
indexPattern,
notFoundFeaturePropertyField
);
expect(await esTooltipProperty.getESFilters()).toEqual([]);
});

test('Should return phrase filter when field value is provided', async () => {
const esTooltipProperty = new ESTooltipProperty(
new TooltipProperty(
featurePropertyField.getName(),
await featurePropertyField.getLabel(),
'my value'
),
indexPattern,
featurePropertyField
);
expect(await esTooltipProperty.getESFilters()).toEqual([
{
meta: {
index: 'indexPatternId',
},
query: {
match_phrase: {
['machine.os']: 'my value',
},
},
},
]);
});

test('Should return NOT exists filter for null values', async () => {
const esTooltipProperty = new ESTooltipProperty(
new TooltipProperty(
featurePropertyField.getName(),
await featurePropertyField.getLabel(),
undefined
),
indexPattern,
featurePropertyField
);
expect(await esTooltipProperty.getESFilters()).toEqual([
{
meta: {
index: 'indexPatternId',
negate: true,
},
exists: {
field: 'machine.os',
},
},
]);
});
});
19 changes: 15 additions & 4 deletions x-pack/plugins/maps/public/layers/tooltips/es_tooltip_property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
import _ from 'lodash';
import { ITooltipProperty } from './tooltip_property';
import { IField } from '../fields/field';
import { esFilters, IFieldType, IndexPattern } from '../../../../../../src/plugins/data/public';
import { PhraseFilter } from '../../../../../../src/plugins/data/public';
import {
esFilters,
Filter,
IFieldType,
IndexPattern,
} from '../../../../../../src/plugins/data/public';

export class ESTooltipProperty implements ITooltipProperty {
private readonly _tooltipProperty: ITooltipProperty;
Expand Down Expand Up @@ -64,12 +68,19 @@ export class ESTooltipProperty implements ITooltipProperty {
);
}

async getESFilters(): Promise<PhraseFilter[]> {
async getESFilters(): Promise<Filter[]> {
const indexPatternField = this._getIndexPatternField();
if (!indexPatternField) {
return [];
}

return [esFilters.buildPhraseFilter(indexPatternField, this.getRawValue(), this._indexPattern)];
const value = this.getRawValue();
if (value == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== relies on type coercion. maybe value === null || typeof value == 'undefined' instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to leave the check as == null. This is a pretty standard way to check for null, for example, from https://medium.com/javascript-in-plain-english/how-to-check-for-null-in-javascript-dffab64d8ed5 "That means checking for null is one of the few times in JavaScript that using == is recommended, while otherwise === is generally recommended." Additionally, this null check is used all over the Kibana code base. Just search for == null to see its usage.

const existsFilter = esFilters.buildExistsFilter(indexPatternField, this._indexPattern);
existsFilter.meta.negate = true;
return [existsFilter];
} else {
return [esFilters.buildPhraseFilter(indexPatternField, value, this._indexPattern)];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { ITooltipProperty } from './tooltip_property';
import { IJoin } from '../joins/join';
import { PhraseFilter } from '../../../../../../src/plugins/data/public';
import { Filter } from '../../../../../../src/plugins/data/public';

export class JoinTooltipProperty implements ITooltipProperty {
private readonly _tooltipProperty: ITooltipProperty;
Expand Down Expand Up @@ -37,7 +37,7 @@ export class JoinTooltipProperty implements ITooltipProperty {
return this._tooltipProperty.getHtmlDisplayValue();
}

async getESFilters(): Promise<PhraseFilter[]> {
async getESFilters(): Promise<Filter[]> {
const esFilters = [];
if (this._tooltipProperty.isFilterable()) {
esFilters.push(...(await this._tooltipProperty.getESFilters()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import _ from 'lodash';
import { PhraseFilter } from '../../../../../../src/plugins/data/public';
import { Filter } from '../../../../../../src/plugins/data/public';
import { TooltipFeature } from '../../../../../plugins/maps/common/descriptor_types';

export interface ITooltipProperty {
Expand All @@ -14,7 +14,7 @@ export interface ITooltipProperty {
getHtmlDisplayValue(): string;
getRawValue(): string | undefined;
isFilterable(): boolean;
getESFilters(): Promise<PhraseFilter[]>;
getESFilters(): Promise<Filter[]>;
}

export interface LoadFeatureProps {
Expand Down Expand Up @@ -70,7 +70,7 @@ export class TooltipProperty implements ITooltipProperty {
return false;
}

async getESFilters(): Promise<PhraseFilter[]> {
async getESFilters(): Promise<Filter[]> {
return [];
}
}