Search in sources :

Example 1 with CompletionMappingBuilder

use of org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project OpenSearch by opensearch-project.

the class ContextCompletionSuggestSearchIT method testGeoFiltering.

public void testGeoFiltering() throws Exception {
    LinkedHashMap<String, ContextMapping<?>> map = new LinkedHashMap<>();
    map.put("geo", ContextBuilder.geo("geo").build());
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    GeoPoint[] geoPoints = new GeoPoint[] { new GeoPoint("ezs42e44yx96"), new GeoPoint("u4pruydqqvj8") };
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).startObject("contexts").field("geo", (i % 2 == 0) ? geoPoints[0].getGeohash() : geoPoints[1].getGeohash()).endObject().endObject().endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder geoFilteringPrefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("geo", Collections.singletonList(GeoQueryContext.builder().setGeoPoint(new GeoPoint(geoPoints[0])).build())));
    assertSuggestions("foo", geoFilteringPrefix, "suggestion8", "suggestion6", "suggestion4", "suggestion2", "suggestion0");
}
Also used : ArrayList(java.util.ArrayList) GeoPoint(org.opensearch.common.geo.GeoPoint) LinkedHashMap(java.util.LinkedHashMap) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) GeoPoint(org.opensearch.common.geo.GeoPoint) ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) CompletionMappingBuilder(org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 2 with CompletionMappingBuilder

use of org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project OpenSearch by opensearch-project.

the class ContextCompletionSuggestSearchIT method testSingleContextBoosting.

public void testSingleContextBoosting() throws Exception {
    CategoryContextMapping contextMapping = ContextBuilder.category("cat").field("cat").build();
    LinkedHashMap<String, ContextMapping<?>> map = new LinkedHashMap<>(Collections.singletonMap("cat", contextMapping));
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject().field("cat", "cat" + i % 2).endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("cat", Arrays.asList(CategoryQueryContext.builder().setCategory("cat0").setBoost(3).build(), CategoryQueryContext.builder().setCategory("cat1").build())));
    assertSuggestions("foo", prefix, "suggestion8", "suggestion6", "suggestion4", "suggestion9", "suggestion2");
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.opensearch.common.geo.GeoPoint) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with CompletionMappingBuilder

use of org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project OpenSearch by opensearch-project.

the class ContextCompletionSuggestSearchIT method testGeoNeighbours.

public void testGeoNeighbours() throws Exception {
    String geohash = "gcpv";
    List<String> neighbours = new ArrayList<>();
    neighbours.add("gcpw");
    neighbours.add("gcpy");
    neighbours.add("u10n");
    neighbours.add("gcpt");
    neighbours.add("u10j");
    neighbours.add("gcps");
    neighbours.add("gcpu");
    neighbours.add("u10h");
    LinkedHashMap<String, ContextMapping<?>> map = new LinkedHashMap<>();
    map.put("geo", ContextBuilder.geo("geo").precision(4).build());
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).startObject("contexts").field("geo", randomFrom(neighbours)).endObject().endObject().endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder geoNeighbourPrefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("geo", Collections.singletonList(GeoQueryContext.builder().setGeoPoint(GeoPoint.fromGeohash(geohash)).build())));
    assertSuggestions("foo", geoNeighbourPrefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.opensearch.common.geo.GeoPoint) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with CompletionMappingBuilder

use of org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project OpenSearch by opensearch-project.

the class ContextCompletionSuggestSearchIT method testContextPrefix.

public void testContextPrefix() throws Exception {
    LinkedHashMap<String, ContextMapping<?>> map = new LinkedHashMap<>();
    map.put("cat", ContextBuilder.category("cat").field("cat").build());
    boolean addAnotherContext = randomBoolean();
    if (addAnotherContext) {
        map.put("type", ContextBuilder.category("type").field("type").build());
    }
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject().field("cat", "cat" + i % 2);
        if (addAnotherContext) {
            source.field("type", "type" + i % 3);
        }
        source.endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("cat", Collections.singletonList(CategoryQueryContext.builder().setCategory("cat").setPrefix(true).build())));
    assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.opensearch.common.geo.GeoPoint) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with CompletionMappingBuilder

use of org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project OpenSearch by opensearch-project.

the class ContextCompletionSuggestSearchIT method testSingleContextFiltering.

public void testSingleContextFiltering() throws Exception {
    CategoryContextMapping contextMapping = ContextBuilder.category("cat").field("cat").build();
    LinkedHashMap<String, ContextMapping<?>> map = new LinkedHashMap<>(Collections.singletonMap("cat", contextMapping));
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject().field("cat", "cat" + i % 2).endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("cat", Collections.singletonList(CategoryQueryContext.builder().setCategory("cat0").build())));
    assertSuggestions("foo", prefix, "suggestion8", "suggestion6", "suggestion4", "suggestion2", "suggestion0");
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.opensearch.common.geo.GeoPoint) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)14 CompletionMappingBuilder (org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)14 CompletionSuggestionBuilder (org.opensearch.search.suggest.completion.CompletionSuggestionBuilder)14 CategoryContextMapping (org.opensearch.search.suggest.completion.context.CategoryContextMapping)14 ContextMapping (org.opensearch.search.suggest.completion.context.ContextMapping)14 GeoContextMapping (org.opensearch.search.suggest.completion.context.GeoContextMapping)14 ArrayList (java.util.ArrayList)13 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)13 GeoPoint (org.opensearch.common.geo.GeoPoint)13 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)11 HashMap (java.util.HashMap)2 List (java.util.List)2 IndexResponse (org.opensearch.action.index.IndexResponse)2 ToXContent (org.opensearch.common.xcontent.ToXContent)2 GeoQueryContext (org.opensearch.search.suggest.completion.context.GeoQueryContext)2 RandomStrings (com.carrotsearch.randomizedtesting.generators.RandomStrings)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Locale (java.util.Locale)1