com.netflix.search.query.engine.es.ElasticsearchSearcher Maven / Gradle / Ivy
/**
* Copyright 2016 Netflix, Inc.
*
* 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 com.netflix.search.query.engine.es;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.netflix.search.query.Properties;
import com.netflix.search.query.engine.BaseSearcher;
public class ElasticsearchSearcher extends BaseSearcher {
@Override
public String getUrlForGettingDoc(String q, List languages, String dataSetId)
{
return getServerUrl() + "/"+Properties.esDocType.get()+"/_search";
}
@Override
public String getJsonForQuery(String q, List languages, String dataSetId) throws JsonProcessingException
{
Map multiMatchObject = Maps.newHashMap();
multiMatchObject.put("query", q);
multiMatchObject.put("type", "best_fields");
multiMatchObject.put("operator", "and");
multiMatchObject.put("fields", getQueryFields(languages).split("\\s+"));
Map queryObject = Maps.newHashMap();
queryObject.put("multi_match", multiMatchObject);
Map termObject = Maps.newHashMap();
termObject.put(Properties.docTypeFieldName.get(), dataSetId);
Map filterObject = Maps.newHashMap();
filterObject.put("term", termObject);
Map filteredObject = Maps.newHashMap();
filteredObject.put("query", queryObject);
filteredObject.put("filter", filterObject);
Map sortFieldObject = Maps.newHashMap();
sortFieldObject.put("order", "desc");
Map sortObject = Maps.newHashMap();
sortObject.put(Properties.idField.get(), sortFieldObject);
Map topLevelQueryObject = Maps.newHashMap();
topLevelQueryObject.put("filtered", filteredObject);
Map topNode = Maps.newHashMap();
topNode.put("query", topLevelQueryObject);
topNode.put("sort", sortObject);
JsonNode node = new ObjectMapper().valueToTree(topNode);
StringBuilder jsonString = new StringBuilder();
jsonString.append(new ObjectMapper().writeValueAsString(node));
return jsonString.toString();
}
@Override
public Set getResultsFromServerResponse(String output) throws JsonProcessingException, IOException
{
Set results = Sets.newHashSet();
ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree(output);
JsonNode arrNode = actualObj.get("hits").get("hits");
if (arrNode.isArray())
{
for (final JsonNode objNode : arrNode)
{
results.add(objNode.get("_id").textValue());
}
}
return results;
}
}