use of org.opensearch.search.suggest.completion.context.GeoContextMapping in project OpenSearch by opensearch-project.
the class CompletionSuggestSearchIT method createIndexAndMappingAndSettings.
private void createIndexAndMappingAndSettings(Settings settings, CompletionMappingBuilder completionMappingBuilder) throws IOException {
XContentBuilder mapping = jsonBuilder().startObject().startObject("properties").startObject("test_field").field("type", "keyword").endObject().startObject("title").field("type", "keyword").endObject().startObject(FIELD).field("type", "completion").field("analyzer", completionMappingBuilder.indexAnalyzer).field("search_analyzer", completionMappingBuilder.searchAnalyzer).field("preserve_separators", completionMappingBuilder.preserveSeparators).field("preserve_position_increments", completionMappingBuilder.preservePositionIncrements);
if (completionMappingBuilder.contextMappings != null) {
mapping = mapping.startArray("contexts");
for (Map.Entry<String, ContextMapping<?>> contextMapping : completionMappingBuilder.contextMappings.entrySet()) {
mapping = mapping.startObject().field("name", contextMapping.getValue().name()).field("type", contextMapping.getValue().type().name());
switch(contextMapping.getValue().type()) {
case CATEGORY:
mapping = mapping.field("path", ((CategoryContextMapping) contextMapping.getValue()).getFieldName());
break;
case GEO:
mapping = mapping.field("path", ((GeoContextMapping) contextMapping.getValue()).getFieldName()).field("precision", ((GeoContextMapping) contextMapping.getValue()).getPrecision());
break;
}
mapping = mapping.endObject();
}
mapping = mapping.endArray();
}
mapping = mapping.endObject().endObject().endObject();
assertAcked(client().admin().indices().prepareCreate(INDEX).setSettings(Settings.builder().put(indexSettings()).put(settings)).addMapping(MapperService.SINGLE_MAPPING_NAME, mapping).get());
}
use of org.opensearch.search.suggest.completion.context.GeoContextMapping in project OpenSearch by opensearch-project.
the class ContextCompletionSuggestSearchIT method createIndexAndMappingAndSettings.
private void createIndexAndMappingAndSettings(Settings settings, CompletionMappingBuilder completionMappingBuilder) throws IOException {
XContentBuilder mapping = jsonBuilder().startObject().startObject("properties").startObject(FIELD).field("type", "completion").field("analyzer", completionMappingBuilder.indexAnalyzer).field("search_analyzer", completionMappingBuilder.searchAnalyzer).field("preserve_separators", completionMappingBuilder.preserveSeparators).field("preserve_position_increments", completionMappingBuilder.preservePositionIncrements);
List<String> categoryContextFields = new ArrayList<>();
if (completionMappingBuilder.contextMappings != null) {
mapping.startArray("contexts");
for (Map.Entry<String, ContextMapping<?>> contextMapping : completionMappingBuilder.contextMappings.entrySet()) {
mapping.startObject().field("name", contextMapping.getValue().name()).field("type", contextMapping.getValue().type().name());
switch(contextMapping.getValue().type()) {
case CATEGORY:
final String fieldName = ((CategoryContextMapping) contextMapping.getValue()).getFieldName();
if (fieldName != null) {
mapping.field("path", fieldName);
categoryContextFields.add(fieldName);
}
break;
case GEO:
final String name = ((GeoContextMapping) contextMapping.getValue()).getFieldName();
mapping.field("precision", ((GeoContextMapping) contextMapping.getValue()).getPrecision());
if (name != null) {
mapping.field("path", name);
}
break;
}
mapping.endObject();
}
mapping.endArray();
}
mapping.endObject();
for (String fieldName : categoryContextFields) {
mapping.startObject(fieldName).field("type", randomBoolean() ? "keyword" : "text").endObject();
}
mapping.endObject().endObject();
assertAcked(client().admin().indices().prepareCreate(INDEX).setSettings(Settings.builder().put(indexSettings()).put(settings)).addMapping(MapperService.SINGLE_MAPPING_NAME, mapping).get());
}
use of org.opensearch.search.suggest.completion.context.GeoContextMapping in project OpenSearch by opensearch-project.
the class GeoContextMappingTests method testParsingQueryContextObject.
public void testParsingQueryContextObject() throws Exception {
XContentBuilder builder = jsonBuilder().startObject().startObject("context").field("lat", 23.654242).field("lon", 90.047153).endObject().field("boost", 10).array("neighbours", 1, 2, 3).endObject();
XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder));
GeoContextMapping mapping = ContextBuilder.geo("geo").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8));
Collection<String> locations = new ArrayList<>();
locations.add("wh0n94");
locations.add("w");
addNeighborsAtLevel("w", 1, locations);
locations.add("wh");
addNeighborsAtLevel("wh", 2, locations);
locations.add("wh0");
addNeighborsAtLevel("wh0", 3, locations);
for (ContextMapping.InternalQueryContext internalQueryContext : internalQueryContexts) {
assertThat(internalQueryContext.context, is(in(locations)));
assertThat(internalQueryContext.boost, equalTo(10));
assertThat(internalQueryContext.isPrefix, equalTo(internalQueryContext.context.length() < GeoContextMapping.DEFAULT_PRECISION));
}
}
use of org.opensearch.search.suggest.completion.context.GeoContextMapping in project OpenSearch by opensearch-project.
the class GeoContextMappingTests method testParsingQueryContextGeoPoint.
public void testParsingQueryContextGeoPoint() throws Exception {
XContentBuilder builder = jsonBuilder().startObject().field("lat", 23.654242).field("lon", 90.047153).endObject();
XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder));
GeoContextMapping mapping = ContextBuilder.geo("geo").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
assertThat(internalQueryContexts.size(), equalTo(1 + 8));
Collection<String> locations = new ArrayList<>();
locations.add("wh0n94");
addNeighborsAtLevel("wh0n94", GeoContextMapping.DEFAULT_PRECISION, locations);
for (ContextMapping.InternalQueryContext internalQueryContext : internalQueryContexts) {
assertThat(internalQueryContext.context, is(in(locations)));
assertThat(internalQueryContext.boost, equalTo(1));
assertThat(internalQueryContext.isPrefix, equalTo(false));
}
}
use of org.opensearch.search.suggest.completion.context.GeoContextMapping in project OpenSearch by opensearch-project.
the class GeoContextMappingTests method testParsingQueryContextBasic.
public void testParsingQueryContextBasic() throws Exception {
XContentBuilder builder = jsonBuilder().value("ezs42e44yx96");
XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder));
GeoContextMapping mapping = ContextBuilder.geo("geo").build();
List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
assertThat(internalQueryContexts.size(), equalTo(1 + 8));
Collection<String> locations = new ArrayList<>();
locations.add("ezs42e");
addNeighborsAtLevel("ezs42e", GeoContextMapping.DEFAULT_PRECISION, locations);
for (ContextMapping.InternalQueryContext internalQueryContext : internalQueryContexts) {
assertThat(internalQueryContext.context, is(in(locations)));
assertThat(internalQueryContext.boost, equalTo(1));
assertThat(internalQueryContext.isPrefix, equalTo(false));
}
}
Aggregations