Java tutorial
/* * Copyright 2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.curly.bloodhound.query.search; import io.curly.bloodhound.query.Transpiler; import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.stereotype.Component; import org.springframework.util.MultiValueMap; import static io.curly.bloodhound.query.References.*; import static java.util.stream.Collectors.joining; /** * @author Joo Pedro Evangelista */ @Component public class ElasticQueryBuilder { private final Transpiler transpiler; @Autowired public ElasticQueryBuilder(Transpiler transpiler) { this.transpiler = transpiler; } public QueryBuilder build(final String query) { final MultiValueMap<String, String> valueMap = transpiler.execute(query); NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder(); if (valueMap.containsKey("text")) { builder.withQuery(new MultiMatchQueryBuilder(valueMap.get("text").stream().collect(joining()), "description", CATEGORY, LANGUAGES, TAGS)); } else { if (valueMap.containsKey(CATEGORY)) { builder.withQuery( new MultiMatchQueryBuilder(valueMap.get(CATEGORY).stream().collect(joining()), CATEGORY)); } else if (valueMap.containsKey(LANGUAGES)) { builder.withQuery( new MultiMatchQueryBuilder(valueMap.get(LANGUAGES).stream().collect(joining()), LANGUAGES)); } else if (valueMap.containsKey(TAGS)) { builder.withQuery(new MultiMatchQueryBuilder(valueMap.get(TAGS).stream().collect(joining()), TAGS)); } } return builder.build().getQuery(); } }