Search in sources :

Example 91 with FessConfig

use of org.codelibs.fess.mylasta.direction.FessConfig in project fess by codelibs.

the class IndexUpdater method addFavoriteCountField.

protected void addFavoriteCountField(final Map<String, Object> map) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final String url = (String) map.get(fessConfig.getIndexFieldUrl());
    if (StringUtil.isNotBlank(url)) {
        final SearchLogHelper searchLogHelper = ComponentUtil.getSearchLogHelper();
        final long count = searchLogHelper.getFavoriteCount(url);
        map.put(fessConfig.getIndexFieldFavoriteCount(), count);
        if (logger.isDebugEnabled()) {
            logger.debug("Favorite Count: {}, url: {}", count, url);
        }
    }
}
Also used : SearchLogHelper(org.codelibs.fess.helper.SearchLogHelper) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Example 92 with FessConfig

use of org.codelibs.fess.mylasta.direction.FessConfig in project fess by codelibs.

the class GsaApiManager method processSearchRequest.

protected void processSearchRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) {
    final SearchService searchService = ComponentUtil.getComponent(SearchService.class);
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    int status = 0;
    String errMsg = StringUtil.EMPTY;
    String query = null;
    // TODO replace response stream
    final StringBuilder buf = new StringBuilder(1000);
    request.setAttribute(Constants.SEARCH_LOG_ACCESS_TYPE, Constants.SEARCH_LOG_ACCESS_TYPE_XML);
    boolean xmlDtd = false;
    try {
        final SearchRenderData data = new SearchRenderData();
        final GsaRequestParams params = new GsaRequestParams(request, fessConfig);
        query = params.getQuery();
        request.setAttribute(Constants.REQUEST_QUERIES, query);
        searchService.search(params, data, OptionalThing.empty());
        final String execTime = data.getExecTime();
        final long allRecordCount = data.getAllRecordCount();
        final List<Map<String, Object>> documentItems = data.getDocumentItems();
        final List<String> getFields = new ArrayList<>();
        // meta tags should be returned
        final String getFieldsParam = request.getParameter("getfields");
        if (StringUtil.isNotBlank(getFieldsParam)) {
            getFields.addAll(Arrays.asList(getFieldsParam.split("\\.")));
        }
        // DTD
        if ("xml".equals(request.getParameter("output"))) {
            xmlDtd = true;
        }
        final StringBuilder requestUri = new StringBuilder(request.getRequestURI());
        if (request.getQueryString() != null) {
            requestUri.append("?").append(request.getQueryString());
        }
        final String uriQueryString = requestUri.toString();
        // Input/Output encoding
        final String ie = request.getCharacterEncoding();
        final String oe = "UTF-8";
        // IP address
        final String ip = ComponentUtil.getViewHelper().getClientIp(request);
        final String start = request.getParameter("start");
        long startNumber = 1;
        if (StringUtil.isNotBlank(start)) {
            startNumber = Long.parseLong(start) + 1;
        }
        long endNumber = startNumber + data.getPageSize() - 1;
        if (endNumber > allRecordCount) {
            endNumber = allRecordCount;
        }
        buf.append("<Q>");
        buf.append(escapeXml(query));
        buf.append("</Q>");
        buf.append("<TM>");
        buf.append(execTime);
        buf.append("</TM>");
        for (final Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
            final String[] values = entry.getValue();
            if (values == null) {
                continue;
            }
            final String key = entry.getKey();
            for (final String value : values) {
                buf.append("<PARAM name=\"");
                buf.append(key);
                buf.append("\" value=\"");
                buf.append(value);
                buf.append("\" original_value=\"");
                buf.append(URLEncoder.encode(value, Constants.UTF_8));
                buf.append("\"/>");
            }
        }
        buf.append("<PARAM name=\"ie\" value=\"");
        buf.append(ie);
        buf.append("\" original_value=\"");
        buf.append(URLEncoder.encode(ie, Constants.UTF_8));
        buf.append("\"/>");
        buf.append("<PARAM name=\"oe\" value=\"");
        buf.append(oe);
        buf.append("\" original_value=\"");
        buf.append(URLEncoder.encode(ie, Constants.UTF_8));
        buf.append("\"/>");
        buf.append("<PARAM name=\"ip\" value=\"");
        buf.append(ip);
        buf.append("\" original_value=\"");
        buf.append(URLEncoder.encode(ie, Constants.UTF_8));
        buf.append("\"/>");
        if (!documentItems.isEmpty()) {
            buf.append("<RES SN=\"");
            buf.append(startNumber);
            buf.append("\" EN=\"");
            buf.append(endNumber);
            buf.append("\">");
            buf.append("<M>");
            buf.append(allRecordCount);
            buf.append("</M>");
            if (endNumber < allRecordCount) {
                buf.append("<NB>");
                buf.append("<NU>");
                buf.append(escapeXml(uriQueryString.replaceFirst("start=([^&]+)", "start=" + endNumber)));
                buf.append("</NU>");
                buf.append("</NB>");
            }
            long recordNumber = startNumber;
            for (final Map<String, Object> document : documentItems) {
                buf.append("<R N=\"");
                buf.append(recordNumber);
                buf.append("\">");
                final String url = (String) document.remove("url");
                document.put("UE", url);
                document.put("U", URLDecoder.decode(url, Constants.UTF_8));
                document.put("T", document.remove("title"));
                final float score = Float.parseFloat((String) document.remove("boost"));
                document.put("RK", (int) (score * 10));
                document.put("S", ((String) document.remove("content_description")).replaceAll("<(/*)em>", "<$1b>"));
                document.put("LANG", document.remove("lang"));
                for (final Map.Entry<String, Object> entry : document.entrySet()) {
                    final String name = entry.getKey();
                    if (StringUtil.isNotBlank(name) && entry.getValue() != null && ComponentUtil.getQueryHelper().isApiResponseField(name)) {
                        if (name.startsWith(gsaMetaPrefix)) {
                            final String tagName = name.replaceAll("^" + gsaMetaPrefix, "").replaceAll(GSA_META_SUFFIX + "\\z", "");
                            if (getFields != null && getFields.contains(tagName)) {
                                buf.append("<MT N=\"");
                                buf.append(tagName);
                                buf.append("\" V=\"");
                                buf.append(escapeXml(entry.getValue().toString()));
                                buf.append("\"/>");
                            }
                        } else {
                            final String tagName = name;
                            buf.append('<');
                            buf.append(tagName);
                            buf.append('>');
                            buf.append(escapeXml(entry.getValue()));
                            buf.append("</");
                            buf.append(tagName);
                            buf.append('>');
                        }
                    }
                }
                buf.append("<HAS>");
                buf.append("<L/>");
                buf.append("<C SZ=\"");
                buf.append(Long.parseLong((String) document.remove("content_length")) / 1000);
                buf.append("k\" CID=\"");
                buf.append(document.remove("doc_id"));
                buf.append("\" ENC=\"");
                String charset = (String) document.remove("charset_s");
                if (StringUtil.isNotBlank(charset)) {
                    buf.append(charset);
                } else {
                    charset = (String) document.remove("contentType_s");
                    if (StringUtil.isNotBlank(charset)) {
                        final Matcher m = Pattern.compile(".*;\\s*charset=(.+)").matcher(charset);
                        if (m.matches()) {
                            charset = m.group(1);
                            buf.append(charset);
                        }
                    }
                }
                buf.append("\"/>");
                buf.append("</HAS>");
                buf.append("</R>");
                recordNumber++;
            }
            buf.append("</RES>");
        }
    } catch (final Exception e) {
        status = 1;
        errMsg = e.getMessage();
        if (errMsg == null) {
            errMsg = e.getClass().getName();
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to process a search request.", e);
        }
        if (e instanceof InvalidAccessTokenException) {
            final InvalidAccessTokenException iate = (InvalidAccessTokenException) e;
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.setHeader("WWW-Authenticate", "Bearer error=\"" + iate.getType() + "\"");
        }
    }
    writeXmlResponse(status, xmlDtd, buf.toString(), errMsg);
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) ServletException(javax.servlet.ServletException) InvalidAccessTokenException(org.codelibs.fess.exception.InvalidAccessTokenException) IOException(java.io.IOException) InvalidAccessTokenException(org.codelibs.fess.exception.InvalidAccessTokenException) SearchService(org.codelibs.fess.app.service.SearchService) SearchRenderData(org.codelibs.fess.entity.SearchRenderData) HashMap(java.util.HashMap) Map(java.util.Map)

Example 93 with FessConfig

use of org.codelibs.fess.mylasta.direction.FessConfig in project fess by codelibs.

the class KeyMatchHelper method reload.

protected void reload(final long interval) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final KeyMatchService keyMatchService = ComponentUtil.getComponent(KeyMatchService.class);
    final Map<String, Pair<QueryBuilder, ScoreFunctionBuilder>> keyMatchQueryMap = new HashMap<>();
    keyMatchService.getAvailableKeyMatchList().stream().forEach(keyMatch -> {
        final BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        getDocumentList(keyMatch).stream().map(doc -> {
            return DocumentUtil.getValue(doc, fessConfig.getIndexFieldDocId(), String.class);
        }).forEach(docId -> {
            boolQuery.should(QueryBuilders.termQuery(fessConfig.getIndexFieldDocId(), docId));
        });
        if (boolQuery.hasClauses()) {
            keyMatchQueryMap.put(toLowerCase(keyMatch.getTerm()), new Pair<>(boolQuery, ScoreFunctionBuilders.weightFactorFunction(keyMatch.getBoost())));
        }
        if (reloadInterval > 0) {
            try {
                Thread.sleep(reloadInterval);
            } catch (final InterruptedException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Interrupted.", e);
                }
            }
        }
    });
    this.keyMatchQueryMap = keyMatchQueryMap;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Constants(org.codelibs.fess.Constants) DocumentUtil(org.codelibs.fess.util.DocumentUtil) Logger(org.slf4j.Logger) FessEsClient(org.codelibs.fess.es.client.FessEsClient) LoggerFactory(org.slf4j.LoggerFactory) Pair(org.codelibs.core.misc.Pair) HashMap(java.util.HashMap) SearchConditionBuilder(org.codelibs.fess.es.client.FessEsClient.SearchConditionBuilder) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ScoreFunctionBuilders(org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders) KeyMatchService(org.codelibs.fess.app.service.KeyMatchService) List(java.util.List) SearchRequestType(org.codelibs.fess.entity.SearchRequestParams.SearchRequestType) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) ComponentUtil(org.codelibs.fess.util.ComponentUtil) Locale(java.util.Locale) Map(java.util.Map) KeyMatch(org.codelibs.fess.es.config.exentity.KeyMatch) PostConstruct(javax.annotation.PostConstruct) ScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Collections(java.util.Collections) HashMap(java.util.HashMap) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) KeyMatchService(org.codelibs.fess.app.service.KeyMatchService) Pair(org.codelibs.core.misc.Pair)

Example 94 with FessConfig

use of org.codelibs.fess.mylasta.direction.FessConfig in project fess by codelibs.

the class IndexingHelper method deleteOldDocuments.

private void deleteOldDocuments(final FessEsClient fessEsClient, final DocList docList) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final List<String> docIdList = new ArrayList<>();
    for (final Map<String, Object> inputDoc : docList) {
        final Object idValue = inputDoc.get(fessConfig.getIndexFieldId());
        if (idValue == null) {
            continue;
        }
        final Object configIdValue = inputDoc.get(fessConfig.getIndexFieldConfigId());
        if (configIdValue == null) {
            continue;
        }
        final QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery(fessConfig.getIndexFieldUrl(), inputDoc.get(fessConfig.getIndexFieldUrl()))).filter(QueryBuilders.termQuery(fessConfig.getIndexFieldConfigId(), configIdValue));
        final List<Map<String, Object>> docs = getDocumentListByQuery(fessEsClient, queryBuilder, new String[] { fessConfig.getIndexFieldId(), fessConfig.getIndexFieldDocId() });
        for (final Map<String, Object> doc : docs) {
            final Object oldIdValue = doc.get(fessConfig.getIndexFieldId());
            if (!idValue.equals(oldIdValue) && oldIdValue != null) {
                final Object oldDocIdValue = doc.get(fessConfig.getIndexFieldDocId());
                if (oldDocIdValue != null) {
                    docIdList.add(oldDocIdValue.toString());
                }
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug(queryBuilder.toString() + " => " + docs);
        }
    }
    if (!docIdList.isEmpty()) {
        fessEsClient.deleteByQuery(fessConfig.getIndexDocumentUpdateIndex(), fessConfig.getIndexDocumentType(), QueryBuilders.idsQuery(fessConfig.getIndexDocumentType()).addIds(docIdList.stream().toArray(n -> new String[n])));
    }
}
Also used : ArrayList(java.util.ArrayList) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) Map(java.util.Map)

Example 95 with FessConfig

use of org.codelibs.fess.mylasta.direction.FessConfig in project fess by codelibs.

the class IndexingHelper method getDocumentListByPrefixId.

public List<Map<String, Object>> getDocumentListByPrefixId(final FessEsClient fessEsClient, final String id, final String[] fields) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final QueryBuilder queryBuilder = QueryBuilders.prefixQuery(fessConfig.getIndexFieldId(), id);
    return getDocumentListByQuery(fessEsClient, queryBuilder, fields);
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig)

Aggregations

FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)176 ArrayList (java.util.ArrayList)60 Map (java.util.Map)54 HashMap (java.util.HashMap)48 StringUtil (org.codelibs.core.lang.StringUtil)42 ComponentUtil (org.codelibs.fess.util.ComponentUtil)42 List (java.util.List)37 Constants (org.codelibs.fess.Constants)36 LogManager (org.apache.logging.log4j.LogManager)30 Logger (org.apache.logging.log4j.Logger)30 StreamUtil.stream (org.codelibs.core.stream.StreamUtil.stream)28 PostConstruct (javax.annotation.PostConstruct)27 IOException (java.io.IOException)24 SystemHelper (org.codelibs.fess.helper.SystemHelper)19 File (java.io.File)18 Collectors (java.util.stream.Collectors)18 SearchEngineClient (org.codelibs.fess.es.client.SearchEngineClient)18 FessSystemException (org.codelibs.fess.exception.FessSystemException)17 Collections (java.util.Collections)15 DocumentUtil (org.codelibs.fess.util.DocumentUtil)15