Navigation Menu

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

Highlighting: dedup field names when using wildcard expression to define which fields to highlight #10916

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -19,10 +19,9 @@

package org.elasticsearch.search.highlight;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.apache.lucene.index.IndexOptions;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.regex.Regex;
Expand All @@ -35,8 +34,8 @@
import org.elasticsearch.search.internal.InternalSearchHit;
import org.elasticsearch.search.internal.SearchContext;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.google.common.collect.Maps.newHashMap;

Expand Down Expand Up @@ -76,12 +75,12 @@ public boolean hitExecutionNeeded(SearchContext context) {
public void hitExecute(SearchContext context, HitContext hitContext) {
Map<String, HighlightField> highlightFields = newHashMap();
for (SearchContextHighlight.Field field : context.highlight().fields()) {
List<String> fieldNamesToHighlight;
Set<String> fieldNamesToHighlight;
Copy link
Member

Choose a reason for hiding this comment

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

Why not add

if (highlightFields.contains(fieldName)) { continue; }

around line 94?

That'll prevent two regexes that find the same field from highlighting it twice.

Copy link
Member Author

Choose a reason for hiding this comment

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

how can the same field be highlighted twice if we use a set that doesn't allow for duplicates? not sure what I'm missing here. Anyway, agree that the problem is probably in mappings code, but I am not familiar with it, somebody else will have to jump in on that.

Copy link
Member

Choose a reason for hiding this comment

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

I'm thinking something like:

"highlight": {
  "cat_*": <other stuff>,
  "*": <stuff>
}

As is now the last matching regex wins but it does so by highlighting the field twice. That's what I meant.

if (Regex.isSimpleMatchPattern(field.field())) {
DocumentMapper documentMapper = context.mapperService().documentMapper(hitContext.hit().type());
fieldNamesToHighlight = documentMapper.mappers().simpleMatchToFullName(field.field());
fieldNamesToHighlight = ImmutableSet.copyOf(documentMapper.mappers().simpleMatchToFullName(field.field()));
} else {
fieldNamesToHighlight = ImmutableList.of(field.field());
fieldNamesToHighlight = ImmutableSet.of(field.field());
}

if (context.highlight().forceSource(field)) {
Expand Down