Authored by Gino Zhang

将searchExplainer移到service

package com.yoho.search.service.searchexplainer;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import com.carrotsearch.hppc.ObjectLookupContainer;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.yoho.search.base.utils.ISearchConstants;
... ... @@ -15,7 +13,6 @@ import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -23,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
... ... @@ -455,60 +453,68 @@ public class SearchExplainerService {
IElasticsearchClient client = esClientMgr.getClient(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
GetMappingsResponse response = client.getMapping(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
Assert.notNull(response, ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
ImmutableOpenMap<String, MappingMetaData> mapping = response.getMappings().get(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
Iterator<ObjectObjectCursor<String, MappingMetaData>> iterator = mapping.iterator();
while(iterator.hasNext())
{
ObjectObjectCursor<String, MappingMetaData> object = iterator.next();
//object.value.getSourceAsMap();
ObjectLookupContainer<String> keys = response.getMappings().keys();
if (keys.isEmpty()) {
throw new RuntimeException("Cannot get field mapping!");
}
// String mappingContent = yohoIndexService.getIndex(ISearchConstants.INDEX_NAME_PRODUCT_INDEX).getMappingContent();
// JSONObject jsonObject = JSON.parseObject(mappingContent);
// JSONObject mapping = jsonObject.getJSONObject(ISearchConstants.INDEX_NAME_PRODUCT_INDEX).getJSONObject("properties");
// Map<String, FieldDesc> fieldDescMap = new HashMap<>();
// for (String field : mapping.keySet()) {
// parseToFieldDesc(fieldDescMap, field, mapping.getJSONObject(field));
// }
String realIndexName = Arrays.asList(keys.toArray(String.class)).get(0);
MappingMetaData mappingMetaData = response.getMappings().get(realIndexName).get(ISearchConstants.INDEX_NAME_PRODUCT_INDEX);
if (mappingMetaData == null) {
throw new RuntimeException("Cannot get field mapping!");
}
// localFieldDescMap = fieldDescMap;
return localFieldDescMap;
Map<String, FieldDesc> fieldDescMap = new HashMap<>();
try {
Map<String, Object> sourceAsMap = mappingMetaData.getSourceAsMap();
Map<String, Object> properties = (Map<String, Object>) sourceAsMap.get("properties");
for (Map.Entry<String, Object> p : properties.entrySet()) {
String field = p.getKey();
Map<String, Object> fieldPropMap = (Map<String, Object>) p.getValue();
parseToFieldDesc(fieldDescMap, field, fieldPropMap);
}
localFieldDescMap = fieldDescMap;
return localFieldDescMap;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private void parseToFieldDesc(Map<String, FieldDesc> fieldDescMap, String field, JSONObject jsonObject) {
if (jsonObject.containsKey("fields")) {
private void parseToFieldDesc(Map<String, FieldDesc> fieldDescMap, String field, Map<String, Object> fieldPropMap) {
if (fieldPropMap.containsKey("fields")) {
// 多字段类型
JSONObject innerJsonObject = jsonObject.getJSONObject("fields");
for (String innerField : innerJsonObject.keySet()) {
Map<String, Object> innerFieldsMap = (Map<String, Object>) fieldPropMap.get("fields");
for (String innerField : innerFieldsMap.keySet()) {
String realFieldName = innerField;
if (!innerField.equals(field)) {
realFieldName = field + "." + innerField;
}
parseSingle(fieldDescMap, realFieldName, innerJsonObject.getJSONObject(innerField));
parseSingle(fieldDescMap, realFieldName, (Map<String, Object>) innerFieldsMap.get(innerField));
}
} else if (jsonObject.containsKey("properties")) {
} else if (fieldPropMap.containsKey("properties")) {
// nested object类型 搜索暂不支持查询内嵌对象的字段
} else {
parseSingle(fieldDescMap, field, jsonObject);
parseSingle(fieldDescMap, field, fieldPropMap);
}
}
private void parseSingle(Map<String, FieldDesc> fieldDescMap, String field, JSONObject jsonObject) {
private void parseSingle(Map<String, FieldDesc> fieldDescMap, String field, Map<String, Object> fieldPropMap) {
FieldDesc fieldDesc = new FieldDesc();
fieldDesc.field = field;
fieldDesc.type = jsonObject.getString("type");
fieldDesc.index = jsonObject.getString("index");
fieldDesc.analyzer = jsonObject.getString("analyzer");
fieldDesc.search_analyzer = jsonObject.getString("search_analyzer");
Object copyTo = jsonObject.get("copy_to");
if (copyTo != null && copyTo instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) copyTo;
for (int i = 0; i < jsonArray.size(); i++) {
fieldDesc.addCopyTo(jsonArray.getString(i));
fieldDesc.type = (String) fieldPropMap.get("type");
fieldDesc.index = (String) fieldPropMap.get("index");
fieldDesc.analyzer = (String) fieldPropMap.get("analyzer");
fieldDesc.search_analyzer = (String) fieldPropMap.get("search_analyzer");
Object copyTo = fieldPropMap.get("copy_to");
if (copyTo != null && copyTo instanceof List) {
List<String> copyToFields = (List<String>) copyTo;
for (int i = 0; i < copyToFields.size(); i++) {
fieldDesc.addCopyTo(copyToFields.get(i));
}
} else if (copyTo != null && copyTo instanceof JSONObject) {
} else if (copyTo != null && copyTo instanceof String) {
fieldDesc.addCopyTo((String) copyTo);
}
... ...
... ... @@ -49,6 +49,6 @@ zkAddress=192.168.102.216:2181
#web.context
web.context=yohosearch
search.multiMatchQuery.type=BEST_FIELDS
search.multiMatchQuery.type=CROSS_FIELDS
search.open.downgrade=true
... ...