Skip to content

Commit

Permalink
Merge pull request #652 from WPMedia/cmg-feeds-content-api-with-condi…
Browse files Browse the repository at this point in the history
…tional-content

Add conditional content tag searching
  • Loading branch information
nschubach authored Sep 20, 2024
2 parents 8486964 + ef7c170 commit 262ae74
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 14 deletions.
16 changes: 8 additions & 8 deletions blocks/feeds-source-content-api-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ Creates an ElasticSearch DSL syntax to query Content-API

## Parameters

- Section: Comma separated list of sections, maps to taxonomy.sections.\_id
- Author: Maps to credits.by.\_id
- Section: Comma separated list of sections, maps to `taxonomy.sections._id`
- Author: Maps to `credits.by._id`
- Keywords: Maps to taxonomy.seo_keywords. It can be a comma separated list of values
- Tags-Text: Maps to taxonomy.tags.text. It can be a comma separated list of values
- Tags-Slug: Maps to taxonomy.tags.slug
- Tags-Text: Maps to `taxonomy.tags.text` and `variations.variants.content.taxonomy.tags.text` applying an `OR` logic. It can be a comma separated list of values.
- Tags-Slug: Maps to `taxonomy.tags.slug` and `variations.variants.content.taxonomy.tags.slug` applying an `OR` logic. It can be a comma separated list of values.
- Include-Terms: If you don’t want to use the default query you can enter a query here. It must be an array formatted like `[{"term":{"type": "story"}},{"range":{"last_updated_date:{"gte":"now-2d","lte":"now"}}}]`
- Exclude-Terms: If you need to exclude terms in your query (NOT) enter them here as an array formatted the same as the Include-Terms
- Exclude-Sections: Comma separated list of sections to exclude, maps to taxonomy.sections.\_id
- Exclude-Sections: Comma separated list of sections to exclude, maps to `taxonomy.sections._id`
- Feed-Size: Integer 1 to 100. Defaults to 100
- Feed-Offset: Integer. Defaults to 0
- Sort: Comma separated list of fields to sort on. Defaults to `publish_date:desc`
- Source-Exclude: ANS fields to remove from \_sourceIncludes default values
- Source-Include: ANS fields to add to \_sourceIncludes default values
- Source-Exclude: ANS fields to remove from `_sourceIncludes` default values
- Source-Include: ANS fields to add to `_sourceIncludes` default values
- Sitemap-at-root: (string) if set replaces all '-' with '/' in Section field
- Include-Distributor-Name: pass to C-API only one distributor field can be set
- Exclude-Distributor-Name: pass to C-API only one distributor field can be set
Expand All @@ -30,7 +30,7 @@ Creates an ElasticSearch DSL syntax to query Content-API

### Usage

Default query: [{"term":{"type": "story"}},{"range":{"last_updated_date":{"gte":"now-2d","lte":"now"}}}]
Default query: `[{"term":{"type": "story"}},{"range":{"last_updated_date":{"gte":"now-2d","lte":"now"}}}]`
You can create a feedDefaultQuery in blocks.json to override the default query.
If no value in passed in Include-Terms the default query will be used.

Expand Down
92 changes: 88 additions & 4 deletions blocks/feeds-source-content-api-block/sources/feeds-content-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,50 @@ const fetch = (key, { cachedCall }) => {
const cleanTagsText = tagsText.replace(/\//g, '').replace(/%20/g, '+')

body.query.bool.must.push({
terms: {
'taxonomy.tags.text.raw': cleanTagsText.split(','),
bool: {
minimum_should_match: 1,
should: [
{
terms: {
'taxonomy.tags.text.raw': cleanTagsText.split(','),
},
},
{
nested: {
ignore_unmapped: true,
path: 'variations.variants',
query: {
bool: {
must: [
{
term: {
'variations.variants.websites': key['arc-site'],
},
},
{
nested: {
ignore_unmapped: true,
path: 'variations.variants.content.taxonomy.tags',
query: {
bool: {
must: [
{
terms: {
'variations.variants.content.taxonomy.tags.text.raw':
cleanTagsText.split(','),
},
},
],
},
},
},
},
],
},
},
},
},
],
},
})
}
Expand All @@ -153,8 +195,50 @@ const fetch = (key, { cachedCall }) => {
const cleanTagsSlug = tagsSlug.replace(/\//g, '')

body.query.bool.must.push({
terms: {
'taxonomy.tags.slug': cleanTagsSlug.split(','),
bool: {
minimum_should_match: 1,
should: [
{
terms: {
'taxonomy.tags.slug': cleanTagsSlug.split(','),
},
},
{
nested: {
ignore_unmapped: true,
path: 'variations.variants',
query: {
bool: {
must: [
{
term: {
'variations.variants.websites': key['arc-site'],
},
},
{
nested: {
ignore_unmapped: true,
path: 'variations.variants.content.taxonomy.tags',
query: {
bool: {
must: [
{
terms: {
'variations.variants.content.taxonomy.tags.slug':
cleanTagsSlug.split(','),
},
},
],
},
},
},
},
],
},
},
},
},
],
},
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ it('returns query by tags text', async () => {

const callUrl = axios.mock.calls[0][0].url
expect(callUrl).toBe(
'undefined/content/v4/search/published?body=%7B%22query%22:%7B%22bool%22:%7B%22must%22:%5B%7B%22term%22:%7B%22type%22:%22story%22%7D%7D,%7B%22range%22:%7B%22last_updated_date%22:%7B%22gte%22:%22now-2d%22,%22lte%22:%22now%22%7D%7D%7D,%7B%22terms%22:%7B%22taxonomy.tags.text.raw%22:%5B%22football%22,%22sports%22%5D%7D%7D%5D%7D%7D%7D&website=demo&size=100&from=0&sort=publish_date:desc&_sourceInclude=canonical_url,canonical_website,created_date,credits,description,display_date,duration,first_publish_date,headlines,last_updated_date,promo_image,promo_items,publish_date,streams,subheadlines,subtitles,subtype,taxonomy.primary_section,taxonomy.seo_keywords,taxonomy.tags,type,video_type,content_elements,websites.demo',
'undefined/content/v4/search/published?body=%7B%22query%22:%7B%22bool%22:%7B%22must%22:%5B%7B%22term%22:%7B%22type%22:%22story%22%7D%7D,%7B%22range%22:%7B%22last_updated_date%22:%7B%22gte%22:%22now-2d%22,%22lte%22:%22now%22%7D%7D%7D,%7B%22bool%22:%7B%22minimum_should_match%22:1,%22should%22:%5B%7B%22terms%22:%7B%22taxonomy.tags.text.raw%22:%5B%22football%22,%22sports%22%5D%7D%7D,%7B%22nested%22:%7B%22ignore_unmapped%22:true,%22path%22:%22variations.variants%22,%22query%22:%7B%22bool%22:%7B%22must%22:%5B%7B%22term%22:%7B%22variations.variants.websites%22:%22demo%22%7D%7D,%7B%22nested%22:%7B%22ignore_unmapped%22:true,%22path%22:%22variations.variants.content.taxonomy.tags%22,%22query%22:%7B%22bool%22:%7B%22must%22:%5B%7B%22terms%22:%7B%22variations.variants.content.taxonomy.tags.text.raw%22:%5B%22football%22,%22sports%22%5D%7D%7D%5D%7D%7D%7D%7D%5D%7D%7D%7D%7D%5D%7D%7D%5D%7D%7D%7D&website=demo&size=100&from=0&sort=publish_date:desc&_sourceInclude=canonical_url,canonical_website,created_date,credits,description,display_date,duration,first_publish_date,headlines,last_updated_date,promo_image,promo_items,publish_date,streams,subheadlines,subtitles,subtype,taxonomy.primary_section,taxonomy.seo_keywords,taxonomy.tags,type,video_type,content_elements,websites.demo',
)
})

Expand All @@ -289,7 +289,7 @@ it('returns query by tags slug', async () => {

const callUrl = axios.mock.calls[0][0].url
expect(callUrl).toBe(
'undefined/content/v4/search/published?body=%7B%22query%22:%7B%22bool%22:%7B%22must%22:%5B%7B%22term%22:%7B%22type%22:%22story%22%7D%7D,%7B%22range%22:%7B%22last_updated_date%22:%7B%22gte%22:%22now-2d%22,%22lte%22:%22now%22%7D%7D%7D,%7B%22terms%22:%7B%22taxonomy.tags.slug%22:%5B%22football%22,%22sports%22%5D%7D%7D%5D%7D%7D%7D&website=demo&size=100&from=0&sort=publish_date:desc&_sourceInclude=canonical_url,canonical_website,created_date,credits,description,display_date,duration,first_publish_date,headlines,last_updated_date,promo_image,promo_items,publish_date,streams,subheadlines,subtitles,subtype,taxonomy.primary_section,taxonomy.seo_keywords,taxonomy.tags,type,video_type,content_elements,websites.demo',
'undefined/content/v4/search/published?body=%7B%22query%22:%7B%22bool%22:%7B%22must%22:%5B%7B%22term%22:%7B%22type%22:%22story%22%7D%7D,%7B%22range%22:%7B%22last_updated_date%22:%7B%22gte%22:%22now-2d%22,%22lte%22:%22now%22%7D%7D%7D,%7B%22bool%22:%7B%22minimum_should_match%22:1,%22should%22:%5B%7B%22terms%22:%7B%22taxonomy.tags.slug%22:%5B%22football%22,%22sports%22%5D%7D%7D,%7B%22nested%22:%7B%22ignore_unmapped%22:true,%22path%22:%22variations.variants%22,%22query%22:%7B%22bool%22:%7B%22must%22:%5B%7B%22term%22:%7B%22variations.variants.websites%22:%22demo%22%7D%7D,%7B%22nested%22:%7B%22ignore_unmapped%22:true,%22path%22:%22variations.variants.content.taxonomy.tags%22,%22query%22:%7B%22bool%22:%7B%22must%22:%5B%7B%22terms%22:%7B%22variations.variants.content.taxonomy.tags.slug%22:%5B%22football%22,%22sports%22%5D%7D%7D%5D%7D%7D%7D%7D%5D%7D%7D%7D%7D%5D%7D%7D%5D%7D%7D%7D&website=demo&size=100&from=0&sort=publish_date:desc&_sourceInclude=canonical_url,canonical_website,created_date,credits,description,display_date,duration,first_publish_date,headlines,last_updated_date,promo_image,promo_items,publish_date,streams,subheadlines,subtitles,subtype,taxonomy.primary_section,taxonomy.seo_keywords,taxonomy.tags,type,video_type,content_elements,websites.demo',
)
})

Expand Down

0 comments on commit 262ae74

Please sign in to comment.