modify search parameters

This commit is contained in:
Shinsuke Sugaya 2015-09-03 22:44:50 +09:00
parent 145a0d94b3
commit 5e6d032789
3 changed files with 92 additions and 89 deletions

View file

@ -35,6 +35,7 @@ import org.codelibs.core.lang.StringUtil;
import org.codelibs.fess.Constants;
import org.codelibs.fess.InvalidQueryException;
import org.codelibs.fess.ResultOffsetExceededException;
import org.codelibs.fess.app.web.RootAction;
import org.codelibs.fess.app.web.RootForm;
import org.codelibs.fess.app.web.base.FessSearchAction;
import org.codelibs.fess.client.FessEsClient.SearchConditionBuilder;
@ -139,7 +140,7 @@ public class SearchAction extends FessSearchAction {
if (StringUtil.isBlank(form.query) && form.fields.isEmpty()) {
// redirect to index page
form.query = null;
return redirect(SearchAction.class);
return redirect(RootAction.class);
}
return asHtml(path_SearchJsp).renderWith(data -> {
@ -272,7 +273,7 @@ public class SearchAction extends FessSearchAction {
form.start = String.valueOf(DEFAULT_START_COUNT);
} else {
try {
Long.parseLong(form.start);
Integer.parseInt(form.start);
} catch (final NumberFormatException e) {
form.start = String.valueOf(DEFAULT_START_COUNT);
}
@ -287,11 +288,14 @@ public class SearchAction extends FessSearchAction {
List<Map<String, Object>> documentItems = null;
try {
documentItems =
fessEsClient.getDocumentList(fieldHelper.docIndex, fieldHelper.docType,
searchRequestBuilder -> {
return SearchConditionBuilder.builder(searchRequestBuilder).query(query).offset(pageStart).size(pageNum)
.facetInfo(form.facet).geoInfo(form.geo).responseFields(queryHelper.getResponseFields()).build();
});
fessEsClient.search(fieldHelper.docIndex, fieldHelper.docType, searchRequestBuilder -> {
return SearchConditionBuilder.builder(searchRequestBuilder).query(query).offset((int) pageStart).size(pageNum)
.facetInfo(form.facet).geoInfo(form.geo).responseFields(queryHelper.getResponseFields()).build();
}, (searchRequestBuilder, execTime, searchResponse) -> {
QueryResponseList queryResponseList = ComponentUtil.getQueryResponseList();
queryResponseList.init(searchResponse, pageStart, pageNum);
return queryResponseList;
});
} catch (final InvalidQueryException e) {
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
@ -310,7 +314,6 @@ public class SearchAction extends FessSearchAction {
// search
final QueryResponseList queryResponseList = (QueryResponseList) documentItems;
data.register("facetResponse", queryResponseList.getFacetResponse());
data.register("moreLikeThisResponse", queryResponseList.getMoreLikeThisResponse());
final NumberFormat nf = NumberFormat.getInstance(LaRequestUtil.getRequest().getLocale());
nf.setMaximumIntegerDigits(2);
nf.setMaximumFractionDigits(2);

View file

@ -23,6 +23,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.codelibs.fess.helper.QueryHelper;
@ -70,8 +71,6 @@ public class QueryResponseList implements List<Map<String, Object>> {
protected FacetResponse facetResponse;
protected MoreLikeThisResponse moreLikeThisResponse;
protected boolean partialResults = false;
protected long queryTime;
@ -85,11 +84,10 @@ public class QueryResponseList implements List<Map<String, Object>> {
this.parent = parent;
}
public void init(final SearchResponse searchResponse, final long start, final int pageSize) {
long numFound = 0;
if (searchResponse != null) {
public void init(final Optional<SearchResponse> searchResponseOpt, final int start, final int pageSize) {
searchResponseOpt.ifPresent(searchResponse -> {
final SearchHits searchHits = searchResponse.getHits();
numFound = searchHits.getTotalHits();
allRecordCount = searchHits.getTotalHits();
queryTime = searchResponse.getTookInMillis();
if (searchResponse.getTotalShards() != searchResponse.getSuccessfulShards()) {
@ -97,65 +95,71 @@ public class QueryResponseList implements List<Map<String, Object>> {
}
// build highlighting fields
final QueryHelper queryHelper = ComponentUtil.getQueryHelper();
ComponentUtil.getFieldHelper();
final String hlPrefix = queryHelper.getHighlightingPrefix();
for (final SearchHit searchHit : searchHits.getHits()) {
final Map<String, Object> docMap = new HashMap<String, Object>();
docMap.putAll(searchHit.getSource());
final QueryHelper queryHelper = ComponentUtil.getQueryHelper();
ComponentUtil.getFieldHelper();
final String hlPrefix = queryHelper.getHighlightingPrefix();
for (final SearchHit searchHit : searchHits.getHits()) {
final Map<String, Object> docMap = new HashMap<String, Object>();
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, 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);
}
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);
}
}
} 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("contentTitle", viewHelper.getContentTitle(docMap));
docMap.put("contentDescription", viewHelper.getContentDescription(docMap));
docMap.put("urlLink", viewHelper.getUrlLink(docMap));
docMap.put("sitePath", viewHelper.getSitePath(docMap));
}
parent.add(docMap);
}
// ContentTitle
final ViewHelper viewHelper = ComponentUtil.getViewHelper();
if (viewHelper != null) {
docMap.put("contentTitle", viewHelper.getContentTitle(docMap));
docMap.put("contentDescription", viewHelper.getContentDescription(docMap));
docMap.put("urlLink", viewHelper.getUrlLink(docMap));
docMap.put("sitePath", viewHelper.getSitePath(docMap));
// facet
final Aggregations aggregations = searchResponse.getAggregations();
if (aggregations != null) {
facetResponse = new FacetResponse(aggregations);
}
parent.add(docMap);
}
});
// facet
final Aggregations aggregations = searchResponse.getAggregations();
if (aggregations != null) {
facetResponse = new FacetResponse(aggregations);
}
}
calculatePageInfo(start, pageSize, numFound);
calculatePageInfo(start, pageSize);
}
protected void calculatePageInfo(final long start, final int size, final long numFound) {
protected void calculatePageInfo(final int start, final int size) {
pageSize = size;
allRecordCount = numFound;
allPageCount = (int) ((allRecordCount - 1) / pageSize) + 1;
existPrevPage = start > 0;
existNextPage = start < (long) (allPageCount - 1) * (long) pageSize;
currentPageNumber = (int) (start / pageSize) + 1;
currentStartRecordNumber = numFound != 0 ? (currentPageNumber - 1) * pageSize + 1 : 0;
currentStartRecordNumber = allRecordCount != 0 ? (currentPageNumber - 1) * pageSize + 1 : 0;
currentEndRecordNumber = currentPageNumber * pageSize;
currentEndRecordNumber = allRecordCount < currentEndRecordNumber ? allRecordCount : currentEndRecordNumber;
@ -364,10 +368,6 @@ public class QueryResponseList implements List<Map<String, Object>> {
return facetResponse;
}
public MoreLikeThisResponse getMoreLikeThisResponse() {
return moreLikeThisResponse;
}
public boolean isPartialResults() {
return partialResults;
}

View file

@ -27,21 +27,21 @@
<property name="minCount">1</property>
<property name="field">["label"]</property>
<property name="query">[
"lastModified:[NOW/DAY-1DAY TO NOW]",
"lastModified:[NOW/DAY-7DAYS TO NOW]",
"lastModified:[NOW/DAY-1MONTH TO NOW]",
"lastModified:[NOW/DAY-1YEAR TO NOW]",
"contentLength:[0 TO 9999]",
"contentLength:[10000 TO 99999]",
"contentLength:[100000 TO 499999]",
"contentLength:[500000 TO 999999]",
"contentLength:[1000000 TO *]",
"filetype_s:html",
"filetype_s:word",
"filetype_s:excel",
"filetype_s:powerpoint",
"filetype_s:pdf",
"filetype_s:others"
"last_modified:[now/d-1d TO now]",
"last_modified:[now/d-7d TO now]",
"last_modified:[now/d-1M TO now]",
"last_modified:[now/d-1y TO now]",
"content_length:[0 TO 9999]",
"content_length:[10000 TO 99999]",
"content_length:[100000 TO 499999]",
"content_length:[500000 TO 999999]",
"content_length:[1000000 TO *]",
"filetype:html",
"filetype:word",
"filetype:excel",
"filetype:powerpoint",
"filetype:pdf",
"filetype:others"
]</property>
</component>
</property>
@ -123,19 +123,19 @@
<property name="title">"label.facet_lastModified_title"</property>
<postConstruct name="addQuery">
<arg>"label.facet_lastModified_1day"</arg>
<arg>"lastModified:[NOW/DAY-1DAY TO NOW]"</arg>
<arg>"last_modified:[now/d-1d TO now]"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_lastModified_1week"</arg>
<arg>"lastModified:[NOW/DAY-7DAYS TO NOW]"</arg>
<arg>"last_modified:[now/d-7d TO now]"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_lastModified_1month"</arg>
<arg>"lastModified:[NOW/DAY-1MONTH TO NOW]"</arg>
<arg>"last_modified:[now/d-1M TO now]"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_lastModified_1year"</arg>
<arg>"lastModified:[NOW/DAY-1YEAR TO NOW]"</arg>
<arg>"last_modified:[now/d-1y TO now]"</arg>
</postConstruct>
</component>
</arg>
@ -146,23 +146,23 @@
<property name="title">"label.facet_contentLength_title"</property>
<postConstruct name="addQuery">
<arg>"label.facet_contentLength_10k"</arg>
<arg>"contentLength:[0 TO 9999]"</arg>
<arg>"content_length:[0 TO 9999]"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_contentLength_10kto100k"</arg>
<arg>"contentLength:[10000 TO 99999]"</arg>
<arg>"content_length:[10000 TO 99999]"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_contentLength_100kto500k"</arg>
<arg>"contentLength:[100000 TO 499999]"</arg>
<arg>"content_length:[100000 TO 499999]"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_contentLength_500kto1m"</arg>
<arg>"contentLength:[500000 TO 999999]"</arg>
<arg>"content_length:[500000 TO 999999]"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_contentLength_1m"</arg>
<arg>"contentLength:[1000000 TO *]"</arg>
<arg>"content_length:[1000000 TO *]"</arg>
</postConstruct>
</component>
</arg>
@ -173,27 +173,27 @@
<property name="title">"label.facet_filetype_title"</property>
<postConstruct name="addQuery">
<arg>"label.facet_filetype_html"</arg>
<arg>"filetype_s:html"</arg>
<arg>"filetype:html"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_filetype_word"</arg>
<arg>"filetype_s:word"</arg>
<arg>"filetype:word"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_filetype_excel"</arg>
<arg>"filetype_s:excel"</arg>
<arg>"filetype:excel"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_filetype_powerpoint"</arg>
<arg>"filetype_s:powerpoint"</arg>
<arg>"filetype:powerpoint"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_filetype_pdf"</arg>
<arg>"filetype_s:pdf"</arg>
<arg>"filetype:pdf"</arg>
</postConstruct>
<postConstruct name="addQuery">
<arg>"label.facet_filetype_others"</arg>
<arg>"filetype_s:others"</arg>
<arg>"filetype:others"</arg>
</postConstruct>
</component>
</arg>