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

Use analyzer set on the field #8943

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,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
Original file line number Diff line number Diff line change
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