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

Fixes #553: fix highlight max_analyzer_offset field name to match wit… #557

Merged
Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fix missing cause message in missing permission to call Fine Grained Access Control Amazon OpenSearch domain ([#473](https://github.com/opensearch-project/opensearch-java/issues/473))
- Fix catching JsonParsingException ([#494](https://github.com/opensearch-project/opensearch-java/issues/494))
- Fix StoryStats numeric value out of range of int ([#489](https://github.com/opensearch-project/opensearch-java/pull/489))

- Fix highlight max_analyzer_offset field name to match with the one introduced in OpenSearch 2.2.0 ([#555](https://github.com/opensearch-project/opensearch-java/pull/555))
-
### Security


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class Highlight implements JsonpSerializable {
private final Query highlightQuery;

@Nullable
private final String maxAnalyzedOffset;
private final String maxAnalyzerOffset;

// ---------------------------------------------------------------------------------------------

Expand All @@ -137,7 +137,7 @@ private Highlight(Builder builder) {
this.requireFieldMatch = builder.requireFieldMatch;
this.tagsSchema = builder.tagsSchema;
this.highlightQuery = builder.highlightQuery;
this.maxAnalyzedOffset = builder.maxAnalyzedOffset;
this.maxAnalyzerOffset = builder.maxAnalyzerOffset;

}

Expand Down Expand Up @@ -295,11 +295,11 @@ public final Query highlightQuery() {
}

/**
* API name: {@code max_analyzed_offset}
* API name: {@code max_analyzer_offset}
*/
@Nullable
public final String maxAnalyzedOffset() {
return this.maxAnalyzedOffset;
public final String maxAnalyzerOffset() {
return this.maxAnalyzerOffset;
}

/**
Expand Down Expand Up @@ -419,9 +419,9 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
this.highlightQuery.serialize(generator, mapper);

}
if (this.maxAnalyzedOffset != null) {
generator.writeKey("max_analyzed_offset");
generator.write(this.maxAnalyzedOffset);
if (this.maxAnalyzerOffset != null) {
generator.writeKey("max_analyzer_offset");
generator.write(this.maxAnalyzerOffset);

}

Expand Down Expand Up @@ -491,7 +491,7 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<H
private Query highlightQuery;

@Nullable
private String maxAnalyzedOffset;
private String maxAnalyzerOffset;

/**
* Required - API name: {@code fields}
Expand Down Expand Up @@ -705,10 +705,10 @@ public final Builder highlightQuery(Function<Query.Builder, ObjectBuilder<Query>
}

/**
* API name: {@code max_analyzed_offset}
* API name: {@code max_analyzer_offset}
*/
public final Builder maxAnalyzedOffset(@Nullable String value) {
this.maxAnalyzedOffset = value;
public final Builder maxAnalyzerOffset(@Nullable String value) {
this.maxAnalyzerOffset = value;
return this;
}

Expand Down Expand Up @@ -756,7 +756,7 @@ protected static void setupHighlightDeserializer(ObjectDeserializer<Highlight.Bu
op.add(Builder::requireFieldMatch, JsonpDeserializer.booleanDeserializer(), "require_field_match");
op.add(Builder::tagsSchema, HighlighterTagsSchema._DESERIALIZER, "tags_schema");
op.add(Builder::highlightQuery, Query._DESERIALIZER, "highlight_query");
op.add(Builder::maxAnalyzedOffset, JsonpDeserializer.stringDeserializer(), "max_analyzed_offset");
op.add(Builder::maxAnalyzerOffset, JsonpDeserializer.stringDeserializer(), "max_analyzer_offset");

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.client.opensearch.integTest;

import org.junit.Test;
import org.opensearch.client.opensearch._types.Refresh;
import org.opensearch.client.opensearch.core.SearchResponse;
import org.opensearch.client.opensearch.core.search.Highlight;
import org.opensearch.client.opensearch.core.search.Hit;
import org.opensearch.client.util.ObjectBuilder;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public abstract class AbstractHighlightIT extends OpenSearchJavaClientTestCase {

@Test
public void testDefaultHighlight() throws Exception {
String index = "queries_highlight";
createTestDocuments(index);

List<Map<String, List<String>>> highlights = highlightQuery("spread",
h -> h.fields("content", b -> b));

assertEquals(2, highlights.size());
for (Map<String, List<String>> hit : highlights) {
assertEquals(1, hit.size());
assertEquals(1, hit.get("content").size());
assertTrue(hit.get("content").get(0).contains("<em>spread</em>"));
}
}

@Test
public void testMultiFieldHighlight() throws Exception {
String index = "queries_highlight";
createTestDocuments(index);

List<Map<String, List<String>>> highlights = highlightQuery("real decades",
h -> h.fields("title", b -> b).fields("content", b -> b));

assertEquals(3, highlights.size());
for (Map<String, List<String>> hit : highlights) {
assertEquals(2, hit.size());
assertEquals(1, hit.get("title").size());
assertTrue(hit.get("title").get(0).contains("<em>Real</em>"));
assertEquals(1, hit.get("content").size());
assertTrue(hit.get("content").get(0).contains("<em>decades</em>"));
}
}

@Test
public void testDifferentMarkersHighlight() throws Exception {
String index = "queries_highlight";
createTestDocuments(index);

List<Map<String, List<String>>> highlights = highlightQuery("spread",
h -> h.preTags("<b>").postTags("</b>").fields("content", b -> b));

assertEquals(2, highlights.size());
for (Map<String, List<String>> hit : highlights) {
assertEquals(1, hit.size());
assertEquals(1, hit.get("content").size());
assertTrue(hit.get("content").get(0).contains("<b>spread</b>"));
}
}

@Test
public void testAnalyzerOffset() throws Exception {

String[] versionNumber = javaClient().info().version().number().split("\\.");
int major = Integer.parseInt(versionNumber[0]);
int minor = Integer.parseInt(versionNumber[1]);

// The max_analyzer_offset setting was introduced in OpenSearch 2.2.0
// before that, the max_analyzer_offset thrown an unknown field exception
// For more details, see:
// https://github.com/opensearch-project/OpenSearch/pull/4031
// https://github.com/opensearch-project/OpenSearch/pull/3893
if (major >= 2 && minor >= 2) {
String index = "queries_highlight";
createTestDocuments(index);
List<Map<String, List<String>>> highlights = highlightQuery("real",
h -> h.maxAnalyzerOffset("5").fields("title", b -> b));

assertEquals(3, highlights.size());
assertEquals(0, highlights.get(0).size());
assertEquals(1, highlights.get(1).size());
assertEquals(1, highlights.get(2).size());

assertTrue(highlights.get(1).get("title").get(0).contains("<em>Real</em>"));
assertTrue(highlights.get(2).get("title").get(0).contains("<em>Real</em>"));
}
}

private List<Map<String, List<String>>> highlightQuery(String query,
Function<Highlight.Builder, ObjectBuilder<Highlight>> fn)
throws IOException {
SearchResponse<Article> response = javaClient()
.search(s -> s
.query(q -> q
.simpleQueryString(sqs -> sqs
.fields("title", "content", "author")
.query(query)))
.highlight(fn), Article.class);
return response.hits().hits().stream().map(Hit::highlight).collect(Collectors.toList());
}

private void createTestDocuments(String index) throws IOException {
javaClient().create(_1 -> _1.index(index).id("1").document(createArticle(
"Superheroes are Real", "Meet these man avoid the fire spread during last decades.", "John Doe"))
.refresh(Refresh.True));
javaClient().create(_1 -> _1.index(index).id("2").document(createArticle(
"Real Slow Ideas", "Why some innovations spread quick while others take decades to catch hold.", "John Foo"))
.refresh(Refresh.True));
javaClient().create(_1 -> _1.index(index).id("3").document(createArticle(
"Real Two Degrees", "How the world failed on climate change during the decades.", "Anne Doe"))
.refresh(Refresh.True));
}

private Article createArticle(String title, String content, String author) {
return new Article(title, content, author);
}

public static class Article {
public String title;
public String content;
public String author;

public Article() {
}

public Article(String title, String content, String author) {
this.title = title;
this.content = content;
this.author = author;
}

public String getTitle() {
return title;
}

public void setTitle(final String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(final String content) {
this.content = content;
}

public String getAuthor() {
return author;
}

public void setAuthor(final String author) {
this.author = author;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.client.opensearch.integTest.httpclient5;

import org.opensearch.client.opensearch.integTest.AbstractHighlightIT;

public class HighlightIT extends AbstractHighlightIT implements HttpClient5TransportSupport {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.client.opensearch.integTest.restclient;

import org.apache.http.HttpHost;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;
import org.opensearch.client.opensearch.integTest.AbstractHighlightIT;
import org.opensearch.client.transport.OpenSearchTransport;
import org.opensearch.client.transport.rest_client.RestClientTransport;
import org.opensearch.common.settings.Settings;

import java.io.IOException;

public class HighlightIT extends AbstractHighlightIT {
@Override
public OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException {
return new RestClientTransport(buildClient(settings, hosts), new JacksonJsonpMapper());
}
}
Loading