From 7bfe151a0e85c262939a437e070a464cd286173e Mon Sep 17 00:00:00 2001 From: "dengchao@xgtl" <2325690622@qq.com> Date: Thu, 16 Apr 2020 10:16:06 +0800 Subject: [PATCH] Fix HighlightBuilder's package path --- .../es/KbsTopicCommentRepositoryImpl.java | 2 +- .../es/KbsTopicRepositoryImpl.java | 2 +- .../es/QuickReplyRepositoryImpl.java | 2 +- .../persistence/es/TopicRepositoryImpl.java | 2 +- .../cc/persistence/es/UKResultMapper.java | 416 +++++++++--------- .../repository/XiaoEUKResultMapper.java | 416 +++++++++--------- 6 files changed, 420 insertions(+), 420 deletions(-) diff --git a/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/KbsTopicCommentRepositoryImpl.java b/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/KbsTopicCommentRepositoryImpl.java index b66f803f..ec973124 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/KbsTopicCommentRepositoryImpl.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/KbsTopicCommentRepositoryImpl.java @@ -21,7 +21,7 @@ import com.chatopera.cc.model.Topic; import org.apache.commons.lang.StringUtils; import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.QueryStringQueryBuilder; -import org.elasticsearch.search.highlight.HighlightBuilder; +import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortOrder; import org.springframework.beans.factory.annotation.Autowired; diff --git a/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/KbsTopicRepositoryImpl.java b/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/KbsTopicRepositoryImpl.java index 9c332937..49ae7412 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/KbsTopicRepositoryImpl.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/KbsTopicRepositoryImpl.java @@ -19,7 +19,7 @@ package com.chatopera.cc.persistence.es; import com.chatopera.cc.model.KbsTopic; import org.apache.commons.lang.StringUtils; import org.elasticsearch.index.query.*; -import org.elasticsearch.search.highlight.HighlightBuilder; +import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortOrder; import org.springframework.beans.factory.annotation.Autowired; diff --git a/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/QuickReplyRepositoryImpl.java b/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/QuickReplyRepositoryImpl.java index 39ac848b..98860499 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/QuickReplyRepositoryImpl.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/QuickReplyRepositoryImpl.java @@ -20,7 +20,7 @@ import com.chatopera.cc.basic.MainContext; import com.chatopera.cc.model.QuickReply; import org.apache.commons.lang.StringUtils; import org.elasticsearch.index.query.*; -import org.elasticsearch.search.highlight.HighlightBuilder; +import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortOrder; import org.springframework.beans.factory.annotation.Autowired; diff --git a/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/TopicRepositoryImpl.java b/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/TopicRepositoryImpl.java index 00e32d83..2048eb27 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/TopicRepositoryImpl.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/TopicRepositoryImpl.java @@ -20,7 +20,7 @@ import com.chatopera.cc.model.Topic; import com.chatopera.cc.persistence.repository.XiaoEUKResultMapper; import org.apache.commons.lang.StringUtils; import org.elasticsearch.index.query.*; -import org.elasticsearch.search.highlight.HighlightBuilder; +import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortOrder; import org.springframework.beans.factory.annotation.Autowired; diff --git a/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/UKResultMapper.java b/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/UKResultMapper.java index df355d8f..7e032447 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/UKResultMapper.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/persistence/es/UKResultMapper.java @@ -1,208 +1,208 @@ -/* - * Copyright (C) 2017 优客服-多渠道客服系统 - * Modifications copyright (C) 2018-2019 Chatopera 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.chatopera.cc.persistence.es; - -import com.fasterxml.jackson.core.JsonEncoding; -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonGenerator; -import org.apache.commons.beanutils.PropertyUtils; -import org.apache.commons.lang.StringUtils; -import org.elasticsearch.action.get.GetResponse; -import org.elasticsearch.action.get.MultiGetItemResponse; -import org.elasticsearch.action.get.MultiGetResponse; -import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchHitField; -import org.elasticsearch.search.highlight.HighlightField; -import org.springframework.data.domain.Pageable; -import org.springframework.data.elasticsearch.ElasticsearchException; -import org.springframework.data.elasticsearch.annotations.Document; -import org.springframework.data.elasticsearch.annotations.ScriptedField; -import org.springframework.data.elasticsearch.core.AbstractResultMapper; -import org.springframework.data.elasticsearch.core.DefaultEntityMapper; -import org.springframework.data.elasticsearch.core.EntityMapper; -import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; -import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl; -import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; -import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; -import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.mapping.context.MappingContext; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.nio.charset.Charset; -import java.util.*; - -public class UKResultMapper extends AbstractResultMapper { - - private MappingContext, ElasticsearchPersistentProperty> mappingContext; - - public UKResultMapper() { - super(new DefaultEntityMapper()); - } - - public UKResultMapper(MappingContext, ElasticsearchPersistentProperty> mappingContext) { - super(new DefaultEntityMapper()); - this.mappingContext = mappingContext; - } - - public UKResultMapper(EntityMapper entityMapper) { - super(entityMapper); - } - - public UKResultMapper( - MappingContext, ElasticsearchPersistentProperty> mappingContext, - EntityMapper entityMapper) { - super(entityMapper); - this.mappingContext = mappingContext; - } - - @Override - public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { - long totalHits = response.getHits().totalHits(); - List results = new ArrayList(); - for (SearchHit hit : response.getHits()) { - if (hit != null) { - T result = null; - if (StringUtils.isNotBlank(hit.sourceAsString())) { - result = mapEntity(hit.sourceAsString() , hit , clazz); - } else { - result = mapEntity(hit.getFields().values() , hit , clazz); - } - setPersistentEntityId(result, hit.getId(), clazz); - populateScriptFields(result, hit); - results.add(result); - } - } - - return new AggregatedPageImpl(results, pageable, totalHits); - } - - public T mapEntity(String source , SearchHit hit , Class clazz) { - T t = mapEntity(source , clazz) ; - - Map highlightFields = hit.getHighlightFields(); - HighlightField highlightNameField = highlightFields.get("title"); - HighlightField contentHightlightField = highlightFields.get("content"); - try { - if(highlightNameField!=null&&highlightNameField.fragments()!=null){ - PropertyUtils.setProperty(t, "title" , highlightNameField.fragments()[0].string()); - } - if(contentHightlightField!=null){ - PropertyUtils.setProperty(t, "content" , contentHightlightField.fragments()[0].string()); - } - PropertyUtils.setProperty(t, "id" , hit.getId()); - } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - e.printStackTrace(); - } - return t; - } - - private void populateScriptFields(T result, SearchHit hit) { - if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) { - for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) { - ScriptedField scriptedField = field.getAnnotation(ScriptedField.class); - if (scriptedField != null) { - String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name(); - SearchHitField searchHitField = hit.getFields().get(name); - if (searchHitField != null) { - field.setAccessible(true); - try { - if(name.equals("title") && hit.getHighlightFields().get("title")!=null){ - field.set(result, hit.getHighlightFields().get("title").fragments()[0].string()); - }else{ - field.set(result, searchHitField.getValue()); - } - } catch (IllegalArgumentException e) { - throw new ElasticsearchException("failed to set scripted field: " + name + " with value: " - + searchHitField.getValue(), e); - } catch (IllegalAccessException e) { - throw new ElasticsearchException("failed to access scripted field: " + name, e); - } - } - } - } - } - } - - - public T mapEntity(Collection values, SearchHit hit , Class clazz) { - return mapEntity(buildJSONFromFields(values) , hit , clazz); - } - - private String buildJSONFromFields(Collection values) { - JsonFactory nodeFactory = new JsonFactory(); - try { - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8); - generator.writeStartObject(); - for (SearchHitField value : values) { - if (value.getValues().size() > 1) { - generator.writeArrayFieldStart(value.getName()); - for (Object val : value.getValues()) { - generator.writeObject(val); - } - generator.writeEndArray(); - } else { - generator.writeObjectField(value.getName(), value.getValue()); - } - } - generator.writeEndObject(); - generator.flush(); - return new String(stream.toByteArray(), Charset.forName("UTF-8")); - } catch (IOException e) { - return null; - } - } - - @Override - public T mapResult(GetResponse response, Class clazz) { - T result = mapEntity(response.getSourceAsString(), clazz); - if (result != null) { - setPersistentEntityId(result, response.getId(), clazz); - } - return result; - } - - @Override - public LinkedList mapResults(MultiGetResponse responses, Class clazz) { - LinkedList list = new LinkedList(); - for (MultiGetItemResponse response : responses.getResponses()) { - if (!response.isFailed() && response.getResponse().isExists()) { - T result = mapEntity(response.getResponse().getSourceAsString(), clazz); - setPersistentEntityId(result, response.getResponse().getId(), clazz); - list.add(result); - } - } - return list; - } - - private void setPersistentEntityId(T result, String id, Class clazz) { - - if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) { - - ElasticsearchPersistentEntity persistentEntity = mappingContext.getPersistentEntity(clazz); - PersistentProperty idProperty = persistentEntity.getIdProperty(); - - // Only deal with String because ES generated Ids are strings ! - if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) { - persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id); - } - } - } -} \ No newline at end of file +/* + * Copyright (C) 2017 优客服-多渠道客服系统 + * Modifications copyright (C) 2018-2019 Chatopera 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.chatopera.cc.persistence.es; + +import com.fasterxml.jackson.core.JsonEncoding; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonGenerator; +import org.apache.commons.beanutils.PropertyUtils; +import org.apache.commons.lang.StringUtils; +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.get.MultiGetItemResponse; +import org.elasticsearch.action.get.MultiGetResponse; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHitField; +import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; +import org.springframework.data.domain.Pageable; +import org.springframework.data.elasticsearch.ElasticsearchException; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.ScriptedField; +import org.springframework.data.elasticsearch.core.AbstractResultMapper; +import org.springframework.data.elasticsearch.core.DefaultEntityMapper; +import org.springframework.data.elasticsearch.core.EntityMapper; +import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; +import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl; +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; +import org.springframework.data.mapping.PersistentProperty; +import org.springframework.data.mapping.context.MappingContext; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.nio.charset.StandardCharsets; +import java.util.*; + +public class UKResultMapper extends AbstractResultMapper { + + private MappingContext, ElasticsearchPersistentProperty> mappingContext; + + public UKResultMapper() { + super(new DefaultEntityMapper()); + } + + public UKResultMapper(MappingContext, ElasticsearchPersistentProperty> mappingContext) { + super(new DefaultEntityMapper()); + this.mappingContext = mappingContext; + } + + public UKResultMapper(EntityMapper entityMapper) { + super(entityMapper); + } + + public UKResultMapper( + MappingContext, ElasticsearchPersistentProperty> mappingContext, + EntityMapper entityMapper) { + super(entityMapper); + this.mappingContext = mappingContext; + } + + @Override + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { + long totalHits = response.getHits().totalHits(); + List results = new ArrayList(); + for (SearchHit hit : response.getHits()) { + if (hit != null) { + T result = null; + if (StringUtils.isNotBlank(hit.sourceAsString())) { + result = mapEntity(hit.sourceAsString() , hit , clazz); + } else { + result = mapEntity(hit.getFields().values() , hit , clazz); + } + setPersistentEntityId(result, hit.getId(), clazz); + populateScriptFields(result, hit); + results.add(result); + } + } + + return new AggregatedPageImpl(results, pageable, totalHits); + } + + public T mapEntity(String source , SearchHit hit , Class clazz) { + T t = mapEntity(source , clazz) ; + + Map highlightFields = hit.getHighlightFields(); + HighlightField highlightNameField = highlightFields.get("title"); + HighlightField contentHightlightField = highlightFields.get("content"); + try { + if(highlightNameField!=null&&highlightNameField.fragments()!=null){ + PropertyUtils.setProperty(t, "title" , highlightNameField.fragments()[0].string()); + } + if(contentHightlightField!=null){ + PropertyUtils.setProperty(t, "content" , contentHightlightField.fragments()[0].string()); + } + PropertyUtils.setProperty(t, "id" , hit.getId()); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); + } + return t; + } + + private void populateScriptFields(T result, SearchHit hit) { + if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) { + for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) { + ScriptedField scriptedField = field.getAnnotation(ScriptedField.class); + if (scriptedField != null) { + String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name(); + SearchHitField searchHitField = hit.getFields().get(name); + if (searchHitField != null) { + field.setAccessible(true); + try { + if(name.equals("title") && hit.getHighlightFields().get("title")!=null){ + field.set(result, hit.getHighlightFields().get("title").fragments()[0].string()); + }else{ + field.set(result, searchHitField.getValue()); + } + } catch (IllegalArgumentException e) { + throw new ElasticsearchException("failed to set scripted field: " + name + " with value: " + + searchHitField.getValue(), e); + } catch (IllegalAccessException e) { + throw new ElasticsearchException("failed to access scripted field: " + name, e); + } + } + } + } + } + } + + + public T mapEntity(Collection values, SearchHit hit , Class clazz) { + return mapEntity(buildJSONFromFields(values) , hit , clazz); + } + + private String buildJSONFromFields(Collection values) { + JsonFactory nodeFactory = new JsonFactory(); + try { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8); + generator.writeStartObject(); + for (SearchHitField value : values) { + if (value.getValues().size() > 1) { + generator.writeArrayFieldStart(value.getName()); + for (Object val : value.getValues()) { + generator.writeObject(val); + } + generator.writeEndArray(); + } else { + generator.writeObjectField(value.getName(), value.getValue()); + } + } + generator.writeEndObject(); + generator.flush(); + return new String(stream.toByteArray(), StandardCharsets.UTF_8); + } catch (IOException e) { + return null; + } + } + + @Override + public T mapResult(GetResponse response, Class clazz) { + T result = mapEntity(response.getSourceAsString(), clazz); + if (result != null) { + setPersistentEntityId(result, response.getId(), clazz); + } + return result; + } + + @Override + public LinkedList mapResults(MultiGetResponse responses, Class clazz) { + LinkedList list = new LinkedList(); + for (MultiGetItemResponse response : responses.getResponses()) { + if (!response.isFailed() && response.getResponse().isExists()) { + T result = mapEntity(response.getResponse().getSourceAsString(), clazz); + setPersistentEntityId(result, response.getResponse().getId(), clazz); + list.add(result); + } + } + return list; + } + + private void setPersistentEntityId(T result, String id, Class clazz) { + + if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) { + + ElasticsearchPersistentEntity persistentEntity = mappingContext.getPersistentEntity(clazz); + PersistentProperty idProperty = persistentEntity.getIdProperty(); + + // Only deal with String because ES generated Ids are strings ! + if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) { + persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id); + } + } + } +} diff --git a/contact-center/app/src/main/java/com/chatopera/cc/persistence/repository/XiaoEUKResultMapper.java b/contact-center/app/src/main/java/com/chatopera/cc/persistence/repository/XiaoEUKResultMapper.java index e30b8192..74fcc3c4 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/persistence/repository/XiaoEUKResultMapper.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/persistence/repository/XiaoEUKResultMapper.java @@ -1,208 +1,208 @@ -/* - * Copyright (C) 2017 优客服-多渠道客服系统 - * Modifications copyright (C) 2018-2019 Chatopera 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.chatopera.cc.persistence.repository; - -import com.fasterxml.jackson.core.JsonEncoding; -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonGenerator; -import org.apache.commons.beanutils.PropertyUtils; -import org.apache.commons.lang.StringUtils; -import org.elasticsearch.action.get.GetResponse; -import org.elasticsearch.action.get.MultiGetItemResponse; -import org.elasticsearch.action.get.MultiGetResponse; -import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchHitField; -import org.elasticsearch.search.highlight.HighlightField; -import org.springframework.data.domain.Pageable; -import org.springframework.data.elasticsearch.ElasticsearchException; -import org.springframework.data.elasticsearch.annotations.Document; -import org.springframework.data.elasticsearch.annotations.ScriptedField; -import org.springframework.data.elasticsearch.core.AbstractResultMapper; -import org.springframework.data.elasticsearch.core.DefaultEntityMapper; -import org.springframework.data.elasticsearch.core.EntityMapper; -import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; -import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl; -import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; -import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; -import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.mapping.context.MappingContext; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.nio.charset.Charset; -import java.util.*; - -public class XiaoEUKResultMapper extends AbstractResultMapper { - - private MappingContext, ElasticsearchPersistentProperty> mappingContext; - - public XiaoEUKResultMapper() { - super(new DefaultEntityMapper()); - } - - public XiaoEUKResultMapper(MappingContext, ElasticsearchPersistentProperty> mappingContext) { - super(new DefaultEntityMapper()); - this.mappingContext = mappingContext; - } - - public XiaoEUKResultMapper(EntityMapper entityMapper) { - super(entityMapper); - } - - public XiaoEUKResultMapper( - MappingContext, ElasticsearchPersistentProperty> mappingContext, - EntityMapper entityMapper) { - super(entityMapper); - this.mappingContext = mappingContext; - } - - @Override - public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { - long totalHits = response.getHits().totalHits(); - List results = new ArrayList(); - for (SearchHit hit : response.getHits()) { - if (hit != null) { - T result = null; - if (StringUtils.isNotBlank(hit.sourceAsString())) { - result = mapEntity(hit.sourceAsString() , hit , clazz); - } else { - result = mapEntity(hit.getFields().values() , hit , clazz); - } - setPersistentEntityId(result, hit.getId(), clazz); - populateScriptFields(result, hit); - results.add(result); - } - } - - return new AggregatedPageImpl(results, pageable, totalHits); - } - - public T mapEntity(String source , SearchHit hit , Class clazz) { - T t = mapEntity(source , clazz) ; - - Map highlightFields = hit.getHighlightFields(); - HighlightField highlightNameField = highlightFields.get("title"); - HighlightField contentHightlightField = highlightFields.get("content"); - try { - if(highlightNameField!=null&&highlightNameField.fragments()!=null){ - PropertyUtils.setProperty(t, "title" , highlightNameField.fragments()[0].string()); - } - if(contentHightlightField!=null){ - PropertyUtils.setProperty(t, "content" , contentHightlightField.fragments()[0].string()); - } - PropertyUtils.setProperty(t, "id" , hit.getId()); - } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - e.printStackTrace(); - } - return t; - } - - private void populateScriptFields(T result, SearchHit hit) { - if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) { - for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) { - ScriptedField scriptedField = field.getAnnotation(ScriptedField.class); - if (scriptedField != null) { - String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name(); - SearchHitField searchHitField = hit.getFields().get(name); - if (searchHitField != null) { - field.setAccessible(true); - try { - if(name.equals("title") && hit.getHighlightFields().get("title")!=null){ - field.set(result, hit.getHighlightFields().get("title").fragments()[0].string()); - }else{ - field.set(result, searchHitField.getValue()); - } - } catch (IllegalArgumentException e) { - throw new ElasticsearchException("failed to set scripted field: " + name + " with value: " - + searchHitField.getValue(), e); - } catch (IllegalAccessException e) { - throw new ElasticsearchException("failed to access scripted field: " + name, e); - } - } - } - } - } - } - - - public T mapEntity(Collection values, SearchHit hit , Class clazz) { - return mapEntity(buildJSONFromFields(values) , hit , clazz); - } - - private String buildJSONFromFields(Collection values) { - JsonFactory nodeFactory = new JsonFactory(); - try { - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8); - generator.writeStartObject(); - for (SearchHitField value : values) { - if (value.getValues().size() > 1) { - generator.writeArrayFieldStart(value.getName()); - for (Object val : value.getValues()) { - generator.writeObject(val); - } - generator.writeEndArray(); - } else { - generator.writeObjectField(value.getName(), value.getValue()); - } - } - generator.writeEndObject(); - generator.flush(); - return new String(stream.toByteArray(), Charset.forName("UTF-8")); - } catch (IOException e) { - return null; - } - } - - @Override - public T mapResult(GetResponse response, Class clazz) { - T result = mapEntity(response.getSourceAsString(), clazz); - if (result != null) { - setPersistentEntityId(result, response.getId(), clazz); - } - return result; - } - - @Override - public LinkedList mapResults(MultiGetResponse responses, Class clazz) { - LinkedList list = new LinkedList(); - for (MultiGetItemResponse response : responses.getResponses()) { - if (!response.isFailed() && response.getResponse().isExists()) { - T result = mapEntity(response.getResponse().getSourceAsString(), clazz); - setPersistentEntityId(result, response.getResponse().getId(), clazz); - list.add(result); - } - } - return list; - } - - private void setPersistentEntityId(T result, String id, Class clazz) { - - if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) { - - ElasticsearchPersistentEntity persistentEntity = mappingContext.getPersistentEntity(clazz); - PersistentProperty idProperty = persistentEntity.getIdProperty(); - - // Only deal with String because ES generated Ids are strings ! - if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) { - persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id); - } - } - } -} \ No newline at end of file +/* + * Copyright (C) 2017 优客服-多渠道客服系统 + * Modifications copyright (C) 2018-2019 Chatopera 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.chatopera.cc.persistence.repository; + +import com.fasterxml.jackson.core.JsonEncoding; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonGenerator; +import org.apache.commons.beanutils.PropertyUtils; +import org.apache.commons.lang.StringUtils; +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.get.MultiGetItemResponse; +import org.elasticsearch.action.get.MultiGetResponse; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHitField; +import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; +import org.springframework.data.domain.Pageable; +import org.springframework.data.elasticsearch.ElasticsearchException; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.ScriptedField; +import org.springframework.data.elasticsearch.core.AbstractResultMapper; +import org.springframework.data.elasticsearch.core.DefaultEntityMapper; +import org.springframework.data.elasticsearch.core.EntityMapper; +import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; +import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl; +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; +import org.springframework.data.mapping.PersistentProperty; +import org.springframework.data.mapping.context.MappingContext; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.nio.charset.StandardCharsets; +import java.util.*; + +public class XiaoEUKResultMapper extends AbstractResultMapper { + + private MappingContext, ElasticsearchPersistentProperty> mappingContext; + + public XiaoEUKResultMapper() { + super(new DefaultEntityMapper()); + } + + public XiaoEUKResultMapper(MappingContext, ElasticsearchPersistentProperty> mappingContext) { + super(new DefaultEntityMapper()); + this.mappingContext = mappingContext; + } + + public XiaoEUKResultMapper(EntityMapper entityMapper) { + super(entityMapper); + } + + public XiaoEUKResultMapper( + MappingContext, ElasticsearchPersistentProperty> mappingContext, + EntityMapper entityMapper) { + super(entityMapper); + this.mappingContext = mappingContext; + } + + @Override + public AggregatedPage mapResults(SearchResponse response, Class clazz, Pageable pageable) { + long totalHits = response.getHits().totalHits(); + List results = new ArrayList(); + for (SearchHit hit : response.getHits()) { + if (hit != null) { + T result = null; + if (StringUtils.isNotBlank(hit.sourceAsString())) { + result = mapEntity(hit.sourceAsString() , hit , clazz); + } else { + result = mapEntity(hit.getFields().values() , hit , clazz); + } + setPersistentEntityId(result, hit.getId(), clazz); + populateScriptFields(result, hit); + results.add(result); + } + } + + return new AggregatedPageImpl(results, pageable, totalHits); + } + + public T mapEntity(String source , SearchHit hit , Class clazz) { + T t = mapEntity(source , clazz) ; + + Map highlightFields = hit.getHighlightFields(); + HighlightField highlightNameField = highlightFields.get("title"); + HighlightField contentHightlightField = highlightFields.get("content"); + try { + if(highlightNameField!=null&&highlightNameField.fragments()!=null){ + PropertyUtils.setProperty(t, "title" , highlightNameField.fragments()[0].string()); + } + if(contentHightlightField!=null){ + PropertyUtils.setProperty(t, "content" , contentHightlightField.fragments()[0].string()); + } + PropertyUtils.setProperty(t, "id" , hit.getId()); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); + } + return t; + } + + private void populateScriptFields(T result, SearchHit hit) { + if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) { + for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) { + ScriptedField scriptedField = field.getAnnotation(ScriptedField.class); + if (scriptedField != null) { + String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name(); + SearchHitField searchHitField = hit.getFields().get(name); + if (searchHitField != null) { + field.setAccessible(true); + try { + if(name.equals("title") && hit.getHighlightFields().get("title")!=null){ + field.set(result, hit.getHighlightFields().get("title").fragments()[0].string()); + }else{ + field.set(result, searchHitField.getValue()); + } + } catch (IllegalArgumentException e) { + throw new ElasticsearchException("failed to set scripted field: " + name + " with value: " + + searchHitField.getValue(), e); + } catch (IllegalAccessException e) { + throw new ElasticsearchException("failed to access scripted field: " + name, e); + } + } + } + } + } + } + + + public T mapEntity(Collection values, SearchHit hit , Class clazz) { + return mapEntity(buildJSONFromFields(values) , hit , clazz); + } + + private String buildJSONFromFields(Collection values) { + JsonFactory nodeFactory = new JsonFactory(); + try { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8); + generator.writeStartObject(); + for (SearchHitField value : values) { + if (value.getValues().size() > 1) { + generator.writeArrayFieldStart(value.getName()); + for (Object val : value.getValues()) { + generator.writeObject(val); + } + generator.writeEndArray(); + } else { + generator.writeObjectField(value.getName(), value.getValue()); + } + } + generator.writeEndObject(); + generator.flush(); + return new String(stream.toByteArray(), StandardCharsets.UTF_8); + } catch (IOException e) { + return null; + } + } + + @Override + public T mapResult(GetResponse response, Class clazz) { + T result = mapEntity(response.getSourceAsString(), clazz); + if (result != null) { + setPersistentEntityId(result, response.getId(), clazz); + } + return result; + } + + @Override + public LinkedList mapResults(MultiGetResponse responses, Class clazz) { + LinkedList list = new LinkedList(); + for (MultiGetItemResponse response : responses.getResponses()) { + if (!response.isFailed() && response.getResponse().isExists()) { + T result = mapEntity(response.getResponse().getSourceAsString(), clazz); + setPersistentEntityId(result, response.getResponse().getId(), clazz); + list.add(result); + } + } + return list; + } + + private void setPersistentEntityId(T result, String id, Class clazz) { + + if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) { + + ElasticsearchPersistentEntity persistentEntity = mappingContext.getPersistentEntity(clazz); + PersistentProperty idProperty = persistentEntity.getIdProperty(); + + // Only deal with String because ES generated Ids are strings ! + if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) { + persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id); + } + } + } +}