Example usage for org.apache.lucene.search BooleanQuery getMaxClauseCount

List of usage examples for org.apache.lucene.search BooleanQuery getMaxClauseCount

Introduction

In this page you can find the example usage for org.apache.lucene.search BooleanQuery getMaxClauseCount.

Prototype

public static int getMaxClauseCount() 

Source Link

Document

Return the maximum number of clauses permitted, 1024 by default.

Usage

From source file:com.esri.gpt.catalog.lucene.LuceneConfig.java

License:Apache License

/**
 * Gets maximum number of clauses within boolean query.
 * @return maximum number of clauses within boolean query
 *///from ww w  .  j a v a2s  .  co  m
public int getMaxClauseCount() {
    return BooleanQuery.getMaxClauseCount();
}

From source file:com.esri.gpt.framework.context.ApplicationConfigurationLoader.java

License:Apache License

/**
 * Loads the catalog configuration./*from  w  w w  .j av  a  2s. c  o m*/
 * 
 * @param appConfig
 *          the primary application configuration
 * @param dom
 *          the configuration document
 * @param root
 *          the root node for the document
 * @throws Exception 
 */
private void loadCatalog(ApplicationConfiguration appConfig, Document dom, Node root) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();

    // catalog configuration
    Node ndCat = (Node) xpath.evaluate("catalog", root, XPathConstants.NODE);
    if (ndCat != null) {
        CatalogConfiguration cfg = appConfig.getCatalogConfiguration();
        cfg.getParameters().clear();
        ImsService publish = cfg.getArcImsCatalog().getPublishService();
        ImsService browse = cfg.getArcImsCatalog().getBrowseService();
        cfg.setTablePrefix(Val.chkStr(xpath.evaluate("@gptTablePrefix", ndCat), "GPT_"));
        cfg.setMvsTablePrefix(Val.chkStr(xpath.evaluate("@mvsTablePrefix", ndCat), "MVS_"));

        publish.setServerUrl(xpath.evaluate("@metadataServerUrl", ndCat));
        publish.setServiceName(
                Val.chkStr(xpath.evaluate("@metadataServerPublishService", ndCat), "GPT_Publish_Metadata"));
        publish.setTimeoutMillisecs(Val.chkInt(xpath.evaluate("@metadataServerTimeoutMillisecs", ndCat), 0));

        browse.setServerUrl(publish.getServerUrl());
        browse.setServiceName(
                Val.chkStr(xpath.evaluate("@metadataServerBrowseService", ndCat), "GPT_Browse_Metadata"));
        browse.setTimeoutMillisecs(publish.getTimeoutMillisecs());

        // additional parameters
        populateParameters(cfg.getParameters(), ndCat);

        // parse http timeouts
        String connectionTimeout = cfg.getParameters().getValue("httpClientRequest.connectionTimeout");
        String responseTimeout = cfg.getParameters().getValue("httpClientRequest.responseTimeout");

        // set http timeouts
        cfg.setConnectionTimeMs(
                (int) parsePeriod(connectionTimeout, HttpClientRequest.DEFAULT_CONNECTION_TIMEOUT).getValue());
        cfg.setResponseTimeOutMs(
                (int) parsePeriod(responseTimeout, HttpClientRequest.DEFAULT_RESPONSE_TIMEOUT).getValue());
    }

    // search configuration
    Node ndSearch = (Node) xpath.evaluate("catalog/search", root, XPathConstants.NODE);
    SearchConfig sCfg = appConfig.getCatalogConfiguration().getSearchConfig();
    sCfg.setSearchConfigNode(ndSearch);
    if (ndSearch != null) {
        sCfg.setResultsReviewsShown(Val.chkStr(xpath.evaluate("@searchResultsReviewsShown", ndSearch)));
        sCfg.setResultsPerPage(xpath.evaluate("@searchResultsPerPage", ndSearch));
        sCfg.setMaxSavedSearches(xpath.evaluate("@maxSavedSearches", ndSearch));
        sCfg.setCswProfile(Val.chkStr(xpath.evaluate("@cswServletUrlProfile", ndSearch),
                "urn:ogc:CSW:2.0.2:HTTP:OGCCORE:ESRI:GPT"));
        sCfg.setSearchUri(xpath.evaluate("@cswServletUrl", ndSearch));
        sCfg.setTimeOut(xpath.evaluate("@searchTimeoutMillisecs", ndSearch));
        sCfg.setDistributedSearchMaxSelectedSites(
                Val.chkStr(xpath.evaluate("@distributedSearchMaxSelectedSites", ndSearch)));
        sCfg.setDistributedSearchTimeoutMillisecs(
                Val.chkStr(xpath.evaluate("@distributedSearchTimeoutMillisecs", ndSearch)));

        sCfg.setAllowExternalSearch(Val.chkBool(xpath.evaluate("@allowExternalSiteSearch", ndSearch), false));
        sCfg.setJsfSuffix(Val.chkStr(xpath.evaluate("@jsfSuffix", ndSearch)));
        sCfg.setGptToCswXsltPath(xpath.evaluate("@gpt2cswXslt", ndSearch));
        sCfg.setMapViewerUrl(Val.chkStr(xpath.evaluate("@mapViewerUrl", ndSearch), ""));
        sCfg.validate();
    }

    NodeList nodes = (NodeList) xpath.evaluate("catalog/search/repositories/repository", root,
            XPathConstants.NODESET);
    NodeList nodeList = nodes;
    LinkedHashMap<String, Map<String, String>> sFactory = new LinkedHashMap<String, Map<String, String>>();
    Map<String, String> attributes = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    /*attributes.put("key", "local");
    attributes.put("class", "com.esri.gpt.catalog.search.SearchEngineLocal");
    attributes.put("resourceKey", "catalog.search.searchSite.defaultsite");
    attributes.put("labelResourceKey", "catalog.search.searchSite.defaultsite");
    attributes.put("abstractResourceKey", "catalog.search.searchSite.defaultsite.abstract");
    sFactory.put("local", attributes);*/
    for (int i = 0; nodeList != null && i < nodeList.getLength(); i++) {
        ndSearch = nodeList.item(i);
        attributes = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
        NamedNodeMap nnm = ndSearch.getAttributes();
        for (int j = 0; nnm != null && j < nnm.getLength(); j++) {
            Node nd = nnm.item(j);
            String key = Val.chkStr(nd.getNodeName());
            String value = Val.chkStr(nd.getNodeValue());
            attributes.put(key, value);
            if (key.equalsIgnoreCase("RESOURCEKEY")) {
                attributes.put("RESOURCEKEY", value);
            }
            if (key.equalsIgnoreCase("labelResourceKey")) {
                attributes.put("labelResourceKey", value);
            }
            if (key.equalsIgnoreCase("abstractResourceKey")) {
                attributes.put("abstractResourceKey", value);
            }
        }
        NodeList params = (NodeList) xpath.evaluate("parameter", ndSearch, XPathConstants.NODESET);
        for (int k = 0; params != null && k < params.getLength(); k++) {
            String key = xpath.evaluate("@key", params.item(k));
            String value = xpath.evaluate("@value", params.item(k));
            attributes.put(Val.chkStr(key), Val.chkStr(value));
        }

        String key = Val.chkStr(xpath.evaluate("@key", ndSearch));
        sFactory.put(key, attributes);

    }
    sCfg.setSearchFactoryRepos(sFactory);

    // Mapviewer
    ArrayList<MapViewerConfigs> mapViewerConfigs = new ArrayList<MapViewerConfigs>();
    nodes = (NodeList) xpath.evaluate("catalog/mapViewer/instance", root, XPathConstants.NODESET);
    for (int j = 0; nodes != null && j < nodes.getLength(); j++) {
        MapViewerConfigs mvConfigs = new MapViewerConfigs();
        Node nd = nodes.item(j);
        mvConfigs.setClassName(
                Val.chkStr(xpath.evaluate("@className", nd), "com.esri.gpt.catalog.search.MapViewerFlex"));
        mvConfigs.setUrl(xpath.evaluate("@url", nd));
        NodeList pNodeList = (NodeList) xpath.evaluate("parameter", nd, XPathConstants.NODESET);
        for (int k = 0; pNodeList != null && k < pNodeList.getLength(); k++) {
            String key = xpath.evaluate("@key", pNodeList.item(k));
            String value = xpath.evaluate("@value", pNodeList.item(k));
            if (key != null || value != null) {
                mvConfigs.addParameter(key, value);
            }
        }
        mapViewerConfigs.add(mvConfigs);

    }
    sCfg.setMapViewerInstances(mapViewerConfigs);

    // Lucene configuration
    Node ndLucene = (Node) xpath.evaluate("catalog/lucene", root, XPathConstants.NODE);
    if (ndLucene != null) {
        CatalogConfiguration cfg = appConfig.getCatalogConfiguration();
        cfg.getLuceneConfig().setIndexLocation(xpath.evaluate("@indexLocation", ndLucene));
        cfg.getLuceneConfig()
                .setWriteLockTimeout(Val.chkInt(xpath.evaluate("@writeLockTimeout", ndLucene), -1));
        cfg.getLuceneConfig().setUseNativeFSLockFactory(
                Val.chkStr(xpath.evaluate("@useNativeFSLockFactory", ndLucene)).equalsIgnoreCase("true"));
        cfg.getLuceneConfig().setAnalyzerClassName(xpath.evaluate("@analyzerClassName", ndLucene));
        cfg.getLuceneConfig().setUseConstantScoreQuery(
                Val.chkBool(xpath.evaluate("@useConstantScoreQuery", ndLucene), false));
        cfg.getLuceneConfig().setMaxClauseCount(
                Val.chkInt(xpath.evaluate("@maxClauseCount", ndLucene), BooleanQuery.getMaxClauseCount()));

        ParserAdaptorInfos infos = new ParserAdaptorInfos();
        NodeList ndLstProxies = (NodeList) xpath.evaluate("adaptor", ndLucene, XPathConstants.NODESET);
        for (int i = 0; i < ndLstProxies.getLength(); i++) {
            Node ndProxy = ndLstProxies.item(i);
            String proxyName = xpath.evaluate("@name", ndProxy);
            String proxyClassName = xpath.evaluate("@className", ndProxy);

            ParserAdaptorInfo info = new ParserAdaptorInfo();
            info.setName(proxyName);
            info.setClassName(proxyClassName);

            NodeList ndListProps = (NodeList) xpath.evaluate("attribute", ndProxy, XPathConstants.NODESET);
            for (int p = 0; p < ndListProps.getLength(); p++) {
                Node ndProp = ndListProps.item(p);
                String key = xpath.evaluate("@key", ndProp);
                String value = xpath.evaluate("@value", ndProp);
                info.getAttributes().set(key, value);
            }

            infos.add(info);
        }

        cfg.getLuceneConfig().setParserProxies(infos.createParserProxies());

        NodeList ndObservers = (NodeList) xpath.evaluate("observer", ndLucene, XPathConstants.NODESET);
        for (Node ndObserver : new NodeListAdapter(ndObservers)) {
            LuceneIndexObserverInfo info = new LuceneIndexObserverInfo();
            info.setClassName(Val.chkStr(xpath.evaluate("@className", ndObserver)));
            NodeList ndListProps = (NodeList) xpath.evaluate("attribute", ndObserver, XPathConstants.NODESET);
            for (Node ndAttribute : new NodeListAdapter(ndListProps)) {
                String key = xpath.evaluate("@key", ndAttribute);
                String value = xpath.evaluate("@value", ndAttribute);
                info.getAttributes().set(key, value);
            }
            LuceneIndexObserver observer = info.createObserver();
            if (observer != null) {
                cfg.getLuceneConfig().getObservers().add(observer);
            }
        }

    }

    loadMetadataAccessPolicyConfiguration(appConfig, root);

}

From source file:com.google.gerrit.lucene.LuceneChangeIndex.java

License:Apache License

@AssistedInject
LuceneChangeIndex(@GerritServerConfig Config cfg, SitePaths sitePaths,
        @IndexExecutor(INTERACTIVE) ListeningExecutorService executor, Provider<ReviewDb> db,
        ChangeData.Factory changeDataFactory, FillArgs fillArgs, @Assisted Schema<ChangeData> schema,
        @Assisted @Nullable String base) throws IOException {
    this.sitePaths = sitePaths;
    this.fillArgs = fillArgs;
    this.executor = executor;
    this.db = db;
    this.changeDataFactory = changeDataFactory;
    this.schema = schema;
    this.useDocValuesForSorting = schema.getVersion() >= 15;
    this.idSortField = sortFieldName(LegacyChangeIdPredicate.idField(schema));

    CustomMappingAnalyzer analyzer = new CustomMappingAnalyzer(new StandardAnalyzer(CharArraySet.EMPTY_SET),
            CUSTOM_CHAR_MAPPING);/*from   w w  w  .  j  a  v a  2 s. co  m*/
    queryBuilder = new QueryBuilder(analyzer);

    BooleanQuery
            .setMaxClauseCount(cfg.getInt("index", "defaultMaxClauseCount", BooleanQuery.getMaxClauseCount()));

    GerritIndexWriterConfig openConfig = new GerritIndexWriterConfig(cfg, "changes_open");
    GerritIndexWriterConfig closedConfig = new GerritIndexWriterConfig(cfg, "changes_closed");

    SearcherFactory searcherFactory = newSearcherFactory();
    if (cfg.getBoolean("index", "lucene", "testInmemory", false)) {
        openIndex = new SubIndex(new RAMDirectory(), "ramOpen", openConfig, searcherFactory);
        closedIndex = new SubIndex(new RAMDirectory(), "ramClosed", closedConfig, searcherFactory);
    } else {
        Path dir = base != null ? Paths.get(base) : LuceneVersionManager.getDir(sitePaths, schema);
        openIndex = new SubIndex(dir.resolve(CHANGES_OPEN), openConfig, searcherFactory);
        closedIndex = new SubIndex(dir.resolve(CHANGES_CLOSED), closedConfig, searcherFactory);
    }
}

From source file:com.google.gerrit.lucene.LuceneIndexModule.java

License:Apache License

@Provides
@Singleton/*from ww  w . j a  v  a2 s .  co m*/
IndexConfig getIndexConfig(@GerritServerConfig Config cfg) {
    BooleanQuery.setMaxClauseCount(cfg.getInt("index", "maxTerms", BooleanQuery.getMaxClauseCount()));
    return IndexConfig.fromConfig(cfg);
}

From source file:com.jaeksoft.searchlib.ClientCatalog.java

License:Open Source License

public static int getMaxClauseCount() {
    return BooleanQuery.getMaxClauseCount();
}

From source file:com.liferay.portal.search.lucene.LuceneIndexSearcherImpl.java

License:Open Source License

public Hits search(String searchEngineId, long companyId, Query query, Sort[] sorts, int start, int end)
        throws SearchException {

    if (_log.isDebugEnabled()) {
        _log.debug("Query " + query);
    }//  w w w  .ja  v a 2 s .c  o  m

    Hits hits = null;

    org.apache.lucene.search.IndexSearcher indexSearcher = null;
    org.apache.lucene.search.Sort luceneSort = null;

    try {
        indexSearcher = LuceneHelperUtil.getSearcher(companyId, true);

        if (sorts != null) {
            SortField[] sortFields = new SortField[sorts.length];

            for (int i = 0; i < sorts.length; i++) {
                Sort sort = sorts[i];

                sortFields[i] = new SortField(sort.getFieldName(), sort.getType(), sort.isReverse());
            }

            luceneSort = new org.apache.lucene.search.Sort(sortFields);
        } else {
            luceneSort = new org.apache.lucene.search.Sort();
        }

        long startTime = System.currentTimeMillis();

        TopFieldDocs topFieldDocs = indexSearcher.search(
                (org.apache.lucene.search.Query) QueryTranslatorUtil.translate(query), null,
                PropsValues.INDEX_SEARCH_LIMIT, luceneSort);

        long endTime = System.currentTimeMillis();

        float searchTime = (float) (endTime - startTime) / Time.SECOND;

        hits = toHits(indexSearcher, new HitDocs(topFieldDocs), query, startTime, searchTime, start, end);
    } catch (BooleanQuery.TooManyClauses tmc) {
        int maxClauseCount = BooleanQuery.getMaxClauseCount();

        BooleanQuery.setMaxClauseCount(Integer.MAX_VALUE);

        try {
            long startTime = System.currentTimeMillis();

            TopFieldDocs topFieldDocs = indexSearcher.search(
                    (org.apache.lucene.search.Query) QueryTranslatorUtil.translate(query), null,
                    PropsValues.INDEX_SEARCH_LIMIT, luceneSort);

            long endTime = System.currentTimeMillis();

            float searchTime = (float) (endTime - startTime) / Time.SECOND;

            hits = toHits(indexSearcher, new HitDocs(topFieldDocs), query, startTime, searchTime, start, end);
        } catch (Exception e) {
            throw new SearchException(e);
        } finally {
            BooleanQuery.setMaxClauseCount(maxClauseCount);
        }
    } catch (ParseException pe) {
        _log.error("Query " + query, pe);

        return new HitsImpl();
    } catch (Exception e) {
        throw new SearchException(e);
    } finally {
        if (indexSearcher != null) {
            try {
                indexSearcher.close();
            } catch (IOException ioe) {
                _log.error(ioe, ioe);
            }
        }
    }

    if (_log.isDebugEnabled()) {
        _log.debug("Search found " + hits.getLength() + " results in " + hits.getSearchTime() + "ms");
    }

    return hits;
}

From source file:com.liferay.portal.search.lucene.LuceneIndexSearcherImpl.java

License:Open Source License

public Hits search(SearchContext searchContext, Query query) throws SearchException {

    if (_log.isDebugEnabled()) {
        _log.debug("Query " + query);
    }/*from www.  j  av  a  2s. c  o  m*/

    Hits hits = null;

    org.apache.lucene.search.IndexSearcher indexSearcher = null;
    Map<String, Facet> facets = null;
    BrowseRequest browseRequest = null;
    Browsable browsable = null;

    try {
        indexSearcher = LuceneHelperUtil.getSearcher(searchContext.getCompanyId(), true);

        List<FacetHandler<?>> facetHandlers = new ArrayList<FacetHandler<?>>();

        facets = searchContext.getFacets();

        for (Facet facet : facets.values()) {
            if (facet.isStatic()) {
                continue;
            }

            FacetConfiguration facetConfiguration = facet.getFacetConfiguration();

            if (facet instanceof MultiValueFacet) {
                MultiValueFacetHandler multiValueFacetHandler = new MultiValueFacetHandler(
                        facetConfiguration.getFieldName(), facetConfiguration.getFieldName());

                JSONObject dataJSONObject = facetConfiguration.getData();

                if (dataJSONObject.has("maxTerms")) {
                    multiValueFacetHandler.setMaxItems(dataJSONObject.getInt("maxTerms"));
                }

                facetHandlers.add(multiValueFacetHandler);
            } else if (facet instanceof RangeFacet) {
                List<String> ranges = new ArrayList<String>();

                JSONObject dataJSONObject = facetConfiguration.getData();

                JSONArray rangesJSONArray = dataJSONObject.getJSONArray("ranges");

                if (rangesJSONArray != null) {
                    for (int i = 0; i < rangesJSONArray.length(); i++) {
                        JSONObject rangeJSONObject = rangesJSONArray.getJSONObject(i);

                        ranges.add(rangeJSONObject.getString("range"));
                    }
                }

                RangeFacetHandler rangeFacetHandler = new RangeFacetHandler(facetConfiguration.getFieldName(),
                        facetConfiguration.getFieldName(), ranges);

                rangeFacetHandler.setTermCountSize(TermCountSize.large);

                facetHandlers.add(rangeFacetHandler);
            } else if (facet instanceof SimpleFacet) {
                SimpleFacetHandler simpleFacetHandler = new SimpleFacetHandler(
                        facetConfiguration.getFieldName(), facetConfiguration.getFieldName());

                facetHandlers.add(simpleFacetHandler);
            }
        }

        BoboIndexReader boboIndexReader = BoboIndexReader.getInstance(indexSearcher.getIndexReader(),
                facetHandlers);

        SortField[] sortFields = new SortField[0];

        Sort[] sorts = searchContext.getSorts();

        if (sorts != null) {
            sortFields = new SortField[sorts.length];

            for (int i = 0; i < sorts.length; i++) {
                Sort sort = sorts[i];

                sortFields[i] = new SortField(sort.getFieldName(), sort.getType(), sort.isReverse());
            }
        }

        browseRequest = new BrowseRequest();

        for (Facet facet : facets.values()) {
            if (facet.isStatic()) {
                continue;
            }

            FacetConfiguration facetConfiguration = facet.getFacetConfiguration();

            FacetSpec facetSpec = new FacetSpec();

            facetSpec.setOrderBy(FacetSortSpec.valueOf(facetConfiguration.getOrder()));

            browseRequest.setFacetSpec(facet.getFieldName(), facetSpec);
        }

        browseRequest.setCount(PropsValues.INDEX_SEARCH_LIMIT);
        browseRequest.setOffset(0);
        browseRequest.setQuery((org.apache.lucene.search.Query) QueryTranslatorUtil.translate(query));
        browseRequest.setSort(sortFields);

        browsable = new BoboBrowser(boboIndexReader);

        long startTime = System.currentTimeMillis();

        BrowseResult browseResult = browsable.browse(browseRequest);

        BrowseHit[] browseHits = browseResult.getHits();

        long endTime = System.currentTimeMillis();

        float searchTime = (float) (endTime - startTime) / Time.SECOND;

        hits = toHits(indexSearcher, new HitDocs(browseHits), query, startTime, searchTime,
                searchContext.getStart(), searchContext.getEnd());

        Map<String, FacetAccessible> facetMap = browseResult.getFacetMap();

        for (Map.Entry<String, FacetAccessible> entry : facetMap.entrySet()) {

            Facet facet = facets.get(entry.getKey());

            FacetAccessible facetAccessible = entry.getValue();

            FacetCollector facetCollector = new BoboFacetCollector(entry.getKey(), facetAccessible);

            facet.setFacetCollector(facetCollector);
        }
    } catch (BooleanQuery.TooManyClauses tmc) {
        int maxClauseCount = BooleanQuery.getMaxClauseCount();

        BooleanQuery.setMaxClauseCount(Integer.MAX_VALUE);

        try {
            long startTime = System.currentTimeMillis();

            BrowseResult result = browsable.browse(browseRequest);

            BrowseHit[] browseHits = result.getHits();

            long endTime = System.currentTimeMillis();

            float searchTime = (float) (endTime - startTime) / Time.SECOND;

            hits = toHits(indexSearcher, new HitDocs(browseHits), query, startTime, searchTime,
                    searchContext.getStart(), searchContext.getEnd());

            Map<String, FacetAccessible> facetMap = result.getFacetMap();

            for (Map.Entry<String, FacetAccessible> entry : facetMap.entrySet()) {

                Facet facet = facets.get(entry.getKey());

                FacetAccessible facetAccessible = entry.getValue();

                FacetCollector facetCollector = new BoboFacetCollector(entry.getKey(), facetAccessible);

                facet.setFacetCollector(facetCollector);
            }
        } catch (Exception e) {
            throw new SearchException(e);
        } finally {
            BooleanQuery.setMaxClauseCount(maxClauseCount);
        }
    } catch (ParseException pe) {
        _log.error("Query " + query, pe);

        return new HitsImpl();
    } catch (Exception e) {
        throw new SearchException(e);
    } finally {
        if (browsable != null) {
            try {
                browsable.close();
            } catch (IOException ioe) {
                _log.error(ioe, ioe);
            }
        }

        if (indexSearcher != null) {
            try {
                indexSearcher.close();
            } catch (IOException ioe) {
                _log.error(ioe, ioe);
            }
        }
    }

    if (_log.isDebugEnabled()) {
        _log.debug("Search found " + hits.getLength() + " results in " + hits.getSearchTime() + "ms");
    }

    return hits;
}

From source file:com.liferay.portal.search.lucene33.LuceneIndexSearcherImpl.java

License:Open Source License

public Hits search(long companyId, Query query, Sort[] sorts, int start, int end) throws SearchException {

    if (_log.isDebugEnabled()) {
        _log.debug("Query " + query);
    }//from   w  w w.ja v a 2 s  .  c om

    Hits hits = null;

    org.apache.lucene.search.IndexSearcher searcher = null;
    org.apache.lucene.search.Sort luceneSort = null;

    try {
        searcher = LuceneHelperUtil.getSearcher(companyId, true);

        if (sorts != null) {
            searcher.setDefaultFieldSortScoring(true, true);

            SortField[] sortFields = new SortField[sorts.length];

            for (int i = 0; i < sorts.length; i++) {
                Sort sort = sorts[i];

                sortFields[i] = new SortField(sort.getFieldName(), sort.getType(), sort.isReverse());
            }

            luceneSort = new org.apache.lucene.search.Sort(sortFields);
        }

        long startTime = System.currentTimeMillis();

        org.apache.lucene.search.TopDocs luceneHits;

        if (luceneSort != null) {
            luceneHits = searcher.search(QueryTranslator.translate(query), Integer.MAX_VALUE, luceneSort);
        } else {
            luceneHits = searcher.search(QueryTranslator.translate(query), Integer.MAX_VALUE);
        }

        long endTime = System.currentTimeMillis();

        float searchTime = (float) (endTime - startTime) / Time.SECOND;

        hits = subset(searcher, luceneHits, query, startTime, searchTime, start, end);
    } catch (BooleanQuery.TooManyClauses tmc) {
        int maxClauseCount = BooleanQuery.getMaxClauseCount();

        BooleanQuery.setMaxClauseCount(Integer.MAX_VALUE);

        try {
            long startTime = System.currentTimeMillis();

            org.apache.lucene.search.TopDocs luceneHits = searcher.search(QueryTranslator.translate(query),
                    Integer.MAX_VALUE);

            long endTime = System.currentTimeMillis();

            float searchTime = (float) (endTime - startTime) / Time.SECOND;

            hits = subset(searcher, luceneHits, query, startTime, searchTime, start, end);
        } catch (Exception e) {
            throw new SearchException(e);
        } finally {
            BooleanQuery.setMaxClauseCount(maxClauseCount);
        }
    } catch (ParseException pe) {
        _log.error("Query: " + query, pe);

        return new HitsImpl();
    } catch (Exception e) {
        throw new SearchException(e);
    } finally {
        try {
            if (searcher != null) {
                searcher.close();
            }
        } catch (IOException ioe) {
            throw new SearchException(ioe);
        }
    }

    if (_log.isDebugEnabled()) {
        _log.debug("Search found " + hits.getLength() + " results in " + hits.getSearchTime() + "ms");
    }

    return hits;
}

From source file:com.redhat.satellite.search.rpc.handlers.IndexHandler.java

License:Open Source License

/**
 * Search index//from  w  ww  .ja v a 2 s. c  o  m
 *
 * @param sessionId
 *            user's application session id
 * @param indexName
 *            index to use
 * @param query
 *            search query
 *  @param lang
 *            language
 *  @param isFineGrained
 *            if set will restrict matches to be stricter and less forgiving
 * @return list of document ids as results
 * @throws XmlRpcFault something bad happened
 */
public List<Result> search(long sessionId, String indexName, String query, String lang, boolean isFineGrained)
        throws XmlRpcFault {
    if (log.isDebugEnabled()) {
        log.debug("IndexHandler:: searching for: " + query + ", indexName = " + indexName + ", lang = " + lang);
    }
    boolean retry = true;
    while (retry) {
        try {
            retry = false;
            List<Result> hits = indexManager.search(indexName, query, lang, isFineGrained);
            if (indexName.equals("package") || indexName.equals("errata") || indexName.equals("server")) {
                return screenHits(sessionId, indexName, hits);
            }
            return hits;
        } catch (IndexingException e) {
            log.error("Caught exception: ", e);
            throw new XmlRpcFault(INDEX_ERROR, e.getMessage());
        } catch (QueryParseException e) {
            log.error("Caught exception: ", e);
            throw new XmlRpcFault(QUERY_ERROR, e.getMessage());
        } catch (SQLException e) {
            log.error("Caught exception: ", e);
            throw new XmlRpcFault(DB_ERROR, e.getMessage());
        } catch (BooleanQuery.TooManyClauses e) {
            int oldQueries = BooleanQuery.getMaxClauseCount();
            if (Integer.MAX_VALUE / 2 > oldQueries) {
                // increase number of max clause count
                // if there's no overflow danger
                int newQueries = oldQueries * 2;
                log.error("Too many hits for query: " + oldQueries + ".  Increasing max clause count to "
                        + newQueries + "\nexception message: " + e.getMessage());
                BooleanQuery.setMaxClauseCount(newQueries);
                retry = true;
            } else {
                // there's no more help
                throw e;
            }
        }
    }
    // return just because of compiler
    return null;
}

From source file:com.searchbox.solr.CategoryLikeThis.java

License:Apache License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    numRequests++;/*from  ww  w . j  ava  2 s . c  o  m*/
    long startTime = System.currentTimeMillis();
    if (!keystate) {
        LOGGER.error(
                "License key failure, not performing clt query. Please email contact@searchbox.com for more information.");
        return;
    }

    try {
        SolrParams params = req.getParams();
        String senseField = params.get(SenseParams.SENSE_FIELD, SenseParams.DEFAULT_SENSE_FIELD);
        BooleanQuery catfilter = new BooleanQuery();
        // Set field flags
        ReturnFields returnFields = new SolrReturnFields(req);
        rsp.setReturnFields(returnFields);
        int flags = 0;
        if (returnFields.wantsScore()) {
            flags |= SolrIndexSearcher.GET_SCORES;
        }

        String defType = params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE);
        String q = params.get(CommonParams.Q);
        Query query = null;
        SortSpec sortSpec = null;
        List<Query> filters = new LinkedList<Query>();
        List<RealTermFreqVector> prototypetfs = new LinkedList<RealTermFreqVector>();

        try {
            if (q != null) {
                QParser parser = QParser.getParser(q, defType, req);
                query = parser.getQuery();
                sortSpec = parser.getSort(true);
            }

            String[] fqs = req.getParams().getParams(CommonParams.FQ);
            if (fqs != null && fqs.length != 0) {
                for (String fq : fqs) {
                    if (fq != null && fq.trim().length() != 0) {
                        QParser fqp = QParser.getParser(fq, null, req);
                        filters.add(fqp.getQuery());
                    }
                }
            }
        } catch (Exception e) {
            numErrors++;
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
        }

        SolrIndexSearcher searcher = req.getSearcher();
        DocListAndSet cltDocs = null;

        // Parse Required Params
        // This will either have a single Reader or valid query
        Reader reader = null;
        try {
            if (q == null || q.trim().length() < 1) {
                Iterable<ContentStream> streams = req.getContentStreams();
                if (streams != null) {
                    Iterator<ContentStream> iter = streams.iterator();
                    if (iter.hasNext()) {
                        reader = iter.next().getReader();
                    }
                    if (iter.hasNext()) {
                        numErrors++;
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                                "SenseLikeThis does not support multiple ContentStreams");
                    }
                }
            }

            int start = params.getInt(CommonParams.START, 0);
            int rows = params.getInt(CommonParams.ROWS, 10);

            // Find documents SenseLikeThis - either with a reader or a query
            // --------------------------------------------------------------------------------
            if (reader != null) {
                numErrors++;
                throw new RuntimeException("SLT based on a reader is not yet implemented");
            } else if (q != null) {

                LOGGER.debug("Query for category:\t" + query);
                DocList match = searcher.getDocList(query, null, null, 0, 10, flags); // get first 10
                if (match.size() == 0) { // no docs to make prototype!
                    LOGGER.info("No documents found for prototype!");
                    rsp.add("response", new DocListAndSet());
                    return;
                }

                HashMap<String, Float> overallFreqMap = new HashMap<String, Float>();
                // Create the TF of blah blah blah
                DocIterator iterator = match.iterator();
                while (iterator.hasNext()) {
                    // do a MoreLikeThis query for each document in results
                    int id = iterator.nextDoc();
                    LOGGER.trace("Working on doc:\t" + id);
                    RealTermFreqVector rtv = new RealTermFreqVector(id, searcher.getIndexReader(), senseField);
                    for (int zz = 0; zz < rtv.getSize(); zz++) {
                        Float prev = overallFreqMap.get(rtv.getTerms()[zz]);
                        if (prev == null) {
                            prev = 0f;
                        }
                        overallFreqMap.put(rtv.getTerms()[zz], rtv.getFreqs()[zz] + prev);
                    }
                    prototypetfs.add(rtv);
                }

                List<String> sortedKeys = Ordering.natural().onResultOf(Functions.forMap(overallFreqMap))
                        .immutableSortedCopy(overallFreqMap.keySet());
                int keyiter = Math.min(sortedKeys.size() - 1, BooleanQuery.getMaxClauseCount() - 1);
                LOGGER.debug("I have this many terms:\t" + sortedKeys.size());
                LOGGER.debug("And i'm going to use this many:\t" + keyiter);
                for (; keyiter >= 0; keyiter--) {
                    TermQuery tq = new TermQuery(new Term(senseField, sortedKeys.get(keyiter)));
                    catfilter.add(tq, BooleanClause.Occur.SHOULD);
                }

            } else {
                numErrors++;
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                        "CategoryLikeThis requires either a query (?q=) or text to find similar documents.");
            }

            LOGGER.debug("document filter is: \t" + catfilter);
            CategorizationBase model = new CategorizationBase(prototypetfs);
            CategoryQuery clt = CategoryQuery.CategoryQueryForDocument(catfilter, model,
                    searcher.getIndexReader(), senseField);
            DocSet filtered = searcher.getDocSet(filters);
            cltDocs = searcher.getDocListAndSet(clt, filtered, Sort.RELEVANCE, start, rows, flags);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }

        if (cltDocs == null) {
            numEmpty++;
            cltDocs = new DocListAndSet(); // avoid NPE
        }
        rsp.add("response", cltDocs.docList);

        // maybe facet the results
        if (params.getBool(FacetParams.FACET, false)) {
            if (cltDocs.docSet == null) {
                rsp.add("facet_counts", null);
            } else {
                SimpleFacets f = new SimpleFacets(req, cltDocs.docSet, params);
                rsp.add("facet_counts", f.getFacetCounts());
            }
        }
    } catch (Exception e) {
        numErrors++;
    } finally {
        totalTime += System.currentTimeMillis() - startTime;
    }

}