Search in sources :

Example 1 with CategoryContextMapping

use of org.elasticsearch.search.suggest.completion.context.CategoryContextMapping in project elasticsearch by elastic.

the class ContextCompletionSuggestSearchIT method createIndexAndMappingAndSettings.

private void createIndexAndMappingAndSettings(Settings settings, CompletionMappingBuilder completionMappingBuilder) throws IOException {
    XContentBuilder mapping = jsonBuilder().startObject().startObject(TYPE).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);
    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:
                    final String fieldName = ((CategoryContextMapping) contextMapping.getValue()).getFieldName();
                    if (fieldName != null) {
                        mapping = mapping.field("path", fieldName);
                    }
                    break;
                case GEO:
                    final String name = ((GeoContextMapping) contextMapping.getValue()).getFieldName();
                    mapping = mapping.field("precision", ((GeoContextMapping) contextMapping.getValue()).getPrecision());
                    if (name != null) {
                        mapping.field("path", name);
                    }
                    break;
            }
            mapping = mapping.endObject();
        }
        mapping = mapping.endArray();
    }
    mapping = mapping.endObject().endObject().endObject().endObject();
    assertAcked(client().admin().indices().prepareCreate(INDEX).setSettings(Settings.builder().put(indexSettings()).put(settings)).addMapping(TYPE, mapping).get());
}
Also used : GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)

Example 2 with CategoryContextMapping

use of org.elasticsearch.search.suggest.completion.context.CategoryContextMapping 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 3 with CategoryContextMapping

use of org.elasticsearch.search.suggest.completion.context.CategoryContextMapping in project elasticsearch by elastic.

the class CategoryContextMappingTests method testQueryContextParsingBasic.

public void testQueryContextParsingBasic() throws Exception {
    XContentBuilder builder = jsonBuilder().value("context1");
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    CategoryContextMapping mapping = ContextBuilder.category("cat").build();
    List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
    assertThat(internalQueryContexts.size(), equalTo(1));
    assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
    assertThat(internalQueryContexts.get(0).boost, equalTo(1));
    assertThat(internalQueryContexts.get(0).isPrefix, equalTo(false));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)

Example 4 with CategoryContextMapping

use of org.elasticsearch.search.suggest.completion.context.CategoryContextMapping in project elasticsearch by elastic.

the class CategoryContextMappingTests method testQueryContextParsingObjectArray.

public void testQueryContextParsingObjectArray() throws Exception {
    XContentBuilder builder = jsonBuilder().startArray().startObject().field("context", "context1").field("boost", 2).field("prefix", true).endObject().startObject().field("context", "context2").field("boost", 3).field("prefix", false).endObject().endArray();
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    CategoryContextMapping mapping = ContextBuilder.category("cat").build();
    List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
    assertThat(internalQueryContexts.size(), equalTo(2));
    assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
    assertThat(internalQueryContexts.get(0).boost, equalTo(2));
    assertThat(internalQueryContexts.get(0).isPrefix, equalTo(true));
    assertThat(internalQueryContexts.get(1).context, equalTo("context2"));
    assertThat(internalQueryContexts.get(1).boost, equalTo(3));
    assertThat(internalQueryContexts.get(1).isPrefix, equalTo(false));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)

Example 5 with CategoryContextMapping

use of org.elasticsearch.search.suggest.completion.context.CategoryContextMapping in project elasticsearch by elastic.

the class CategoryContextMappingTests method testQueryContextParsingObject.

public void testQueryContextParsingObject() throws Exception {
    XContentBuilder builder = jsonBuilder().startObject().field("context", "context1").field("boost", 10).field("prefix", true).endObject();
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    CategoryContextMapping mapping = ContextBuilder.category("cat").build();
    List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
    assertThat(internalQueryContexts.size(), equalTo(1));
    assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
    assertThat(internalQueryContexts.get(0).boost, equalTo(10));
    assertThat(internalQueryContexts.get(0).isPrefix, equalTo(true));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)

Aggregations

CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)12 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)8 LinkedHashMap (java.util.LinkedHashMap)6 ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)6 GeoContextMapping (org.elasticsearch.search.suggest.completion.context.GeoContextMapping)6 XContentParser (org.elasticsearch.common.xcontent.XContentParser)5 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)4 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)4 ArrayList (java.util.ArrayList)3 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)3 GeoPoint (org.elasticsearch.common.geo.GeoPoint)3 Map (java.util.Map)2 HashMap (java.util.HashMap)1 StringField (org.apache.lucene.document.StringField)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 ParseContext (org.elasticsearch.index.mapper.ParseContext)1 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)1 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1