use of org.opensearch.common.xcontent.XContentFactory.jsonBuilder in project OpenSearch by opensearch-project.
the class QueryRescorerIT method testMoreDocs.
public void testMoreDocs() throws Exception {
Builder builder = Settings.builder();
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("type", "text").field("analyzer", "whitespace").endObject().endObject().endObject().endObject();
assertAcked(client().admin().indices().prepareCreate("test").addMapping("type1", mapping).setSettings(builder.put("index.number_of_shards", 1)));
client().prepareIndex("test").setId("1").setSource("field1", "massachusetts avenue boston massachusetts").get();
client().prepareIndex("test").setId("2").setSource("field1", "lexington avenue boston massachusetts").get();
client().prepareIndex("test").setId("3").setSource("field1", "boston avenue lexington massachusetts").get();
client().admin().indices().prepareRefresh("test").get();
client().prepareIndex("test").setId("4").setSource("field1", "boston road lexington massachusetts").get();
client().prepareIndex("test").setId("5").setSource("field1", "lexington street lexington massachusetts").get();
client().prepareIndex("test").setId("6").setSource("field1", "massachusetts avenue lexington massachusetts").get();
client().prepareIndex("test").setId("7").setSource("field1", "bosten street san franciso california").get();
client().admin().indices().prepareRefresh("test").get();
client().prepareIndex("test").setId("8").setSource("field1", "hollywood boulevard los angeles california").get();
client().prepareIndex("test").setId("9").setSource("field1", "1st street boston massachussetts").get();
client().prepareIndex("test").setId("10").setSource("field1", "1st street boston massachusetts").get();
client().admin().indices().prepareRefresh("test").get();
client().prepareIndex("test").setId("11").setSource("field1", "2st street boston massachusetts").get();
client().prepareIndex("test").setId("12").setSource("field1", "3st street boston massachusetts").get();
client().admin().indices().prepareRefresh("test").get();
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).get();
assertThat(searchResponse.getHits().getHits().length, equalTo(5));
assertHitCount(searchResponse, 9);
assertFirstHit(searchResponse, hasId("2"));
assertSecondHit(searchResponse, hasId("6"));
assertThirdHit(searchResponse, hasId("3"));
searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).get();
assertThat(searchResponse.getHits().getHits().length, equalTo(5));
assertHitCount(searchResponse, 9);
assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
assertFirstHit(searchResponse, hasId("2"));
assertSecondHit(searchResponse, hasId("6"));
assertThirdHit(searchResponse, hasId("3"));
// Make sure non-zero from works:
searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(2).setSize(5).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).get();
assertThat(searchResponse.getHits().getHits().length, equalTo(5));
assertHitCount(searchResponse, 9);
assertThat(searchResponse.getHits().getMaxScore(), greaterThan(searchResponse.getHits().getHits()[0].getScore()));
assertFirstHit(searchResponse, hasId("3"));
}
use of org.opensearch.common.xcontent.XContentFactory.jsonBuilder in project OpenSearch by opensearch-project.
the class PercolatorFieldMapperTests method testDuplicatedClauses.
public void testDuplicatedClauses() throws Exception {
addQueryFieldMappings();
QueryBuilder qb = boolQuery().must(boolQuery().must(termQuery("field", "value1")).must(termQuery("field", "value2"))).must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3")));
ParsedDocument doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), XContentType.JSON));
List<String> values = Arrays.stream(doc.rootDoc().getFields(fieldType.queryTermsField.name())).map(f -> f.binaryValue().utf8ToString()).sorted().collect(Collectors.toList());
assertThat(values.size(), equalTo(3));
assertThat(values.get(0), equalTo("field\0value1"));
assertThat(values.get(1), equalTo("field\0value2"));
assertThat(values.get(2), equalTo("field\0value3"));
int msm = doc.rootDoc().getFields(fieldType.minimumShouldMatchField.name())[0].numericValue().intValue();
assertThat(msm, equalTo(3));
qb = boolQuery().must(boolQuery().must(termQuery("field", "value1")).must(termQuery("field", "value2"))).must(boolQuery().must(termQuery("field", "value2")).must(termQuery("field", "value3"))).must(boolQuery().must(termQuery("field", "value3")).must(termQuery("field", "value4"))).must(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5")));
doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), XContentType.JSON));
values = Arrays.stream(doc.rootDoc().getFields(fieldType.queryTermsField.name())).map(f -> f.binaryValue().utf8ToString()).sorted().collect(Collectors.toList());
assertThat(values.size(), equalTo(5));
assertThat(values.get(0), equalTo("field\0value1"));
assertThat(values.get(1), equalTo("field\0value2"));
assertThat(values.get(2), equalTo("field\0value3"));
assertThat(values.get(3), equalTo("field\0value4"));
assertThat(values.get(4), equalTo("field\0value5"));
msm = doc.rootDoc().getFields(fieldType.minimumShouldMatchField.name())[0].numericValue().intValue();
assertThat(msm, equalTo(4));
qb = boolQuery().minimumShouldMatch(3).should(boolQuery().should(termQuery("field", "value1")).should(termQuery("field", "value2"))).should(boolQuery().should(termQuery("field", "value2")).should(termQuery("field", "value3"))).should(boolQuery().should(termQuery("field", "value3")).should(termQuery("field", "value4"))).should(boolQuery().should(termQuery("field", "value4")).should(termQuery("field", "value5")));
doc = mapperService.documentMapper("doc").parse(new SourceToParse("test", "doc", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field(fieldName, qb).endObject()), XContentType.JSON));
values = Arrays.stream(doc.rootDoc().getFields(fieldType.queryTermsField.name())).map(f -> f.binaryValue().utf8ToString()).sorted().collect(Collectors.toList());
assertThat(values.size(), equalTo(5));
assertThat(values.get(0), equalTo("field\0value1"));
assertThat(values.get(1), equalTo("field\0value2"));
assertThat(values.get(2), equalTo("field\0value3"));
assertThat(values.get(3), equalTo("field\0value4"));
assertThat(values.get(4), equalTo("field\0value5"));
msm = doc.rootDoc().getFields(fieldType.minimumShouldMatchField.name())[0].numericValue().intValue();
assertThat(msm, equalTo(1));
}
use of org.opensearch.common.xcontent.XContentFactory.jsonBuilder in project OpenSearch by opensearch-project.
the class QueryRescorerIT method testRescorerMadeScoresWorse.
// Tests a rescorer that penalizes the scores:
public void testRescorerMadeScoresWorse() throws Exception {
Builder builder = Settings.builder();
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("type", "text").field("analyzer", "whitespace").endObject().endObject().endObject().endObject();
assertAcked(client().admin().indices().prepareCreate("test").addMapping("type1", mapping).setSettings(builder.put("index.number_of_shards", 1)));
client().prepareIndex("test").setId("3").setSource("field1", "massachusetts").get();
client().prepareIndex("test").setId("6").setSource("field1", "massachusetts avenue lexington massachusetts").get();
client().admin().indices().prepareRefresh("test").get();
client().prepareIndex("test").setId("1").setSource("field1", "lexington massachusetts avenue").get();
client().prepareIndex("test").setId("2").setSource("field1", "lexington avenue boston massachusetts road").get();
client().admin().indices().prepareRefresh("test").get();
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).get();
assertThat(searchResponse.getHits().getHits().length, equalTo(4));
assertHitCount(searchResponse, 4);
assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
assertFirstHit(searchResponse, hasId("3"));
assertSecondHit(searchResponse, hasId("6"));
assertThirdHit(searchResponse, hasId("1"));
assertFourthHit(searchResponse, hasId("2"));
// Now, penalizing rescore (nothing matches the rescore query):
searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(1.0f).setRescoreQueryWeight(-1f), 3).get();
// 6 and 1 got worse, and then the hit (2) outside the rescore window were sorted ahead:
assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
assertFirstHit(searchResponse, hasId("3"));
assertSecondHit(searchResponse, hasId("2"));
assertThirdHit(searchResponse, hasId("6"));
assertFourthHit(searchResponse, hasId("1"));
}
use of org.opensearch.common.xcontent.XContentFactory.jsonBuilder in project OpenSearch by opensearch-project.
the class QueryRescorerIT method testSmallRescoreWindow.
// Tests a rescore window smaller than number of hits:
public void testSmallRescoreWindow() throws Exception {
Builder builder = Settings.builder();
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1").field("type", "text").field("analyzer", "whitespace").endObject().endObject().endObject().endObject();
assertAcked(client().admin().indices().prepareCreate("test").addMapping("type1", mapping).setSettings(builder.put("index.number_of_shards", 1)));
client().prepareIndex("test").setId("3").setSource("field1", "massachusetts").get();
client().prepareIndex("test").setId("6").setSource("field1", "massachusetts avenue lexington massachusetts").get();
client().admin().indices().prepareRefresh("test").get();
client().prepareIndex("test").setId("1").setSource("field1", "lexington massachusetts avenue").get();
client().prepareIndex("test").setId("2").setSource("field1", "lexington avenue boston massachusetts road").get();
client().admin().indices().prepareRefresh("test").get();
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts")).setFrom(0).setSize(5).get();
assertThat(searchResponse.getHits().getHits().length, equalTo(4));
assertHitCount(searchResponse, 4);
assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
assertFirstHit(searchResponse, hasId("3"));
assertSecondHit(searchResponse, hasId("6"));
assertThirdHit(searchResponse, hasId("1"));
assertFourthHit(searchResponse, hasId("2"));
// Now, rescore only top 2 hits w/ proximity:
searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts")).setFrom(0).setSize(5).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 2).get();
// Only top 2 hits were re-ordered:
assertThat(searchResponse.getHits().getHits().length, equalTo(4));
assertHitCount(searchResponse, 4);
assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
assertFirstHit(searchResponse, hasId("6"));
assertSecondHit(searchResponse, hasId("3"));
assertThirdHit(searchResponse, hasId("1"));
assertFourthHit(searchResponse, hasId("2"));
// Now, rescore only top 3 hits w/ proximity:
searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "massachusetts")).setFrom(0).setSize(5).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 3).get();
// Only top 3 hits were re-ordered:
assertThat(searchResponse.getHits().getHits().length, equalTo(4));
assertHitCount(searchResponse, 4);
assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
assertFirstHit(searchResponse, hasId("1"));
assertSecondHit(searchResponse, hasId("6"));
assertThirdHit(searchResponse, hasId("3"));
assertFourthHit(searchResponse, hasId("2"));
}
use of org.opensearch.common.xcontent.XContentFactory.jsonBuilder in project OpenSearch by opensearch-project.
the class NestedObjectMapperTests method testLimitOfNestedFieldsPerIndex.
public void testLimitOfNestedFieldsPerIndex() throws Exception {
Function<String, String> mapping = type -> {
try {
return Strings.toString(XContentFactory.jsonBuilder().startObject().startObject(type).startObject("properties").startObject("nested1").field("type", "nested").startObject("properties").startObject("nested2").field("type", "nested").endObject().endObject().endObject().endObject().endObject().endObject());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
// default limit allows at least two nested fields
createIndex("test1").mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_UPDATE);
// explicitly setting limit to 0 prevents nested fields
Exception e = expectThrows(IllegalArgumentException.class, () -> createIndex("test2", Settings.builder().put(MapperService.INDEX_MAPPING_NESTED_FIELDS_LIMIT_SETTING.getKey(), 0).build()).mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_UPDATE));
assertThat(e.getMessage(), containsString("Limit of nested fields [0] has been exceeded"));
// setting limit to 1 with 2 nested fields fails
e = expectThrows(IllegalArgumentException.class, () -> createIndex("test3", Settings.builder().put(MapperService.INDEX_MAPPING_NESTED_FIELDS_LIMIT_SETTING.getKey(), 1).build()).mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_UPDATE));
assertThat(e.getMessage(), containsString("Limit of nested fields [1] has been exceeded"));
// do not check nested fields limit if mapping is not updated
createIndex("test4", Settings.builder().put(MapperService.INDEX_MAPPING_NESTED_FIELDS_LIMIT_SETTING.getKey(), 0).build()).mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_RECOVERY);
}
Aggregations