io.curly.bloodhound.query.SearchTranspiler.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.bloodhound.query.SearchTranspiler.java

Source

/*
 *        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;

import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import javax.inject.Inject;

/**
 * Translates the query into a multi map containing the values and the keys of the query
 * to be processed on {@link ElasticSearchBuilder} and translated on a match query
 *
 * @author Joo Evangelista
 */
@Component
public class SearchTranspiler implements Transpiler {

    private final QueryResolver resolver;

    @Inject
    public SearchTranspiler(QueryResolver resolver) {
        this.resolver = resolver;
    }

    /**
     * Execute the steps to change the query into a multi map, if there is no key the
     * full text search parameter will take place.
     *
     * @param query the user input query
     * @return MultiMap with query items and its values
     */
    @Override
    public MultiValueMap<String, String> execute(String query) {
        Assert.hasText(query, "A query must have text!");
        if (QueryUtils.isParametrized(query)) {
            MultiValueMap<String, String> parameter = QueryParser.resolveMultiParameter(query);
            return resolver.resolve(parameter);
        } else if (QueryUtils.isFullText(query)) {
            MultiValueMap<String, String> fullTextMap = new LinkedMultiValueMap<>(1);
            fullTextMap.add("text", query);
            return fullTextMap;
        }
        return new LinkedMultiValueMap<>();
    }
}