|
@@ -24,6 +24,7 @@ import java.util.ListIterator;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.codelibs.core.stream.StreamUtil;
|
|
|
import org.codelibs.fess.helper.QueryHelper;
|
|
|
import org.codelibs.fess.helper.ViewHelper;
|
|
|
import org.codelibs.fess.mylasta.direction.FessConfig;
|
|
@@ -31,6 +32,7 @@ import org.dbflute.optional.OptionalEntity;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.common.text.Text;
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
|
+import org.elasticsearch.search.SearchHitField;
|
|
|
import org.elasticsearch.search.SearchHits;
|
|
|
import org.elasticsearch.search.aggregations.Aggregations;
|
|
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
|
@@ -84,6 +86,7 @@ public class QueryResponseList implements List<Map<String, Object>> {
|
|
|
|
|
|
public void init(final OptionalEntity<SearchResponse> searchResponseOpt, final int start, final int pageSize) {
|
|
|
searchResponseOpt.ifPresent(searchResponse -> {
|
|
|
+ final FessConfig fessConfig = ComponentUtil.getFessConfig();
|
|
|
final SearchHits searchHits = searchResponse.getHits();
|
|
|
allRecordCount = searchHits.getTotalHits();
|
|
|
queryTime = searchResponse.getTookInMillis();
|
|
@@ -96,45 +99,28 @@ public class QueryResponseList implements List<Map<String, Object>> {
|
|
|
final QueryHelper queryHelper = ComponentUtil.getQueryHelper();
|
|
|
final String hlPrefix = queryHelper.getHighlightPrefix();
|
|
|
for (final SearchHit searchHit : searchHits.getHits()) {
|
|
|
- final Map<String, Object> docMap = new HashMap<>();
|
|
|
- if (searchHit.getSource() == null) {
|
|
|
- searchHit.getFields().forEach((key, value) -> {
|
|
|
- docMap.put(key, value.getValue());
|
|
|
- });
|
|
|
- } else {
|
|
|
- docMap.putAll(searchHit.getSource());
|
|
|
- }
|
|
|
-
|
|
|
- final Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
|
|
|
- try {
|
|
|
- if (highlightFields != null) {
|
|
|
- for (final Map.Entry<String, HighlightField> entry : highlightFields.entrySet()) {
|
|
|
- final HighlightField highlightField = entry.getValue();
|
|
|
- final Text[] fragments = highlightField.fragments();
|
|
|
- if (fragments != null && fragments.length != 0) {
|
|
|
- final String[] texts = new String[fragments.length];
|
|
|
- for (int i = 0; i < fragments.length; i++) {
|
|
|
- texts[i] = fragments[i].string();
|
|
|
+ final Map<String, Object> docMap = parseSearchHit(fessConfig, hlPrefix, searchHit);
|
|
|
+
|
|
|
+ if (fessConfig.isResultCollapsed()) {
|
|
|
+ final Map<String, SearchHits> innerHits = searchHit.getInnerHits();
|
|
|
+ if (innerHits != null) {
|
|
|
+ final SearchHits innerSearchHits = innerHits.get(fessConfig.getQueryCollapseInnerHitsName());
|
|
|
+ if (innerSearchHits != null) {
|
|
|
+ long totalHits = innerSearchHits.getTotalHits();
|
|
|
+ if (totalHits > 0) {
|
|
|
+ docMap.put(fessConfig.getQueryCollapseInnerHitsName() + "_count", totalHits);
|
|
|
+ SearchHitField bitsField = searchHit.getFields().get(fessConfig.getIndexFieldContentMinhashBits());
|
|
|
+ if (bitsField != null && !bitsField.getValues().isEmpty()) {
|
|
|
+ docMap.put(fessConfig.getQueryCollapseInnerHitsName() + "_hash", bitsField.getValues().get(0));
|
|
|
}
|
|
|
- final String value = StringUtils.join(texts, "...");
|
|
|
- docMap.put(hlPrefix + highlightField.getName(), value);
|
|
|
+ docMap.put(
|
|
|
+ fessConfig.getQueryCollapseInnerHitsName(),
|
|
|
+ StreamUtil.stream(innerSearchHits.getHits()).get(
|
|
|
+ stream -> stream.map(v -> parseSearchHit(fessConfig, hlPrefix, v)).toArray(
|
|
|
+ n -> new Map[n])));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- } catch (final Exception e) {
|
|
|
- if (logger.isDebugEnabled()) {
|
|
|
- logger.debug("Could not create a highlighting value: " + docMap, e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // ContentTitle
|
|
|
- final ViewHelper viewHelper = ComponentUtil.getViewHelper();
|
|
|
- if (viewHelper != null) {
|
|
|
- final FessConfig fessConfig = ComponentUtil.getFessConfig();
|
|
|
- docMap.put(fessConfig.getResponseFieldContentTitle(), viewHelper.getContentTitle(docMap));
|
|
|
- docMap.put(fessConfig.getResponseFieldContentDescription(), viewHelper.getContentDescription(docMap));
|
|
|
- docMap.put(fessConfig.getResponseFieldUrlLink(), viewHelper.getUrlLink(docMap));
|
|
|
- docMap.put(fessConfig.getResponseFieldSitePath(), viewHelper.getSitePath(docMap));
|
|
|
}
|
|
|
|
|
|
parent.add(docMap);
|
|
@@ -151,6 +137,49 @@ public class QueryResponseList implements List<Map<String, Object>> {
|
|
|
calculatePageInfo(start, pageSize);
|
|
|
}
|
|
|
|
|
|
+ private Map<String, Object> parseSearchHit(final FessConfig fessConfig, final String hlPrefix, final SearchHit searchHit) {
|
|
|
+ final Map<String, Object> docMap = new HashMap<>();
|
|
|
+ if (searchHit.getSource() == null) {
|
|
|
+ searchHit.getFields().forEach((key, value) -> {
|
|
|
+ docMap.put(key, value.getValue());
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ docMap.putAll(searchHit.getSource());
|
|
|
+ }
|
|
|
+
|
|
|
+ final Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
|
|
|
+ try {
|
|
|
+ if (highlightFields != null) {
|
|
|
+ for (final Map.Entry<String, HighlightField> entry : highlightFields.entrySet()) {
|
|
|
+ final HighlightField highlightField = entry.getValue();
|
|
|
+ final Text[] fragments = highlightField.fragments();
|
|
|
+ if (fragments != null && fragments.length != 0) {
|
|
|
+ final String[] texts = new String[fragments.length];
|
|
|
+ for (int i = 0; i < fragments.length; i++) {
|
|
|
+ texts[i] = fragments[i].string();
|
|
|
+ }
|
|
|
+ final String value = StringUtils.join(texts, "...");
|
|
|
+ docMap.put(hlPrefix + highlightField.getName(), value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (final Exception e) {
|
|
|
+ if (logger.isDebugEnabled()) {
|
|
|
+ logger.debug("Could not create a highlighting value: " + docMap, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ContentTitle
|
|
|
+ final ViewHelper viewHelper = ComponentUtil.getViewHelper();
|
|
|
+ if (viewHelper != null) {
|
|
|
+ docMap.put(fessConfig.getResponseFieldContentTitle(), viewHelper.getContentTitle(docMap));
|
|
|
+ docMap.put(fessConfig.getResponseFieldContentDescription(), viewHelper.getContentDescription(docMap));
|
|
|
+ docMap.put(fessConfig.getResponseFieldUrlLink(), viewHelper.getUrlLink(docMap));
|
|
|
+ docMap.put(fessConfig.getResponseFieldSitePath(), viewHelper.getSitePath(docMap));
|
|
|
+ }
|
|
|
+ return docMap;
|
|
|
+ }
|
|
|
+
|
|
|
protected void calculatePageInfo(final int start, final int size) {
|
|
|
pageSize = size;
|
|
|
allPageCount = (int) ((allRecordCount - 1) / pageSize) + 1;
|