Search in sources :

Example 6 with CompletionMappingBuilder

use of org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project elasticsearch by elastic.

the class ContextCompletionSuggestSearchIT method testContextFuzzy.

public void testContextFuzzy() 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", "sugxgestion" + 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, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg", Fuzziness.ONE);
    assertSuggestions("foo", prefix, "sugxgestion9", "sugxgestion8", "sugxgestion7", "sugxgestion6", "sugxgestion5");
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with CompletionMappingBuilder

use of org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project elasticsearch by elastic.

the class ContextCompletionSuggestSearchIT method testSingleContextFiltering.

public void testSingleContextFiltering() throws Exception {
    CategoryContextMapping contextMapping = ContextBuilder.category("cat").field("cat").build();
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<String, ContextMapping>(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, TYPE, "" + 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.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with CompletionMappingBuilder

use of org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project elasticsearch by elastic.

the class ContextCompletionSuggestSearchIT method testGeoBoosting.

public void testGeoBoosting() 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, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
    GeoQueryContext context1 = GeoQueryContext.builder().setGeoPoint(geoPoints[0]).setBoost(2).build();
    GeoQueryContext context2 = GeoQueryContext.builder().setGeoPoint(geoPoints[1]).build();
    CompletionSuggestionBuilder geoBoostingPrefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("geo", Arrays.asList(context1, context2)));
    assertSuggestions("foo", geoBoostingPrefix, "suggestion8", "suggestion6", "suggestion4", "suggestion9", "suggestion7");
}
Also used : ArrayList(java.util.ArrayList) GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoQueryContext(org.elasticsearch.search.suggest.completion.context.GeoQueryContext) LinkedHashMap(java.util.LinkedHashMap) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 9 with CompletionMappingBuilder

use of org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project elasticsearch by elastic.

the class ContextCompletionSuggestSearchIT method testSingleContextMultipleContexts.

public void testSingleContextMultipleContexts() throws Exception {
    CategoryContextMapping contextMapping = ContextBuilder.category("cat").field("cat").build();
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<String, ContextMapping>(Collections.singletonMap("cat", contextMapping));
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<String> contexts = Arrays.asList("type1", "type2", "type3", "type4");
    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", contexts).endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
}
Also used : ArrayList(java.util.ArrayList) GeoPoint(org.elasticsearch.common.geo.GeoPoint) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) LinkedHashMap(java.util.LinkedHashMap) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 10 with CompletionMappingBuilder

use of org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project elasticsearch by elastic.

the class ContextCompletionSuggestSearchIT method testSingleContextBoosting.

public void testSingleContextBoosting() throws Exception {
    CategoryContextMapping contextMapping = ContextBuilder.category("cat").field("cat").build();
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<String, ContextMapping>(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, TYPE, "" + 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.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)16 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)16 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)16 CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)16 ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)16 GeoContextMapping (org.elasticsearch.search.suggest.completion.context.GeoContextMapping)16 ArrayList (java.util.ArrayList)15 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15 GeoPoint (org.elasticsearch.common.geo.GeoPoint)15 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)13 HashMap (java.util.HashMap)2 List (java.util.List)2 ToXContent (org.elasticsearch.common.xcontent.ToXContent)2 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 GeoQueryContext (org.elasticsearch.search.suggest.completion.context.GeoQueryContext)1