Example usage for org.apache.solr.common.params DisMaxParams QF

List of usage examples for org.apache.solr.common.params DisMaxParams QF

Introduction

In this page you can find the example usage for org.apache.solr.common.params DisMaxParams QF.

Prototype

String QF

To view the source code for org.apache.solr.common.params DisMaxParams QF.

Click Source Link

Document

query and init param for query fields

Usage

From source file:com.tsgrp.solr.handler.NYPhilSearchHandler.java

License:Mozilla Public License

/**
 * @see org.apache.solr.handler.component.SearchHandler#handleRequestBody(org.apache.solr.request.SolrQueryRequest,
 *      org.apache.solr.request.SolrQueryResponse)
 *///  w w w .  java  2s  .c  o m
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp)
        throws Exception, ParseException, InstantiationException, IllegalAccessException {
    SolrParams requestParams = req.getParams();

    NamedList<Object> params = requestParams.toNamedList();

    //for now lets echo the handler and all the params for debugging purposes
    //params.add( CommonParams.HEADER_ECHO_HANDLER, Boolean.TRUE );
    //params.add( CommonParams.HEADER_ECHO_PARAMS, CommonParams.EchoParamStyle.ALL );

    String rows = (String) params.get(CommonParams.ROWS);
    if (rows == null || rows.trim().length() < 1) {
        //setup items per page, default to 10 items
        params.add(CommonParams.ROWS, 10);
        rows = "10";
    }

    //enable faceting and only return facets with at least 1 item
    params.add(FacetParams.FACET, Boolean.TRUE);
    params.add(FacetParams.FACET_MINCOUNT, 1);

    //defult ordering to index order (alphabetical)
    params.add(FacetParams.FACET_SORT, FacetParams.FACET_SORT_INDEX);

    //only return a maximum of 10 facet values
    params.add(FacetParams.FACET_LIMIT, 10);

    //always add facets unless they are explicitly not requested
    String addFacets = (String) params.get(PARAM_GENERATE_FACETS);
    boolean generateFacets = (addFacets == null) ? true : addFacets.equalsIgnoreCase("true");

    //enable highlighting
    params.add(HighlightParams.HIGHLIGHT, Boolean.TRUE);

    //remove the query if provided, always want to use our translated query
    String originalQuery = (String) params.remove(CommonParams.Q);

    if (logger.isDebugEnabled()) {
        logger.debug("Original query: " + originalQuery);
    }

    //setup sorting params
    String sortColumn = (String) params.get(PARAM_SORT_COLUMN);
    String sortOrder = (String) params.get(PARAM_SORT_ORDER);
    if (sortColumn != null && sortOrder != null) {
        params.add(CommonParams.SORT, sortColumn + " " + sortOrder);
    }

    //get date fields
    String sDateFrom = (String) params.get(PARAM_DATE_FROM);
    String sDateTo = (String) params.get(PARAM_DATE_TO);
    Date dateFrom = null;
    Date dateTo = null;
    if (sDateFrom != null && sDateTo != null) {
        dateFrom = DATE_FORMAT_PARAM.parse(sDateFrom);
        dateTo = DATE_FORMAT_PARAM.parse(sDateTo);
    }

    String keywords = (String) params.get(PARAM_KEYWORDS);

    if (keywords == null || keywords.trim().length() < 1) {
        //query everything since nothing was provided
        keywords = "*";
    }

    //always use the extended dismax parser
    params.add("defType", ExtendedDismaxQParserPlugin.NAME);

    //the keywords sent in are the query since we're in DISMAX mode
    params.add(CommonParams.Q, keywords);

    String pageIndex = (String) params.get(PARAM_PAGE_INDEX);
    String resultsPerPage = (String) params.get(PARAM_RESULTS_PER_PAGE);
    if (resultsPerPage == null || resultsPerPage.trim().length() < 0) {
        resultsPerPage = "10";
    }

    if (pageIndex == null || pageIndex.trim().length() < 1) {
        pageIndex = "1";
    }

    //figure out the skip count, use the (pageIndex - 1) * resultsPerPage
    //ie pageIndex = 3, 10 results per page, we'll set start to (3-1)*10 = 20
    int start = (Integer.parseInt(pageIndex) - 1) * Integer.parseInt(resultsPerPage);
    params.add(CommonParams.START, start);

    String doctype = (String) params.get(PARAM_DOCTYPE);
    if (doctype == null || doctype.trim().length() < 1) {
        doctype = "";
    }

    String facetQuery = (String) params.get(PARAM_FACET_QUERY);
    if (facetQuery != null && facetQuery.trim().length() > 0) {
        //facetQuery is pre formatted and correct, just add it as a filter query
        params.add(CommonParams.FQ, facetQuery);
    }

    String suggestedQuery = (String) params.get(PARAM_SUGGESTED_QUERY);
    if (suggestedQuery != null && suggestedQuery.trim().length() > 0) {
        //suggestedQuery is pre formatted and correct, just add it as a filter query
        params.add(CommonParams.FQ, suggestedQuery);
    }

    //base fields
    params.add(DisMaxParams.QF, "nyp:DocumentType");
    params.add(DisMaxParams.QF, "nyp:Notes");

    //program fields
    params.add(DisMaxParams.QF, "npp:ProgramID");
    params.add(DisMaxParams.QF, "npp:Season");
    params.add(DisMaxParams.QF, "npp:OrchestraCode");
    params.add(DisMaxParams.QF, "npp:OrchestraName");
    params.add(DisMaxParams.QF, "npp:LocationName");
    params.add(DisMaxParams.QF, "npp:VenueName");
    params.add(DisMaxParams.QF, "npp:EventTypeName");
    params.add(DisMaxParams.QF, "npp:SubEventName");
    params.add(DisMaxParams.QF, "npp:ConductorName");
    params.add(DisMaxParams.QF, "npp:SoloistsNames");
    params.add(DisMaxParams.QF, "npp:SoloistsInstrumentName");
    params.add(DisMaxParams.QF, "npp:WorksComposerNames");
    params.add(DisMaxParams.QF, "npp:WorksTitle");
    params.add(DisMaxParams.QF, "npp:WorksShortTitle");
    params.add(DisMaxParams.QF, "npp:WorksConductorNames");

    //printedMusic fields
    params.add(DisMaxParams.QF, "npm:LibraryID");
    params.add(DisMaxParams.QF, "npm:ShortTitle");
    params.add(DisMaxParams.QF, "npm:ComposerName");
    params.add(DisMaxParams.QF, "npm:PublisherName");
    params.add(DisMaxParams.QF, "npm:ComposerNameTitle");
    params.add(DisMaxParams.QF, "npm:ScoreMarkingArtist");
    params.add(DisMaxParams.QF, "npm:ScoreEditionTypeDesc");
    params.add(DisMaxParams.QF, "npm:ScoreNotes");

    //part fields
    params.add(DisMaxParams.QF, "npm:PartTypeDesc");
    params.add(DisMaxParams.QF, "npm:PartMarkingArtist");
    params.add(DisMaxParams.QF, "npm:UsedByArtistName");

    //businessRecord fields
    params.add(DisMaxParams.QF, "npb:BoxNumber");
    params.add(DisMaxParams.QF, "npb:RecordGroup");
    params.add(DisMaxParams.QF, "npb:Series");
    params.add(DisMaxParams.QF, "npb:SubSeries");
    params.add(DisMaxParams.QF, "npb:Folder");
    params.add(DisMaxParams.QF, "npb:Names");
    params.add(DisMaxParams.QF, "npb:Subject");
    params.add(DisMaxParams.QF, "npb:Abstract");

    //visual fields
    params.add(DisMaxParams.QF, "npv:ID");
    params.add(DisMaxParams.QF, "npv:BoxNumber");
    params.add(DisMaxParams.QF, "npv:PhilharmonicSource");
    params.add(DisMaxParams.QF, "npv:OutsideSource");
    params.add(DisMaxParams.QF, "npv:Photographer");
    params.add(DisMaxParams.QF, "npv:CopyrightHolder");
    params.add(DisMaxParams.QF, "npv:PlaceOfImage");
    params.add(DisMaxParams.QF, "npv:PersonalNames");
    params.add(DisMaxParams.QF, "npv:CorporateNames");
    params.add(DisMaxParams.QF, "npv:Event");
    params.add(DisMaxParams.QF, "npv:ImageType");
    params.add(DisMaxParams.QF, "npv:LocationName");
    params.add(DisMaxParams.QF, "npv:VenueName");

    //audio fields
    params.add(DisMaxParams.QF, "npa:ProgramID");
    params.add(DisMaxParams.QF, "npa:Location");
    params.add(DisMaxParams.QF, "npa:EventTypeName");
    params.add(DisMaxParams.QF, "npa:ConductorName");
    params.add(DisMaxParams.QF, "npa:SoloistsAndInstruments");
    params.add(DisMaxParams.QF, "npa:ComposerWork");
    params.add(DisMaxParams.QF, "npa:OrchestraName");
    params.add(DisMaxParams.QF, "npa:IntermissionFeature");
    params.add(DisMaxParams.QF, "npa:LocationName");
    params.add(DisMaxParams.QF, "npa:VenueName");
    params.add(DisMaxParams.QF, "npa:SubEventName");
    params.add(DisMaxParams.QF, "npa:URLLocation");
    params.add(DisMaxParams.QF, "npa:IntermissionGuests");
    params.add(DisMaxParams.QF, "npa:Announcer");

    //video fields
    params.add(DisMaxParams.QF, "npx:ProgramID");
    params.add(DisMaxParams.QF, "npx:Location");
    params.add(DisMaxParams.QF, "npx:EventTypeName");
    params.add(DisMaxParams.QF, "npx:ConductorName");
    params.add(DisMaxParams.QF, "npx:SoloistsAndInstruments");
    params.add(DisMaxParams.QF, "npx:ComposerNameWork");
    params.add(DisMaxParams.QF, "npx:OrchestraName");
    params.add(DisMaxParams.QF, "npx:IntermissionFeature");
    params.add(DisMaxParams.QF, "npx:LocationName");
    params.add(DisMaxParams.QF, "npx:VenueName");
    params.add(DisMaxParams.QF, "npx:SubEventName");
    params.add(DisMaxParams.QF, "npx:IntermissionGuests");
    params.add(DisMaxParams.QF, "npx:Announcer");

    params.add(DisMaxParams.QF, "npt:tagged");

    //add in all the restrictions that can be applied to a document type count in the same filter query

    //this includes all DATE range queries
    //if a date query does not exist, the TYPE is queried so all results are shown
    //restriction is applied to business records so only the web publishable items are shown
    StringBuffer filterTypesCountsQuery = new StringBuffer();

    if (dateFrom != null && dateTo != null) {
        //program date query
        filterTypesCountsQuery.append("(npp\\:Date:[" + solrField.toExternal(dateFrom) + " TO "
                + solrField.toExternal(dateTo) + "])");

        //printedMusic (scores) and parts have no date range, so just or in the type so those results come back
        filterTypesCountsQuery.append(" OR (nyp\\:DocumentType:Printed Music)");
        filterTypesCountsQuery.append(" OR (nyp\\:DocumentType:Part)");

        //business record range AND web publishable restriction
        filterTypesCountsQuery
                .append(" OR (nyp\\:WebPublishable:true AND ((npb\\:DateFrom:[" + solrField.toExternal(dateFrom)
                        + " TO *] AND npb\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "])");
        filterTypesCountsQuery.append(" OR (npb\\:DateFrom:[* TO " + solrField.toExternal(dateFrom)
                + "] AND npb\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "] AND npb\\:DateTo:["
                + solrField.toExternal(dateFrom) + " TO *])");
        filterTypesCountsQuery.append(" OR (npb\\:DateFrom:[" + solrField.toExternal(dateFrom)
                + " TO *] AND npb\\:DateTo:[" + solrField.toExternal(dateTo)
                + " TO *] AND npb\\:DateFrom:[* TO " + solrField.toExternal(dateFrom) + "])))");

        //visual
        filterTypesCountsQuery.append(" OR ((npv\\:DateFrom:[" + solrField.toExternal(dateFrom)
                + " TO *] AND npv\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "])");
        filterTypesCountsQuery.append(" OR (npv\\:DateFrom:[* TO " + solrField.toExternal(dateFrom)
                + "] AND npv\\:DateTo:[* TO " + solrField.toExternal(dateTo) + "] AND npv\\:DateTo:["
                + solrField.toExternal(dateFrom) + " TO *])");
        filterTypesCountsQuery.append(" OR (npv\\:DateFrom:[" + solrField.toExternal(dateFrom)
                + " TO *] AND npv\\:DateTo:[" + solrField.toExternal(dateTo)
                + " TO *] AND npv\\:DateFrom:[* TO " + solrField.toExternal(dateFrom) + "]))");

        //audio
        filterTypesCountsQuery.append(" OR (npa\\:Date:[" + solrField.toExternal(dateFrom) + " TO "
                + solrField.toExternal(dateTo) + "])");

        //video
        filterTypesCountsQuery.append(" OR (npx\\:Date:[" + solrField.toExternal(dateFrom) + " TO "
                + solrField.toExternal(dateTo) + "])");
    } else {
        //no date queries, just perform query based on type restrictions            
        filterTypesCountsQuery.append(
                "(nyp\\:DocumentType:Program) OR (nyp\\:DocumentType:Printed Music)  OR (nyp\\:DocumentType:Part) OR (nyp\\:DocumentType:Business Record AND nyp\\:WebPublishable:true)");
        filterTypesCountsQuery.append(
                " OR (nyp\\:DocumentType:Visual) OR (nyp\\:DocumentType:Audio) OR (nyp\\:DocumentType:Video)");
    }

    params.add(CommonParams.FQ, filterTypesCountsQuery);

    //default facet for document type that will always return the counts regardless of filter queries applied
    params.add(FacetParams.FACET_FIELD, "{!ex=test}nyp:DocumentType_facet");
    //allow this facet to always display all values, even when there are 0 results for the type
    params.add("f.nyp:DocumentType_facet.facet.mincount", 0);

    if (doctype.equalsIgnoreCase("program")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Program");

        if (generateFacets) {
            params.add(FacetParams.FACET_FIELD, "npp:ConductorName_facet");
            params.add(FacetParams.FACET_FIELD, "npp:SoloistsNames_facet");
            params.add(FacetParams.FACET_FIELD, "npp:WorksComposerNames_facet");
            params.add(FacetParams.FACET_FIELD, "npp:LocationName_facet");
            params.add(FacetParams.FACET_FIELD, "npp:VenueName_facet");
            params.add(FacetParams.FACET_FIELD, "npp:EventTypeName_facet");
            params.add(FacetParams.FACET_FIELD, "npp:Season_facet");
        }
    } else if (doctype.equalsIgnoreCase("printedMusic")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Printed Music");

        if (generateFacets) {
            params.add(FacetParams.FACET_FIELD, "npm:ScoreMarkingArtist_facet");
            params.add(FacetParams.FACET_FIELD, "npm:ComposerName_facet");
        }

    } else if (doctype.equalsIgnoreCase("part")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Part");

        if (generateFacets) {
            params.add(FacetParams.FACET_FIELD, "npm:ComposerName_facet");
            params.add(FacetParams.FACET_FIELD, "npm:UsedByArtistName_facet");
            params.add(FacetParams.FACET_FIELD, "npm:PartMarkingArtist_facet");
            params.add(FacetParams.FACET_FIELD, "npm:PartTypeDesc_facet");
        }

        // if sort parameter has been supplied (e.g. non-facet search) - apply additional part sorting
        if (sortColumn != null && sortOrder != null) {
            logger.debug("Addition additional sort parameter for PART type...");
            String prevParams = (String) params.get(CommonParams.SORT);
            String newParams = prevParams + ", npm:PartID ASC";
            params.add(CommonParams.SORT, newParams);
        }

    } else if (doctype.equalsIgnoreCase("businessRecord")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Business Record");

        if (generateFacets) {
            params.add(FacetParams.FACET_FIELD, "npb:Names_facet");
            params.add(FacetParams.FACET_FIELD, "npb:Subject_facet");
            params.add(FacetParams.FACET_FIELD, "npb:RecordGroup_facet");
            params.add(FacetParams.FACET_FIELD, "npb:Series_facet");
            params.add(FacetParams.FACET_FIELD, "npb:SubSeries_facet");
        }

    } else if (doctype.equalsIgnoreCase("visual")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Visual");

        if (generateFacets) {
            params.add(FacetParams.FACET_FIELD, "npv:Photographer_facet");
            params.add(FacetParams.FACET_FIELD, "npv:CopyrightHolder_facet");
            params.add(FacetParams.FACET_FIELD, "npv:ImageType_facet");
            params.add(FacetParams.FACET_FIELD, "npv:PlaceOfImage_facet");
            params.add(FacetParams.FACET_FIELD, "npv:Event_facet");
            params.add(FacetParams.FACET_FIELD, "npv:PersonalNames_facet");
            params.add(FacetParams.FACET_FIELD, "npv:LocationName_facet");
            params.add(FacetParams.FACET_FIELD, "npv:VenueName_facet");
        }

    } else if (doctype.equalsIgnoreCase("audio")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Audio");

        //no facets being generated
    } else if (doctype.equalsIgnoreCase("video")) {
        //set the document type restriction
        params.add(CommonParams.FQ, "{!tag=test}nyp\\:DocumentType:Video");

        //no facets being generated
    } else {
        logger.error("Invalid document type: " + doctype);
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Invalid document type: " + doctype);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Params: " + params);
    }

    //set the new request parameters, then call the default handler behavior
    req.setParams(SolrParams.toSolrParams(params));

    super.handleRequestBody(req, rsp);
}

From source file:net.yacy.cora.federate.solr.connector.AbstractSolrConnector.java

License:Open Source License

public static SolrQuery getSolrQuery(final String querystring, final String sort, final int offset,
        final int count, final String... fields) {
    // construct query
    final SolrQuery params = new SolrQuery();
    //if (count < 2 && querystring.startsWith("{!raw f=")) {
    //    params.setQuery("*:*");
    //    params.addFilterQuery(querystring);
    //} else {/*w w w.  ja  v a 2  s .co m*/
    params.setQuery(querystring);
    //}
    params.clearSorts();
    if (sort != null) {
        params.set(CommonParams.SORT, sort);
    }
    params.setRows(count);
    params.setStart(offset);
    params.setFacet(false);
    if (fields != null && fields.length > 0)
        params.setFields(fields);
    params.setIncludeScore(false);
    params.setParam("defType", "edismax");
    params.setParam(DisMaxParams.QF, CollectionSchema.text_t.getSolrFieldName() + "^1.0");
    return params;
}

From source file:net.yacy.http.servlets.GSAsearchServlet.java

License:Open Source License

/**
 * from here copy of htroot/gsa/gsasearchresult.java
 * with modification to use HttpServletRequest instead of (yacy) RequestHeader
 *///  w  w  w.j  a  v a 2s.c  o  m

public static void respond(final HttpServletRequest header, final Switchboard sb, final OutputStream out) {

    // remember the peer contact for peer statistics
    String clientip = header.getHeader(HeaderFramework.CONNECTION_PROP_CLIENTIP);
    if (clientip == null)
        clientip = "<unknown>"; // read an artificial header addendum
    String userAgent = header.getHeader(HeaderFramework.USER_AGENT);
    if (userAgent == null)
        userAgent = "<unknown>";
    sb.peers.peerActions.setUserAgent(clientip, userAgent);

    // --- handled by Servlet securityHandler
    // check if user is allowed to search (can be switched in /ConfigPortal.html)
    boolean authenticated = header.isUserInRole(UserDB.AccessRight.ADMIN_RIGHT.toString()); //sb.adminAuthenticated(header) >= 2;
    // final boolean searchAllowed = authenticated || sb.getConfigBool(SwitchboardConstants.PUBLIC_SEARCHPAGE, true);
    // if (!searchAllowed) return null;

    // create post
    serverObjects post = new serverObjects();
    post.put(CommonParams.Q, "");
    post.put("num", "0");
    // convert servletrequest parameter to old style serverObjects map
    Map<String, String[]> map = header.getParameterMap();
    Iterator<Map.Entry<String, String[]>> it = map.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, String[]> param = it.next();
        post.put(param.getKey(), param.getValue()); // hint: post.put uses String[] for String value anyways
    }

    ConcurrentLog.info("GSA Query", post.toString());
    sb.intermissionAllThreads(3000); // tell all threads to do nothing for a specific time

    // rename post fields according to result style
    //post.put(CommonParams.Q, post.remove("q")); // same as solr
    //post.put(CommonParams.START, post.remove("start")); // same as solr
    //post.put(, post.remove("client"));//required, example: myfrontend
    //post.put(, post.remove("output"));//required, example: xml,xml_no_dtd
    String originalQuery = post.get(CommonParams.Q, "");
    post.put("originalQuery", originalQuery);

    // get a solr query string
    QueryGoal qg = new QueryGoal(originalQuery);
    List<String> solrFQ = qg.collectionTextFilterQuery(false);
    StringBuilder solrQ = qg.collectionTextQuery();
    post.put("defType", "edismax");
    for (String fq : solrFQ)
        post.add(CommonParams.FQ, fq);
    post.put(CommonParams.Q, solrQ.toString());
    post.put(CommonParams.ROWS, post.remove("num"));
    post.put(CommonParams.ROWS,
            Math.min(post.getInt(CommonParams.ROWS, 10), (authenticated) ? 100000000 : 100));

    // set ranking
    final Ranking ranking = sb.index.fulltext().getDefaultConfiguration().getRanking(0);
    final String qf = ranking.getQueryFields();
    if (!qf.isEmpty())
        post.put(DisMaxParams.QF, qf);
    if (post.containsKey(CommonParams.SORT)) {
        // if a gsa-style sort attribute is given, use this to set the solr sort attribute
        GSAResponseWriter.Sort sort = new GSAResponseWriter.Sort(post.get(CommonParams.SORT, ""));
        String sorts = sort.toSolr();
        if (sorts == null) {
            post.remove(CommonParams.SORT);
        } else {
            post.put(CommonParams.SORT, sorts);
        }
    } else {
        // if no such sort attribute is given, use the ranking as configured for YaCy
        String fq = ranking.getFilterQuery();
        String bq = ranking.getBoostQuery();
        String bf = ranking.getBoostFunction();
        if (fq.length() > 0)
            post.put(CommonParams.FQ, fq);
        if (bq.length() > 0)
            post.put(DisMaxParams.BQ, bq);
        if (bf.length() > 0)
            post.put("boost", bf); // a boost function extension, see http://wiki.apache.org/solr/ExtendedDisMax#bf_.28Boost_Function.2C_additive.29
    }
    String daterange[] = post.remove("daterange");
    if (daterange != null) {
        String origfq = post.get(CommonParams.FQ);
        String datefq = "";
        for (String dr : daterange) {
            String from_to[] = dr.endsWith("..") ? new String[] { dr.substring(0, dr.length() - 2), "" }
                    : dr.startsWith("..") ? new String[] { "", dr.substring(2) } : dr.split("\\.\\.");
            if (from_to.length != 2)
                continue;
            Date from = HeaderFramework.parseGSAFS(from_to[0]);
            if (from == null)
                from = new Date(0);
            Date to = HeaderFramework.parseGSAFS(from_to[1]);
            if (to == null)
                to = new Date();
            to.setTime(to.getTime() + 24L * 60L * 60L * 1000L); // we add a day because the day is inclusive
            String z = CollectionSchema.last_modified.getSolrFieldName() + ":["
                    + ISO8601Formatter.FORMATTER.format(from) + " TO " + ISO8601Formatter.FORMATTER.format(to)
                    + "]";
            datefq = datefq.length() == 0 ? z : " OR " + z;
        }
        if (datefq.length() > 0)
            post.put(CommonParams.FQ,
                    origfq == null || origfq.length() == 0 ? datefq : "(" + origfq + ") AND (" + datefq + ")");
    }
    post.put(CommonParams.FL,
            CollectionSchema.content_type.getSolrFieldName() + ',' + CollectionSchema.id.getSolrFieldName()
                    + ',' + CollectionSchema.sku.getSolrFieldName() + ','
                    + CollectionSchema.title.getSolrFieldName() + ','
                    + CollectionSchema.description_txt.getSolrFieldName() + ','
                    + CollectionSchema.load_date_dt.getSolrFieldName() + ','
                    + CollectionSchema.last_modified.getSolrFieldName() + ','
                    + CollectionSchema.size_i.getSolrFieldName());
    post.put("hl", "true");
    post.put("hl.q", originalQuery);
    post.put("hl.fl", CollectionSchema.description_txt + "," + CollectionSchema.h4_txt.getSolrFieldName() + ","
            + CollectionSchema.h3_txt.getSolrFieldName() + "," + CollectionSchema.h2_txt.getSolrFieldName()
            + "," + CollectionSchema.h1_txt.getSolrFieldName() + ","
            + CollectionSchema.text_t.getSolrFieldName());
    post.put("hl.alternateField", CollectionSchema.description_txt.getSolrFieldName());
    post.put("hl.simple.pre", "<b>");
    post.put("hl.simple.post", "</b>");
    post.put("hl.fragsize", Integer.toString(SearchEvent.SNIPPET_MAX_LENGTH));

    //String[] access = post.remove("access");
    //String[] entqr = post.remove("entqr");

    // add sites operator
    String[] site = post.remove("site"); // example: col1|col2
    if (site != null && site[0].length() > 0) {
        String origfq = post.get(CommonParams.FQ);
        String sitefq = QueryModifier.parseCollectionExpression(site[0]);
        post.put(CommonParams.FQ,
                origfq == null || origfq.length() == 0 ? sitefq : "(" + origfq + ") AND (" + sitefq + ")");
    }

    // get the embedded connector
    EmbeddedSolrConnector connector = sb.index.fulltext().getDefaultEmbeddedConnector();
    if (connector == null)
        return;

    // do the solr request
    SolrQueryRequest req = connector.request(post.toSolrParams(null));
    SolrQueryResponse response = null;
    Exception e = null;
    try {
        response = connector.query(req);
    } catch (final SolrException ee) {
        e = ee;
    }
    if (response != null)
        e = response.getException();
    if (e != null) {
        ConcurrentLog.logException(e);
        if (req != null)
            req.close();
        SolrRequestInfo.clearRequestInfo();
        return;
    }

    // set some context for the writer
    /*
    Map<Object,Object> context = req.getContext();
    context.put("ip", header.get("CLIENTIP", ""));
    context.put("client", "vsm_frontent");
    context.put("sort", sort.sort);
    context.put("site", site == null ? "" : site);
    context.put("access", access == null ? "p" : access[0]);
    context.put("entqr", entqr == null ? "3" : entqr[0]);
    */

    // write the result directly to the output stream
    Writer ow = new FastWriter(new OutputStreamWriter(out, UTF8.charset));
    try {
        responseWriter.write(ow, req, response);
        ow.flush();
    } catch (final IOException e1) {
    } finally {
        req.close();
        SolrRequestInfo.clearRequestInfo();
        try {
            ow.close();
        } catch (final IOException e1) {
        }
    }

    // log result
    Object rv = response.getValues().get("response");
    int matches = 0;
    if (rv != null && rv instanceof ResultContext) {
        matches = ((ResultContext) rv).docs.matches();
    } else if (rv != null && rv instanceof SolrDocumentList) {
        matches = (int) ((SolrDocumentList) rv).getNumFound();
    }
    AccessTracker.addToDump(originalQuery, Integer.toString(matches));
    ConcurrentLog.info("GSA Query", "results: " + matches + ", for query:" + post.toString());
}

From source file:net.yacy.http.servlets.SolrSelectServlet.java

License:Open Source License

@Override
public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException {

    HttpServletRequest hrequest = (HttpServletRequest) request;
    HttpServletResponse hresponse = (HttpServletResponse) response;
    SolrQueryRequest req = null;/*from   w  w  w.  j a v a  2  s .  co  m*/

    final Method reqMethod = Method.getMethod(hrequest.getMethod());

    Writer out = null;
    try {
        // prepare request to solr
        MultiMapSolrParams mmsp = SolrRequestParsers.parseQueryString(hrequest.getQueryString());

        Switchboard sb = Switchboard.getSwitchboard();
        boolean authenticated = true;

        // count remote searches if this was part of a p2p search
        if (mmsp.getMap().containsKey("partitions")) {
            final int partitions = mmsp.getInt("partitions", 30);
            sb.searchQueriesGlobal += 1.0f / partitions; // increase query counter
        }

        // get the ranking profile id
        int profileNr = mmsp.getInt("profileNr", 0);

        // rename post fields according to result style
        String querystring = "";
        if (!mmsp.getMap().containsKey(CommonParams.Q) && mmsp.getMap().containsKey(CommonParams.QUERY)) {
            querystring = mmsp.get(CommonParams.QUERY, "");
            mmsp.getMap().remove(CommonParams.QUERY);
            QueryModifier modifier = new QueryModifier(0);
            querystring = modifier.parse(querystring);
            modifier.apply(mmsp);
            QueryGoal qg = new QueryGoal(querystring);
            StringBuilder solrQ = qg.collectionTextQuery();
            mmsp.getMap().put(CommonParams.Q, new String[] { solrQ.toString() }); // sru patch
        }
        String q = mmsp.get(CommonParams.Q, "");
        if (querystring.length() == 0)
            querystring = q;
        if (!mmsp.getMap().containsKey(CommonParams.START)) {
            int startRecord = mmsp.getFieldInt("startRecord", null, 0);
            mmsp.getMap().remove("startRecord");
            mmsp.getMap().put(CommonParams.START, new String[] { Integer.toString(startRecord) }); // sru patch
        }
        if (!mmsp.getMap().containsKey(CommonParams.ROWS)) {
            int maximumRecords = mmsp.getFieldInt("maximumRecords", null, 10);
            mmsp.getMap().remove("maximumRecords");
            mmsp.getMap().put(CommonParams.ROWS, new String[] { Integer.toString(maximumRecords) }); // sru patch
        }
        mmsp.getMap().put(CommonParams.ROWS, new String[] { Integer
                .toString(Math.min(mmsp.getInt(CommonParams.ROWS, 10), (authenticated) ? 100000000 : 100)) });

        // set ranking according to profile number if ranking attributes are not given in the request
        Ranking ranking = sb.index.fulltext().getDefaultConfiguration().getRanking(profileNr);
        if (!mmsp.getMap().containsKey(CommonParams.SORT) && !mmsp.getMap().containsKey(DisMaxParams.BQ)
                && !mmsp.getMap().containsKey(DisMaxParams.BF) && !mmsp.getMap().containsKey("boost")) {
            if (!mmsp.getMap().containsKey("defType"))
                mmsp.getMap().put("defType", new String[] { "edismax" });
            String fq = ranking.getFilterQuery();
            String bq = ranking.getBoostQuery();
            String bf = ranking.getBoostFunction();
            if (fq.length() > 0)
                mmsp.getMap().put(CommonParams.FQ, new String[] { fq });
            if (bq.length() > 0)
                mmsp.getMap().put(DisMaxParams.BQ, new String[] { bq });
            if (bf.length() > 0)
                mmsp.getMap().put("boost", new String[] { bf }); // a boost function extension, see http://wiki.apache.org/solr/ExtendedDisMax#bf_.28Boost_Function.2C_additive.29
        }

        // get a response writer for the result
        String wt = mmsp.get(CommonParams.WT, "xml"); // maybe use /solr/select?q=*:*&start=0&rows=10&wt=exml
        QueryResponseWriter responseWriter = RESPONSE_WRITER.get(wt);
        if (responseWriter == null)
            throw new ServletException("no response writer");
        if (responseWriter instanceof OpensearchResponseWriter) {
            // set the title every time, it is possible that it has changed
            final String promoteSearchPageGreeting = (sb
                    .getConfigBool(SwitchboardConstants.GREETING_NETWORK_NAME, false))
                            ? sb.getConfig("network.unit.description", "")
                            : sb.getConfig(SwitchboardConstants.GREETING, "");
            ((OpensearchResponseWriter) responseWriter).setTitle(promoteSearchPageGreeting);
        }

        // if this is a call to YaCys special search formats, enhance the query with field assignments
        if ((responseWriter instanceof YJsonResponseWriter
                || responseWriter instanceof OpensearchResponseWriter)
                && "true".equals(mmsp.get("hl", "true"))) {
            // add options for snippet generation
            if (!mmsp.getMap().containsKey("hl.q"))
                mmsp.getMap().put("hl.q", new String[] { q });
            if (!mmsp.getMap().containsKey("hl.fl"))
                mmsp.getMap().put("hl.fl",
                        new String[] { CollectionSchema.description_txt + ","
                                + CollectionSchema.h4_txt.getSolrFieldName() + ","
                                + CollectionSchema.h3_txt.getSolrFieldName() + ","
                                + CollectionSchema.h2_txt.getSolrFieldName() + ","
                                + CollectionSchema.h1_txt.getSolrFieldName() + ","
                                + CollectionSchema.text_t.getSolrFieldName() });
            if (!mmsp.getMap().containsKey("hl.alternateField"))
                mmsp.getMap().put("hl.alternateField",
                        new String[] { CollectionSchema.description_txt.getSolrFieldName() });
            if (!mmsp.getMap().containsKey("hl.simple.pre"))
                mmsp.getMap().put("hl.simple.pre", new String[] { "<b>" });
            if (!mmsp.getMap().containsKey("hl.simple.post"))
                mmsp.getMap().put("hl.simple.post", new String[] { "</b>" });
            if (!mmsp.getMap().containsKey("hl.fragsize"))
                mmsp.getMap().put("hl.fragsize",
                        new String[] { Integer.toString(SearchEvent.SNIPPET_MAX_LENGTH) });
        }

        // get the embedded connector
        String requestURI = hrequest.getRequestURI();
        boolean defaultConnector = (requestURI.startsWith("/solr/" + WebgraphSchema.CORE_NAME)) ? false
                : requestURI.startsWith("/solr/" + CollectionSchema.CORE_NAME)
                        || mmsp.get("core", CollectionSchema.CORE_NAME).equals(CollectionSchema.CORE_NAME);
        mmsp.getMap().remove("core");
        SolrConnector connector = defaultConnector ? sb.index.fulltext().getDefaultEmbeddedConnector()
                : sb.index.fulltext().getEmbeddedConnector(WebgraphSchema.CORE_NAME);
        if (connector == null) {
            connector = defaultConnector ? sb.index.fulltext().getDefaultConnector()
                    : sb.index.fulltext().getConnectorForRead(WebgraphSchema.CORE_NAME);
        }
        if (connector == null)
            throw new ServletException("no core");

        // add default queryfield parameter according to local ranking config (or defaultfield)
        if (ranking != null) { // ranking normally never null
            final String qf = ranking.getQueryFields();
            if (qf.length() > 4) { // make sure qf has content (else use df)
                addParam(DisMaxParams.QF, qf, mmsp.getMap()); // add QF that we set to be best suited for our index
                // TODO: if every peer applies a decent QF itself, this can be reverted to getMap().put()
            } else {
                mmsp.getMap().put(CommonParams.DF, new String[] { CollectionSchema.text_t.getSolrFieldName() });
            }
        } else {
            mmsp.getMap().put(CommonParams.DF, new String[] { CollectionSchema.text_t.getSolrFieldName() });
        }

        // do the solr request, generate facets if we use a special YaCy format
        final SolrQueryResponse rsp;
        if (connector instanceof EmbeddedSolrConnector) {
            req = ((EmbeddedSolrConnector) connector).request(mmsp);
            rsp = ((EmbeddedSolrConnector) connector).query(req);

            // prepare response
            hresponse.setHeader("Cache-Control", "no-cache, no-store");
            HttpCacheHeaderUtil.checkHttpCachingVeto(rsp, hresponse, reqMethod);

            // check error
            if (rsp.getException() != null) {
                AccessTracker.addToDump(querystring, "0", new Date());
                sendError(hresponse, rsp.getException());
                return;
            }

            NamedList<?> values = rsp.getValues();
            DocList r = ((ResultContext) values.get("response")).docs;
            int numFound = r.matches();
            AccessTracker.addToDump(querystring, Integer.toString(numFound), new Date());

            // write response header
            final String contentType = responseWriter.getContentType(req, rsp);
            if (null != contentType)
                response.setContentType(contentType);

            if (Method.HEAD == reqMethod) {
                return;
            }

            // write response body
            if (responseWriter instanceof BinaryResponseWriter) {
                ((BinaryResponseWriter) responseWriter).write(response.getOutputStream(), req, rsp);
            } else {
                out = new FastWriter(new OutputStreamWriter(response.getOutputStream(), UTF8.charset));
                responseWriter.write(out, req, rsp);
                out.flush();
            }
        } else {
            // write a 'faked' response using a call to the backend
            SolrDocumentList sdl = connector.getDocumentListByQuery(mmsp.getMap().get(CommonParams.Q)[0],
                    mmsp.getMap().get(CommonParams.SORT) == null ? null
                            : mmsp.getMap().get(CommonParams.SORT)[0],
                    Integer.parseInt(mmsp.getMap().get(CommonParams.START)[0]),
                    Integer.parseInt(mmsp.getMap().get(CommonParams.ROWS)[0]),
                    mmsp.getMap().get(CommonParams.FL));
            OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream());
            EnhancedXMLResponseWriter.write(osw, req, sdl);
            osw.close();
        }
    } catch (final Throwable ex) {
        sendError(hresponse, ex);
    } finally {
        if (req != null) {
            req.close();
        }
        SolrRequestInfo.clearRequestInfo();
        if (out != null)
            try {
                out.close();
            } catch (final IOException e1) {
            }
    }
}

From source file:net.yacy.search.query.QueryParams.java

License:Open Source License

private SolrQuery solrTextQuery(final boolean getFacets, final boolean excludeintext_image) {
    if (this.cachedQuery != null) {
        this.cachedQuery.setStart(this.offset);
        if (!getFacets)
            this.cachedQuery.setFacet(false);
        return this.cachedQuery;
    }/*from  w  w  w .j a  va 2s  .com*/

    // construct query
    final SolrQuery params = getBasicParams(getFacets,
            this.queryGoal.collectionTextFilterQuery(excludeintext_image));
    int rankingProfile = this.ranking.coeff_date == RankingProfile.COEFF_MAX ? 1
            : (this.modifier.sitehash != null || this.modifier.sitehost != null) ? 2 : 0;
    params.setQuery(this.queryGoal.collectionTextQuery().toString());
    Ranking actRanking = indexSegment.fulltext().getDefaultConfiguration().getRanking(rankingProfile); // for a by-date ranking select different ranking profile

    String fq = actRanking.getFilterQuery();
    String bq = actRanking.getBoostQuery();
    String bf = actRanking.getBoostFunction();
    final String qf = actRanking.getQueryFields();
    if (!qf.isEmpty())
        params.setParam(DisMaxParams.QF, qf);
    if (this.queryGoal.getIncludeSize() > 1) {
        // add boost on combined words
        if (bq.length() > 0)
            bq += " ";
        bq += CollectionSchema.text_t.getSolrFieldName() + ":\"" + this.queryGoal.getIncludeString() + "\"^10";
    }
    if (fq.length() > 0) {
        String[] oldfq = params.getFilterQueries();
        ArrayList<String> newfq = new ArrayList<>(oldfq.length + 1);
        for (String x : oldfq)
            newfq.add(x);
        newfq.add(fq);
        params.setFilterQueries(newfq.toArray(new String[newfq.size()]));
    }
    if (bq.length() > 0)
        params.setParam(DisMaxParams.BQ, bq);
    if (bf.length() > 0)
        params.setParam("boost", bf); // a boost function extension, see http://wiki.apache.org/solr/ExtendedDisMax#bf_.28Boost_Function.2C_additive.29

    // prepare result
    ConcurrentLog.info("Protocol", "SOLR QUERY: " + params.toString());
    this.cachedQuery = params;
    return params;
}

From source file:net.yacy.search.query.QueryParams.java

License:Open Source License

private SolrQuery getBasicParams(boolean getFacets, List<String> fqs) {
    final SolrQuery params = new SolrQuery();
    params.setParam("defType", "edismax");
    params.setParam(DisMaxParams.QF, CollectionSchema.text_t.getSolrFieldName() + "^1.0");
    params.setStart(this.offset);
    params.setRows(this.itemsPerPage);
    params.setFacet(false);//from   w w w. java 2 s. c o m

    if (this.ranking.coeff_date == RankingProfile.COEFF_MAX) {
        // set a most-recent ordering
        params.setSort(new SortClause(CollectionSchema.last_modified.getSolrFieldName(), SolrQuery.ORDER.desc));
        //params.setSortField(CollectionSchema.last_modified.getSolrFieldName(), ORDER.desc); // deprecated in Solr 4.2
    }

    // add site facets
    fqs.addAll(getFacetsFilterQueries());
    if (fqs.size() > 0) {
        params.setFilterQueries(fqs.toArray(new String[fqs.size()]));
    }

    // set facet query attributes
    if (getFacets && this.facetfields.size() > 0) {
        params.setFacet(true);
        params.setFacetMinCount(1);
        params.setFacetLimit(FACETS_STANDARD_MAXCOUNT);
        params.setFacetSort(FacetParams.FACET_SORT_COUNT);
        params.setParam(FacetParams.FACET_METHOD, FacetParams.FACET_METHOD_fcs);
        for (String field : this.facetfields)
            params.addFacetField("{!ex=" + field + "}" + field); // params.addFacetField("{!ex=" + field + "}" + field);
        if (this.facetfields.contains(CollectionSchema.dates_in_content_dts.name())) {
            params.setParam("facet.range", CollectionSchema.dates_in_content_dts.name());
            @SuppressWarnings({ "static-access", "deprecation" })
            String start = TrieDateField
                    .formatExternal(new Date(System.currentTimeMillis() - 1000L * 60L * 60L * 24L * 3));
            @SuppressWarnings({ "static-access", "deprecation" })
            String end = TrieDateField
                    .formatExternal(new Date(System.currentTimeMillis() + 1000L * 60L * 60L * 24L * 3));
            params.setParam(
                    "f." + CollectionSchema.dates_in_content_dts.getSolrFieldName() + ".facet.range.start",
                    start);
            params.setParam(
                    "f." + CollectionSchema.dates_in_content_dts.getSolrFieldName() + ".facet.range.end", end);
            params.setParam(
                    "f." + CollectionSchema.dates_in_content_dts.getSolrFieldName() + ".facet.range.gap",
                    "+1DAY");
            params.setParam("f." + CollectionSchema.dates_in_content_dts.getSolrFieldName() + ".facet.sort",
                    "index");
            params.setParam("f." + CollectionSchema.dates_in_content_dts.getSolrFieldName() + ".facet.limit",
                    Integer.toString(FACETS_DATE_MAXCOUNT)); // the year constraint should cause that limitation already
        }
        //for (String k: params.getParameterNames()) {ArrayList<String> al = new ArrayList<>(); for (String s: params.getParams(k)) al.add(s); System.out.println("Parameter: " + k + "=" + al.toString());}
        //http://localhost:8090/solr/collection1/select?q=*:*&rows=0&facet=true&facet.field=dates_in_content_dts&f.dates_in_content_dts.facet.limit=730&f.dates_in_content_dts.facet.sort=index
    } else {
        params.setFacet(false);
    }
    params.setFields("*", "score"); // we need the score for post-ranking
    return params;
}

From source file:org.phenotips.termrequester.db.solr.SolrDatabaseService.java

License:Open Source License

@Override
public List<Phenotype> searchPhenotypes(String text) throws IOException {
    checkUp();/*w  ww  .j a v  a2  s .  co m*/
    try {
        SolrQuery q = new SolrQuery();
        String escaped = ClientUtils.escapeQueryChars(text);
        q.add(CommonParams.Q, escaped);
        q.add(SpellingParams.SPELLCHECK_Q, text);
        q.add(DisMaxParams.PF,
                String.format("%s^20 %s^36 %s^100 %s^30 %s^15 %s^25 %s^70 %s^20 %s^3 %s^5", Schema.NAME,
                        Schema.NAME_SPELL, Schema.NAME_EXACT, Schema.NAME_PREFIX, Schema.SYNONYM,
                        Schema.SYNONYM_SPELL, Schema.SYNONYM_EXACT, Schema.SYNONYM_PREFIX, Schema.TEXT,
                        Schema.TEXT_SPELL));
        String qstring = String.format("%s^10 %s^18 %s^5 %s^6 %s^10 %s^3 %s^1 %s^2 %s^0.5", Schema.NAME,
                Schema.NAME_SPELL, Schema.NAME_STUB, Schema.SYNONYM, Schema.SYNONYM_SPELL, Schema.SYNONYM_STUB,
                Schema.TEXT, Schema.TEXT, Schema.TEXT_SPELL, Schema.TEXT_STUB);
        qstring = addStatusFilter(qstring, Phenotype.Status.SYNONYM);
        q.add(DisMaxParams.QF, qstring);
        q.add("spellcheck", Boolean.toString(true));
        q.add(SpellingParams.SPELLCHECK_COLLATE, Boolean.toString(true));
        q.add(SpellingParams.SPELLCHECK_COUNT, "100");
        q.add(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES, "3");
        q.add("lowercaseOperators", Boolean.toString(false));
        q.add("defType", "edismax");
        QueryResponse resp = server.query(q);
        List<SolrDocument> results = resp.getResults();
        List<Phenotype> retval = new ArrayList<>(results.size());
        for (SolrDocument doc : results) {
            retval.add(mapper.fromDoc(doc));
        }
        return retval;
    } catch (SolrServerException e) {
        throw new IOException(e);
    }
}

From source file:org.phenotips.vocabulary.internal.solr.FrenchHPOTranslationTest.java

License:Open Source License

@Test
public void queriesAreExtendedToIncludeFrenchFieldsWhenLocaleIsFr() {
    SolrQuery query = new SolrQuery("seizures");
    query.set(DisMaxParams.QF, "name");
    query.set(DisMaxParams.PF, "name");
    this.component.extendQuery(query, this.vocabulary);
    Assert.assertEquals("name name_fr^60 synonym_fr^45 def_fr^12 ", query.get(DisMaxParams.PF));
    Assert.assertEquals("name name_fr^30 synonym_fr^21 def_fr^6 ", query.get(DisMaxParams.QF));
}

From source file:org.phenotips.vocabulary.internal.solr.FrenchHPOTranslationTest.java

License:Open Source License

@Test
public void queriesAreExtendedToIncludeFrenchFieldsWhenLocaleIsFrFR() {
    when(this.localizationContext.getCurrentLocale()).thenReturn(Locale.FRANCE);
    SolrQuery query = new SolrQuery("seizures");
    query.set(DisMaxParams.QF, "name");
    query.set(DisMaxParams.PF, "name");
    this.component.extendQuery(query, this.vocabulary);
    Assert.assertEquals("name name_fr^60 synonym_fr^45 def_fr^12 ", query.get(DisMaxParams.PF));
    Assert.assertEquals("name name_fr^30 synonym_fr^21 def_fr^6 ", query.get(DisMaxParams.QF));
}

From source file:org.phenotips.vocabulary.internal.solr.FrenchHPOTranslationTest.java

License:Open Source License

@Test
public void queriesAreNotModifiedWhenLocaleIsDe() {
    when(this.localizationContext.getCurrentLocale()).thenReturn(Locale.GERMAN);
    SolrQuery query = new SolrQuery("seizures");
    query.set(DisMaxParams.QF, "name");
    query.set(DisMaxParams.PF, "name");
    this.component.extendQuery(query, this.vocabulary);
    Assert.assertEquals("name", query.get(DisMaxParams.PF));
    Assert.assertEquals("name", query.get(DisMaxParams.QF));
}