From 98a13b078e5b1bbb80e87c0364a181d63b7ab570 Mon Sep 17 00:00:00 2001 From: Paul Beaudoin Date: Tue, 19 Nov 2024 15:53:39 -0500 Subject: [PATCH] Add additional adv search test --- test/elastic-query-builder.test.js | 70 +++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/test/elastic-query-builder.test.js b/test/elastic-query-builder.test.js index 6fe9cea2..8ed4bcd9 100644 --- a/test/elastic-query-builder.test.js +++ b/test/elastic-query-builder.test.js @@ -235,7 +235,7 @@ describe('ElasticQueryBuilder', () => { }) describe('multiple adv search params', () => { - it('applies contributor clauses to query', () => { + it('applies multiple param clauses to query', () => { const request = new ApiRequest({ title: 'title value', contributor: 'contributor value', @@ -282,5 +282,73 @@ describe('ElasticQueryBuilder', () => { }) }) }) + + describe('adv search params with filters', () => { + it('applies param and filter clauses to query', () => { + const request = new ApiRequest({ + title: 'title value', + contributor: 'contributor value', + filters: { + dateAfter: 2020, + dateBefore: 2021, + materialType: ['resourcetypes:aud'] + } + }) + const inst = ElasticQueryBuilder.forApiRequest(request) + + const query = inst.query.toJson() + + // Assert there's a multi-match: + expect(query).to.nested + .include({ + // Multi-match on title fields: + 'bool.must[0].bool.must[0].multi_match.fields[0]': 'title^5', + 'bool.must[0].bool.must[0].multi_match.query': 'title value', + + // Multi-match on creator/contrib fields: + 'bool.must[1].bool.must[0].multi_match.fields[0]': 'creatorLiteral^4', + 'bool.must[1].bool.must[0].multi_match.query': 'contributor value' + }) + + // Assert there's at least one of the title boosting clauses: + const titleShoulds = query.bool.must[0].bool.should + const prefixMatch = titleShoulds.find((should) => should.prefix) + expect(prefixMatch).to.deep.equal({ + prefix: { + 'title.keywordLowercasedStripped': { + value: 'title value', + boost: 50 + } + } + }) + + // Assert there's at least one of the creator boosting clauses: + const creatorShoulds = query.bool.must[1].bool.should + const creatorPrefixMatch = creatorShoulds.find((should) => should.prefix) + expect(creatorPrefixMatch).to.deep.equal({ + prefix: { + 'creatorLiteralNormalized.keywordLowercased': { + value: 'contributor value', + boost: 100 + } + } + }) + + // Asset filter clauses: + expect(query).to.nested.include({ + // Match filers[dateBefore]: + 'bool.filter[0].bool.must[0].bool.should[0].range.dateStartYear.lte': 2021, + 'bool.filter[0].bool.must[0].bool.should[1].range.dateEndYear.lte': 2021, + // Match filters[dateAfter]: + 'bool.filter[0].bool.must[1].bool.should[0].range.dateStartYear.gte': 2020, + 'bool.filter[0].bool.must[1].bool.should[1].range.dateEndYear.gte': 2020 + }) + + expect(query).to.nested.include({ + // Match filters[materialType]: + 'bool.filter[1].bool.should[0].term.materialType\\.id': 'resourcetypes:aud' + }) + }) + }) }) })