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

feat: exact match modifier for fulltext #2208

Closed
wants to merge 4 commits into from
Closed
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
11 changes: 7 additions & 4 deletions packages/portal/src/components/search/SearchQueryBuilder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
v-model="queryRules[index]"
:tooltips="index === 0"
:validate="validatingRules"
@change="(field, value) => handleChangeRule(index, field, value)"
@change="(formField, value) => handleChangeRule(index, formField, value)"
@clear="clearRule(index)"
@invalid="handleInvalidRule(index)"
@valid="handleValidRule(index)"
Expand Down Expand Up @@ -113,10 +113,10 @@
}
this.handleSubmitForm();
},
handleChangeRule(index, field, value) {
this.queryRules[index][field] = value;
handleChangeRule(index, formField, value) {
this.queryRules[index][formField] = value;
const validRule = this.checkIfValidRule(this.queryRules[index]);
if (validRule || field === 'term') {
if (validRule || formField === 'term') {
this.handleSubmitForm();
}
},
Expand Down Expand Up @@ -144,6 +144,9 @@
}
},
checkIfValidRule(rule) {
if (!this.advancedSearchFieldSupportsExact(rule.field) && rule.modifier === 'exact') {
rule.modifier = null;
}
return Boolean(rule.field && rule.modifier && rule.term);
},
trackAdvancedSearch() {
Expand Down
13 changes: 9 additions & 4 deletions packages/portal/src/components/search/SearchQueryBuilderRule.vue
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,15 @@
],
modifier: [
{
options: this.advancedSearchModifiers.map((mod) => ({
value: mod.name,
text: this.$t(`search.advanced.modifiers.${mod.name}`)
}))
options: this.advancedSearchModifiers.map((mod) => {
if (mod.name === 'exact' && !(this.advancedSearchFieldSupportsExact(this.field))) {
return null;
}
return {
value: mod.name,
text: this.$t(`search.advanced.modifiers.${mod.name}`)
};
}).filter((mod) => mod !== null)
}
]
};
Expand Down
1 change: 1 addition & 0 deletions packages/portal/src/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,7 @@ export default {
"individual": "Individual fields"
},
"modifiers": {
"exact": "contains the phrase",
"contains": "contains",
"doesNotContain": "does not contain"
},
Expand Down
19 changes: 17 additions & 2 deletions packages/portal/src/mixins/advancedSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ const advancedSearchFieldsForEntityLookUp = advancedSearchFields.filter(field =>
const advancedSearchFieldNames = advancedSearchFields.map((field) => field.name);

const advancedSearchModifiers = [
{
name: 'exact',
query: {
[FIELD_TYPE_FULLTEXT]: '<field>:"<term>"'
}
},
{
name: 'contains',
query: {
Expand Down Expand Up @@ -95,17 +101,26 @@ export default {
},

methods: {
advancedSearchFieldByName(name) {
return this.advancedSearchFields.find((field) => field.name === name);
},

advancedSearchFieldSupportsExact(field) {
const fieldType = this.advancedSearchFieldByName(field)?.type;
return !!advancedSearchModifiers.find((modifier) => modifier.name === 'exact')?.query[fieldType];
},

advancedSearchFieldLabel(fieldName, locale) {
const fieldKey = fieldName === 'YEAR' ? 'year' : camelCase(fieldName.replace('proxy_', ''));
return this.$t(`fieldLabels.default.${fieldKey}`, locale);
},

advancedSearchQueryFromRule(rule, escaped) {
const field = this.advancedSearchFields.find((field) => field.name === rule.field);
const field = this.advancedSearchFieldByName(rule.field);
const modifier = this.advancedSearchModifiers.find((modifier) => modifier.name === rule.modifier);
const escapedTerm = escapeLuceneSpecials(rule.term, { spaces: true });
const term = escaped ? escapedTerm : rule.term;
return modifier?.query[field.type].replace('<field>', field.name).replace('<term>', term);
return modifier.query[field.type].replace('<field>', field.name).replace('<term>', term);
},

advancedSearchRouteQueryFromRules(rules) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,21 @@ describe('components/search/SearchQueryBuilder', () => {
expect(wrapper.vm.handleSubmitForm.called).toBe(true);
});
});

describe('when the respective rule does not support an exact modfier, but it was present on the rule', () => {
it('unsets the modifier and does NOT trigger a search', async() => {
const wrapper = factory();
sinon.spy(wrapper.vm, 'handleSubmitForm');

await wrapper.setData({ queryRules: [
{ field: 'fulltext', modifier: 'exact', term: 'dog' }
] });

wrapper.vm.handleChangeRule(0, 'field', 'proxy_dc_title');
expect(wrapper.vm.queryRules[0].modifier).toBe(null);
expect(wrapper.vm.handleSubmitForm.called).toBe(false);
});
});
});

describe('@invalid', () => {
Expand Down
27 changes: 27 additions & 0 deletions packages/portal/tests/unit/mixins/advancedSearch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ const factory = ({ mocks = {} } = {}) => shallowMountNuxt(component, {

describe('mixins/advancedSearch', () => {
describe('methods', () => {
describe('advancedSearchFieldByName', () => {
it('looks up the field based on the name attribute', () => {
const wrapper = factory();

const advancedSearchField = wrapper.vm.advancedSearchFieldByName('proxy_dc_date');

expect(advancedSearchField).toStrictEqual({ name: 'proxy_dc_date', type: 'string', suggestEntityType: 'timespan' });
});
});
describe('advancedSearchFieldSupportsExact', () => {
describe('when the field is configured to support exact match searches', () => {
it('returns true', () => {
const wrapper = factory();

const supportsExact = wrapper.vm.advancedSearchFieldSupportsExact('fulltext');
expect(supportsExact).toBe(true);
});
});
describe('when the field is NOT configured to support exact match searches', () => {
it('returns false', () => {
const wrapper = factory();

const supportsExact = wrapper.vm.advancedSearchFieldSupportsExact('fullproxy_dc_date');
expect(supportsExact).toBe(false);
});
});
});
describe('advancedSearchFieldLabel', () => {
it('translates "year" label for "YEAR" field', () => {
const wrapper = factory();
Expand Down
Loading