<commit_before>package org.intermine.web.struts;

/*
 * Copyright (C) 2002-2013 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.actions.TilesAction;
import org.intermine.api.InterMineAPI;
import org.intermine.api.profile.InterMineBag;
import org.intermine.api.profile.Profile;
import org.intermine.model.InterMineObject;
import org.intermine.web.logic.config.WebConfig;
import org.intermine.web.logic.session.SessionMethods;
import org.intermine.web.search.KeywordSearch;
import org.intermine.web.search.KeywordSearchFacetData;
import org.intermine.web.search.KeywordSearchHit;
import org.intermine.web.search.KeywordSearchResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.browseengine.bobo.api.BrowseHit;
import com.browseengine.bobo.api.BrowseResult;

/**
 * Controller for keyword search.
 *
 * @author nils
 */
public class KeywordSearchResultsController extends TilesAction
{
    private static final Logger LOG = Logger.getLogger(KeywordSearchResultsController.class);
    private static Logger searchLog = null;

    /**
     * {@inheritDoc}
     */
    @Override
    public ActionForward execute(ComponentContext context,
            @SuppressWarnings("unused") ActionMapping mapping,
            @SuppressWarnings("unused") ActionForm form,
            HttpServletRequest request,
            @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
        long time = System.currentTimeMillis();
        final InterMineAPI im = SessionMethods.getInterMineAPI(request.getSession());
        javax.servlet.ServletContext servletContext = request.getSession().getServletContext();
        String contextPath = servletContext.getRealPath("/");
        synchronized (this) {
            // if this decreases performance too much we might have to change it
            intialiseLogging(SessionMethods.getWebProperties(servletContext).getProperty(
                    "project.title", "unknown").toLowerCase());
        }
        KeywordSearch.initKeywordSearch(im, contextPath);
        Vector<KeywordSearchFacetData> facets = KeywordSearch.getFacets();
        int totalHits = 0;

        // term
        String searchTerm = request.getParameter("searchTerm");
        LOG.debug("SEARCH TERM: '" + searchTerm + "'");

        //track the keyword search
        Profile profile = SessionMethods.getProfile(request.getSession());
        im.getTrackerDelegate().trackKeywordSearch(searchTerm, profile,
                request.getSession().getId());

        // search in bag (list)
        String searchBag = request.getParameter("searchBag");
        if (searchBag == null) {
            searchBag = "";
        }
        List<Integer> ids = getBagIds(im, request, searchBag);
        int offset = getOffset(request);
        Map<String, String> facetValues = getFacetValues(request, facets);
        LOG.debug("Initializing took " + (System.currentTimeMillis() - time) + " ms");

        // show overview by default
        if (StringUtils.isBlank(searchTerm)) {
            searchTerm = "*:*";
        }

        //TODO remove - just  for performance testing
        // KeywordSearch.runLuceneSearch(searchTerm);

        long searchTime = System.currentTimeMillis();
        BrowseResult result = KeywordSearch.runBrowseSearch(searchTerm, offset, facetValues,
                ids);
        searchTime = System.currentTimeMillis() - searchTime;
        Vector<KeywordSearchResult> searchResultsParsed = new Vector<KeywordSearchResult>();
        Vector<KeywordSearchFacet> searchResultsFacets = new Vector<KeywordSearchFacet>();
        Set<Integer> objectIds = new HashSet<Integer>();

        if (result != null) {
            totalHits = result.getNumHits();
            LOG.debug("Browse found " + result.getNumHits() + " hits");
            BrowseHit[] browseHits = result.getHits();            
            objectIds = KeywordSearch.getObjectIds(browseHits);
            Map<Integer, InterMineObject> objMap = KeywordSearch.getObjects(im, objectIds);<commit_msg>- overriding searchTerm so that it never shows the lucene index<commit_after>package org.intermine.web.struts;

/*
 * Copyright (C) 2002-2013 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.actions.TilesAction;
import org.intermine.api.InterMineAPI;
import org.intermine.api.profile.InterMineBag;
import org.intermine.api.profile.Profile;
import org.intermine.model.InterMineObject;
import org.intermine.web.logic.config.WebConfig;
import org.intermine.web.logic.session.SessionMethods;
import org.intermine.web.search.KeywordSearch;
import org.intermine.web.search.KeywordSearchFacetData;
import org.intermine.web.search.KeywordSearchHit;
import org.intermine.web.search.KeywordSearchResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.browseengine.bobo.api.BrowseHit;
import com.browseengine.bobo.api.BrowseResult;

/**
 * Controller for keyword search.
 *
 * @author nils
 */
public class KeywordSearchResultsController extends TilesAction
{
    private static final Logger LOG = Logger.getLogger(KeywordSearchResultsController.class);
    private static Logger searchLog = null;

    /**
     * {@inheritDoc}
     */
    @Override
    public ActionForward execute(ComponentContext context,
            @SuppressWarnings("unused") ActionMapping mapping,
            @SuppressWarnings("unused") ActionForm form,
            HttpServletRequest request,
            @SuppressWarnings("unused") HttpServletResponse response) throws Exception {
        long time = System.currentTimeMillis();
        final InterMineAPI im = SessionMethods.getInterMineAPI(request.getSession());
        javax.servlet.ServletContext servletContext = request.getSession().getServletContext();
        String contextPath = servletContext.getRealPath("/");
        synchronized (this) {
            // if this decreases performance too much we might have to change it
            intialiseLogging(SessionMethods.getWebProperties(servletContext).getProperty(
                    "project.title", "unknown").toLowerCase());
        }
        KeywordSearch.initKeywordSearch(im, contextPath);
        Vector<KeywordSearchFacetData> facets = KeywordSearch.getFacets();
        int totalHits = 0;

        // term
        String searchTerm = request.getParameter("searchTerm");
        LOG.debug("SEARCH TERM: '" + searchTerm + "'");

        //track the keyword search
        Profile profile = SessionMethods.getProfile(request.getSession());
        im.getTrackerDelegate().trackKeywordSearch(searchTerm, profile,
                request.getSession().getId());

        // search in bag (list)
        String searchBag = request.getParameter("searchBag");
        if (searchBag == null) {
            searchBag = "";
        }
        List<Integer> ids = getBagIds(im, request, searchBag);
        int offset = getOffset(request);
        Map<String, String> facetValues = getFacetValues(request, facets);
        LOG.debug("Initializing took " + (System.currentTimeMillis() - time) + " ms");

        // show overview by default
        if (StringUtils.isBlank(searchTerm) || "*:*".equals(searchTerm)) {
            searchTerm = "*";
        }

        //TODO remove - just  for performance testing
        // KeywordSearch.runLuceneSearch(searchTerm);

        long searchTime = System.currentTimeMillis();
        BrowseResult result = KeywordSearch.runBrowseSearch(searchTerm, offset, facetValues,
                ids);
        searchTime = System.currentTimeMillis() - searchTime;
        Vector<KeywordSearchResult> searchResultsParsed = new Vector<KeywordSearchResult>();
        Vector<KeywordSearchFacet> searchResultsFacets = new Vector<KeywordSearchFacet>();
        Set<Integer> objectIds = new HashSet<Integer>();

        if (result != null) {
            totalHits = result.getNumHits();
            LOG.debug("Browse found " + result.getNumHits() + " hits");
            BrowseHit[] browseHits = result.getHits();            
            objectIds = KeywordSearch.getObjectIds(browseHits);
            Map<Integer, InterMineObject> objMap = KeywordSearch.getObjects(im, objectIds);
{'fraction_non_alphanumeric': 0.10391772771792361, 'fraction_numerical': 0.0025465230166503428, 'mean_word_length': 3.4923009238891334, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1894648', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2827, 'n_words': 606}