Skip to content

Commit

Permalink
Revert "Search terms highlighted in Wire [CPCN-17] (#388)"
Browse files Browse the repository at this point in the history
This reverts commit e0f2ee1.
  • Loading branch information
petrjasek committed Jun 9, 2023
1 parent b38f4ae commit ac9de61
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 87 deletions.
10 changes: 3 additions & 7 deletions assets/ui/components/ArticleHeadline.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import React from 'react';
import PropTypes from 'prop-types';

export default function ArticleHeadline({item}) {
return item.headline ? (
<h2 className="wire-column__preview__headline">
{item.es_highlight && item.es_highlight.headline ? (
<span dangerouslySetInnerHTML={{__html: item.es_highlight.headline[0]}} />
) : item.headline}
</h2>
) : null;
return (
item.headline && <h2 className='wire-column__preview__headline'>{item.headline}</h2> || null
);
}

ArticleHeadline.propTypes = {
Expand Down
12 changes: 4 additions & 8 deletions assets/ui/components/ArticleSlugline.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import {getSlugline} from 'utils';

export default function ArticleSlugline({item}) {
const slugline = getSlugline(item, true);

return slugline ? (
<div className="wire-column__preview__slugline">
{item.es_highlight && item.es_highlight.slugline ? (
<span dangerouslySetInnerHTML={{__html: item.es_highlight.slugline[0]}} />
) : slugline}
</div>
) : null;

return !slugline ? null : (
<span className="wire-column__preview__slug">{slugline}</span>
);
}

ArticleSlugline.propTypes = {
Expand Down
2 changes: 1 addition & 1 deletion assets/wire/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export function search(state, next, aggs) {
timezone_offset: getTimezoneOffset(),
newsOnly,
product: searchParams.product,
es_highlight: !searchParams.query && !searchParams.advancedSearch ? null : 1,
es_highlight: !searchParams.query ? null : 1,
all_versions: !searchAllVersions ? null : 1,
prepend_embargoed: !state.bookmarks ? null : 0,
aggs: aggs === false ? '0' : '1',
Expand Down
12 changes: 3 additions & 9 deletions assets/wire/components/WireListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@ class WireListItem extends React.Component {
)}
<Embargo item={item} />
<UrgencyLabel item={item} listConfig={listConfig} filterGroupLabels={this.props.filterGroupLabels} />
{item.es_highlight && item.es_highlight.headline ? <div
dangerouslySetInnerHTML={({__html: item.es_highlight.headline && item.es_highlight.headline[0]})}
/> : item.headline}
{item.headline}
</div>
</h4>

Expand All @@ -201,9 +199,7 @@ class WireListItem extends React.Component {
/>
<div className="wire-articles__item__meta-info">
<span className="bold">
{item.es_highlight && item.es_highlight.slugline ? <div
dangerouslySetInnerHTML={({__html:item.es_highlight.slugline && item.es_highlight.slugline[0]})}
/> : getSlugline(item, true)}
{getSlugline(item, true)}
</span>
<span>
<FieldComponents
Expand Down Expand Up @@ -265,9 +261,7 @@ class WireListItem extends React.Component {

{isExtended && (
<div className="wire-articles__item__text">
{item.es_highlight ? <div
dangerouslySetInnerHTML={({__html:item.es_highlight.body_html && item.es_highlight.body_html[0]})}
/> : <p>{shortText(item, 40, listConfig)}</p>}
<p>{shortText(item, 40, listConfig)}</p>
</div>
)}

Expand Down
13 changes: 2 additions & 11 deletions newsroom/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,7 @@ def prefill_search_items(self, search):
def prefill_search_highlights(self, search, req):
query_string = search.args.get("q")
query_string_settings = app.config["ELASTICSEARCH_SETTINGS"]["settings"]["query_string"]
advanced_search = search.args.get("advanced_search")
if app.data.elastic.should_highlight(req) and (
query_string
or (advanced_search and json.loads(advanced_search).get("all"))
or (advanced_search and json.loads(advanced_search).get("any"))
):
if app.data.elastic.should_highlight(req) and query_string:
elastic_highlight_query = get_elastic_highlight_query(
query_string={
"query": query_string,
Expand All @@ -512,11 +507,7 @@ def prefill_search_highlights(self, search, req):
"lenient": True,
},
)
elastic_highlight_query["fields"] = {
"body_html": {},
"headline": {},
"slugline": {},
}
elastic_highlight_query["fields"] = {"body_html": elastic_highlight_query["fields"]["body_html"]}

search.highlight = elastic_highlight_query

Expand Down
52 changes: 1 addition & 51 deletions tests/core/test_wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,60 +801,10 @@ def test_wire_delete(client, app):


def test_highlighting(client, app):
app.data.insert(
"items",
[
{
"_id": "foo",
"body_html": "Story that involves cheese and onions",
"slugline": "That's the test slugline cheese",
"headline": "Demo Article",
}
],
)
app.data.insert("items", [{"_id": "foo", "body_html": "Story that involves cheese and onions"}])
resp = client.get("/wire/search?q=cheese&es_highlight=1")
data = json.loads(resp.get_data())
assert (
data["_items"][0]["es_highlight"]["body_html"][0] == 'Story that involves <span class="es-highlight">'
"cheese</span> and onions"
)
assert (
data["_items"][0]["es_highlight"]["slugline"][0]
== 'That\'s the test slugline <span class="es-highlight">cheese</span>'
)

resp = client.get("/wire/search?q=demo&es_highlight=1")
data = json.loads(resp.get_data())
assert data["_items"][0]["es_highlight"]["headline"][0] == '<span class="es-highlight">Demo</span> Article'


def test_highlighting_with_advanced_search(client, app):
app.data.insert(
"items",
[
{
"_id": "foo",
"body_html": "Story that involves cheese and onions",
"slugline": "That's the test slugline cheese",
"headline": "Demo Article",
}
],
)
advanced_search_params = parse.quote('{"fields":[],"all":"demo"}')
url = f"/wire/search?advanced_search={advanced_search_params}&es_highlight=1"
resp = client.get(url)
data = json.loads(resp.get_data())
assert data["_items"][0]["es_highlight"]["headline"][0] == '<span class="es-highlight">Demo</span> Article'

advanced_search_params = parse.quote('{"fields":[],"any":"cheese"}')
url = f"/wire/search?advanced_search={advanced_search_params}&es_highlight=1"
resp = client.get(url)
data = json.loads(resp.get_data())
assert (
data["_items"][0]["es_highlight"]["slugline"][0]
== 'That\'s the test slugline <span class="es-highlight">cheese</span>'
)
assert (
data["_items"][0]["es_highlight"]["body_html"][0] == 'Story that involves <span class="es-highlight">'
"cheese</span> and onions"
)

0 comments on commit ac9de61

Please sign in to comment.