Skip to content

Commit

Permalink
Highlight: Use analyzer set on the field
Browse files Browse the repository at this point in the history
Make highlighter use analyzer set on the field.

Highlight with PlainHighlighter doesn't work on a field with an analyzer specified
when a document has document level analyzer specified by _analyzer.

With this fix, it first look for analyzer set on the field. If not found, try _analyzer's path.
If not found or _analyzer's path is null, try analyzer set on type level.

Closes #8757
  • Loading branch information
masaruh committed May 7, 2015
1 parent e7acb0e commit 636f354
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
Expand Up @@ -135,16 +135,19 @@ public Analyzer setAnalyzer(HighlighterContext context){
return context.analyzer();
}

Analyzer analyzer = null;

if (path != null) {
String analyzerName = (String) context.context.lookup().source().extractValue(path);
analyzer = context.context.mapperService().analysisService().analyzer(analyzerName);
}
Analyzer analyzer = context.mapper.indexAnalyzer();

if (analyzer == null) {
analyzer = context.context.mapperService().documentMapper(context.hitContext.hit().type()).mappers().indexAnalyzer();
if (path != null) {
String analyzerName = (String) context.context.lookup().source().extractValue(path);
analyzer = context.context.mapperService().analysisService().analyzer(analyzerName);
}

if (analyzer == null) {
analyzer = context.context.mapperService().documentMapper(context.hitContext.hit().type()).mappers().indexAnalyzer();
}
}

context.analyzer(analyzer);

return analyzer;
Expand Down
Expand Up @@ -679,6 +679,42 @@ public void testPlainHighlighterDocumentAnalyzer() throws Exception {
assertHighlight(response, 0, "text", 0, 1, equalTo("Look at me, I'm eating <1>cars</1>."));
}

@Test
public void testPlainHighlighterDocumentAnalyzerOverriddenByFieldAnalyzer() throws Exception {
client().admin().indices().prepareCreate("test")
.addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1")
.startObject("_analyzer")
.field("path", "language_analyzer")
.endObject()
.startObject("properties")
.startObject("language_analyzer")
.field("type", "string")
.field("index", "not_analyzed")
.endObject()
.startObject("text")
.field("type", "string")
.field("analyzer", "standard")
.endObject()
.endObject()
.endObject().endObject()).execute().actionGet();
ensureYellow();

index("test", "type1", "1",
"language_analyzer", "english",
"text", "Look at me, I'm eating cars.");
refresh();

// Make sure both "text" field and query string are analyzed by standard analyzer.
// If english analyzer specified in "language_analyzer" field is used for "text" field, "cars" in the field is analyzed as "car",
// which is different from analyzed query, "cars". As a result, there'll be no highlight (See #8943).
SearchResponse response = client().prepareSearch("test")
.setQuery(QueryBuilders.matchQuery("text", "cars"))
.addHighlightedField(
new HighlightBuilder.Field("text").preTags("<1>").postTags("</1>").requireFieldMatch(true))
.get();
assertHighlight(response, 0, "text", 0, 1, equalTo("Look at me, I'm eating <1>cars</1>."));
}

@Test
public void testFastVectorHighlighter() throws Exception {
assertAcked(prepareCreate("test").addMapping("type1", type1TermVectorMapping()));
Expand Down

0 comments on commit 636f354

Please sign in to comment.