Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Enhancements

### Bug Fixes
* [SemanticHighlighter] Fix SemanticHighlighterExtBuilder.toXContent ([#1906](https://github.com/opensearch-project/neural-search/issues/1906)) (query-insights [#651](https://github.com/opensearch-project/query-insights/issues/651))

### Infrastructure

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
*/
package org.opensearch.neuralsearch.query.ext;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.neuralsearch.highlight.SemanticHighlightingConstants;
import org.opensearch.search.SearchExtBuilder;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

/**
* Search request ext builder that opts a request into batch semantic highlighting.
*
Expand Down Expand Up @@ -61,7 +61,7 @@ public void writeTo(StreamOutput out) throws IOException {

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.value(enabled);
return builder.field(NAME, enabled);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
*/
package org.opensearch.neuralsearch.query.ext;

import java.io.IOException;
import java.util.List;

import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.ParseField;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.search.SearchExtBuilder;
import org.opensearch.search.builder.SearchSourceBuilder;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.util.List;

public class SemanticHighlighterExtBuilderTests extends OpenSearchTestCase {

@Override
Expand Down Expand Up @@ -57,6 +62,14 @@ public void testRoundTripStreamingFalse() throws IOException {
roundTrip(false);
}

public void testRoundTripXContentTrue() throws IOException {
roundTripXContent(true);
}

public void testRoundTripXContentFalse() throws IOException {
roundTripXContent(false);
}

public void testParseBooleanTrue() throws IOException {
SemanticHighlighterExtBuilder result = parseValue("true");
assertTrue(result.isEnabled());
Expand Down Expand Up @@ -97,6 +110,26 @@ private void roundTrip(boolean value) throws IOException {
}
}

private void roundTripXContent(boolean value) throws IOException {
SemanticHighlighterExtBuilder original = new SemanticHighlighterExtBuilder(value);
MediaType xContentType = randomFrom(XContentType.values());
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().ext(List.of(original));
assertEquals("{\"ext\":{\"semantic_highlighting_batch\":" + value + "}}", sourceBuilder.toString());
boolean humanReadable = randomBoolean();
BytesReference shuffledXContent = this.toShuffledXContent(
sourceBuilder,
xContentType,
ToXContentObject.EMPTY_PARAMS,
humanReadable
);

try (XContentParser parser = this.createParser(xContentType.xContent(), shuffledXContent)) {
SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser);
assertEquals(1, searchSourceBuilder.ext().size());
assertEquals(original, searchSourceBuilder.ext().getFirst());
}
}

private SemanticHighlighterExtBuilder parseValue(String json) throws IOException {
XContentParser parser = createParser(org.opensearch.common.xcontent.XContentType.JSON.xContent(), json);
// advance past START_TOKEN to the value
Expand Down
Loading