Example usage for org.apache.solr.common.params CommonParams Q

List of usage examples for org.apache.solr.common.params CommonParams Q

Introduction

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

Prototype

String Q

To view the source code for org.apache.solr.common.params CommonParams Q.

Click Source Link

Document

query string

Usage

From source file:com.searchbox.TaggerComponent.java

License:Apache License

@Override
public void process(ResponseBuilder rb) throws IOException {
    LOGGER.trace(("Hit process"));
    numRequests++;//ww  w.  j a  v a  2s .c  o  m

    SolrParams params = rb.req.getParams();
    String[] fields = params
            .getParams(TaggerComponentParams.QUERY_FIELDS + "." + TaggerComponentParams.QUERY_FIELD);

    if (fields == null) {
        fields = gfields;
    } else {
        for (String field : fields) {
            LOGGER.info("Using overrode fields:" + field);
        }
    }
    boolean build = params.getBool(TaggerComponentParams.PRODUCT_NAME + "." + TaggerComponentParams.BUILD,
            false);
    SolrIndexSearcher searcher = rb.req.getSearcher();
    if (build) {
        long lstartTime = System.currentTimeMillis();
        buildAndWrite(searcher, fields);
        totalBuildTime += System.currentTimeMillis() - lstartTime;
        lastbuildDate = new Date().toString();
    }

    if (dfb == null) {
        LOGGER.error("Model for SBtagger not created, create using sbtagger.build=true");
        return;
    }
    String commonparamsQuery = params.get(CommonParams.Q);
    String query = params.get(TaggerComponentParams.PRODUCT_NAME + "." + TaggerComponentParams.QUERY);

    int lcount = params.getInt(TaggerComponentParams.PRODUCT_NAME + "." + TaggerComponentParams.COUNT,
            TaggerComponentParams.COUNT_DEFAULT);

    LOGGER.debug("Tagger Query:\t" + query);
    LOGGER.debug("Common params Query:\t" + commonparamsQuery);

    long lstartTime = System.currentTimeMillis();

    NamedList response = null;
    if (commonparamsQuery != null) {
        // do for documents
        response = doDocuments(rb, params, searcher, lcount);
    } else if (query != null) {
        // do for tag text
        response = doText(query, lcount);
    } else {
        LOGGER.warn("No query in q or sbtagger.q, returning..maybe was just used for  building index?");
        numErrors++;
        return;
    }

    rb.rsp.add(TaggerComponentParams.PRODUCT_NAME, response);
    totalRequestsTime += System.currentTimeMillis() - lstartTime;
}

From source file:com.tamingtext.qa.PassageRankingComponent.java

License:Apache License

@Override
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    if (!params.getBool(COMPONENT_NAME, false)) {
        return;// w ww. j  a v a2  s.c  om
    }
    Query origQuery = rb.getQuery();
    //TODO: longer term, we don't have to be a span query, we could re-analyze the document
    if (origQuery != null) {
        if (origQuery instanceof SpanNearQuery == false) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
                    "Illegal query type.  The incoming query must be a Lucene SpanNearQuery and it was a "
                            + origQuery.getClass().getName());
        }
        SpanNearQuery sQuery = (SpanNearQuery) origQuery;
        SolrIndexSearcher searcher = rb.req.getSearcher();
        IndexReader reader = searcher.getIndexReader();
        Spans spans = sQuery.getSpans(reader);
        //Assumes the query is a SpanQuery
        //Build up the query term weight map and the bi-gram
        Map<String, Float> termWeights = new HashMap<String, Float>();
        Map<String, Float> bigramWeights = new HashMap<String, Float>();
        createWeights(params.get(CommonParams.Q), sQuery, termWeights, bigramWeights, reader);
        float adjWeight = params.getFloat(ADJACENT_WEIGHT, DEFAULT_ADJACENT_WEIGHT);
        float secondAdjWeight = params.getFloat(SECOND_ADJ_WEIGHT, DEFAULT_SECOND_ADJACENT_WEIGHT);
        float bigramWeight = params.getFloat(BIGRAM_WEIGHT, DEFAULT_BIGRAM_WEIGHT);
        //get the passages
        int primaryWindowSize = params.getInt(QAParams.PRIMARY_WINDOW_SIZE, DEFAULT_PRIMARY_WINDOW_SIZE);
        int adjacentWindowSize = params.getInt(QAParams.ADJACENT_WINDOW_SIZE, DEFAULT_ADJACENT_WINDOW_SIZE);
        int secondaryWindowSize = params.getInt(QAParams.SECONDARY_WINDOW_SIZE, DEFAULT_SECONDARY_WINDOW_SIZE);
        WindowBuildingTVM tvm = new WindowBuildingTVM(primaryWindowSize, adjacentWindowSize,
                secondaryWindowSize);
        PassagePriorityQueue rankedPassages = new PassagePriorityQueue();
        //intersect w/ doclist
        DocList docList = rb.getResults().docList;
        while (spans.next() == true) {
            //build up the window
            if (docList.exists(spans.doc())) {
                tvm.spanStart = spans.start();
                tvm.spanEnd = spans.end();
                reader.getTermFreqVector(spans.doc(), sQuery.getField(), tvm);
                //The entries map contains the window, do some ranking of it
                if (tvm.passage.terms.isEmpty() == false) {
                    log.debug("Candidate: Doc: {} Start: {} End: {} ",
                            new Object[] { spans.doc(), spans.start(), spans.end() });
                }
                tvm.passage.lDocId = spans.doc();
                tvm.passage.field = sQuery.getField();
                //score this window
                try {
                    addPassage(tvm.passage, rankedPassages, termWeights, bigramWeights, adjWeight,
                            secondAdjWeight, bigramWeight);
                } catch (CloneNotSupportedException e) {
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
                            "Internal error cloning Passage", e);
                }
                //clear out the entries for the next round
                tvm.passage.clear();
            }
        }
        NamedList qaResp = new NamedList();
        rb.rsp.add("qaResponse", qaResp);
        int rows = params.getInt(QA_ROWS, 5);

        SchemaField uniqField = rb.req.getSchema().getUniqueKeyField();
        if (rankedPassages.size() > 0) {
            int size = Math.min(rows, rankedPassages.size());
            Set<String> fields = new HashSet<String>();
            for (int i = size - 1; i >= 0; i--) {
                Passage passage = rankedPassages.pop();
                if (passage != null) {
                    NamedList passNL = new NamedList();
                    qaResp.add(("answer"), passNL);
                    String idName;
                    String idValue;
                    if (uniqField != null) {
                        idName = uniqField.getName();
                        fields.add(idName);
                        fields.add(passage.field);//prefetch this now, so that it is cached
                        idValue = searcher.doc(passage.lDocId, fields).get(idName);
                    } else {
                        idName = "luceneDocId";
                        idValue = String.valueOf(passage.lDocId);
                    }
                    passNL.add(idName, idValue);
                    passNL.add("field", passage.field);
                    //get the window
                    String fldValue = searcher.doc(passage.lDocId, fields).get(passage.field);
                    if (fldValue != null) {
                        //get the window of words to display, we don't use the passage window, as that is based on the term vector
                        int start = passage.terms.first().start;//use the offsets
                        int end = passage.terms.last().end;
                        if (start >= 0 && start < fldValue.length() && end >= 0 && end < fldValue.length()) {
                            passNL.add("window",
                                    fldValue.substring(start, end + passage.terms.last().term.length()));
                        } else {
                            log.debug("Passage does not have correct offset information");
                            passNL.add("window", fldValue);//we don't have offsets, or they are incorrect, return the whole field value
                        }
                    }
                } else {
                    break;
                }
            }
        }
    }

}

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

License:Mozilla Public License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse res)
        throws Exception, ParseException, InstantiationException, IllegalAccessException {
    NamedList<Object> params = req.getParams().toNamedList();

    String assetId = (String) params.get(PARAM_ASSET_ID);
    if (assetId == null || assetId.trim().length() < 0) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Asset Id required to retrieve tags.");
    }//from  w w  w.  j ava 2  s  .  c om

    boolean allTags = (params.get(PARAM_ALL_TAGS) != null
            && Boolean.parseBoolean((String) params.get(PARAM_ALL_TAGS)));

    StringBuffer q = new StringBuffer("+").append(NYPhilSolrConstants.NPT_ASSET_ID_ESC).append(":")
            .append(QueryParser.escape(assetId));

    if (!allTags) {
        q.append(" +").append(NYPhilSolrConstants.NPT_STATUS_ESC).append(":")
                .append(NYPhilSolrConstants.STATUS_APPROVED);
    }

    String cb = (String) params.get(PARAM_CALLBACK);
    if (cb != null && cb.length() > 0) {
        params.add("json.wrf", cb);
    }

    params.add(CommonParams.HEADER_ECHO_PARAMS, "explicit");
    params.add(CommonParams.WT, "json");
    params.add("json.nl", "map");

    params.add(CommonParams.Q, q.toString());
    params.add(CommonParams.ROWS, 1000);

    req.setParams(SolrParams.toSolrParams(params));

    super.handleRequestBody(req, res);
}

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)
 *//*from   w w  w.ja va2  s .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:com.tsgrp.solr.handler.NYPhilTagAutoCompleteHandler.java

License:Mozilla Public License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse res)
        throws Exception, ParseException, InstantiationException, IllegalAccessException {
    NamedList<Object> params = req.getParams().toNamedList();

    params.add(CommonParams.ROWS, 0);//from   w  w  w . j av a2  s .  co m
    params.add(FacetParams.FACET, true);
    params.add(FacetParams.FACET_FIELD, NYPhilSolrConstants.NPT_CONTENT_FACET);
    params.add(FacetParams.FACET_MINCOUNT, 1);
    params.add(FacetParams.FACET_SORT, FacetParams.FACET_SORT_INDEX);
    params.add(CommonParams.HEADER_ECHO_PARAMS, "explicit");
    params.add(CommonParams.WT, "json");
    params.add("json.nl", "map");

    String query = (String) params.get(PARAM_VALUE);
    if (query == null || query.length() == 0) {
        query = "*";
    } else {
        query = QueryParser.escape(query.toLowerCase());
    }

    String[] queryTerms = query.split(" ");

    // wrap our query term in parentheses to allow searching on terms separated by whitespace
    StringBuffer q = new StringBuffer();

    // for each query term, require the term with a trailing wildcard match
    for (String queryTerm : queryTerms) {

        // remove all non-alphanumeric chars from the query term
        queryTerm = QUERY_TERM_REGEX.matcher(queryTerm.toLowerCase()).replaceAll("");
        q.append("+").append(NYPhilSolrConstants.NPT_CONTENT_ESC).append(":")
                .append(QueryParser.escape(queryTerm)).append("* ");
    }
    q.append("+").append(NYPhilSolrConstants.NPT_STATUS_ESC).append(":")
            .append(NYPhilSolrConstants.STATUS_APPROVED);

    params.add(CommonParams.Q, q.toString());

    if (logger.isDebugEnabled()) {
        logger.debug("Autocomplete Query: " + q.toString());
    }

    String cb = (String) params.get(PARAM_CALLBACK);
    if (cb != null && cb.length() > 0) {
        params.add("json.wrf", cb);
    }

    req.setParams(SolrParams.toSolrParams(params));

    super.handleRequestBody(req, res);
}

From source file:de.qaware.chronix.solr.query.analysis.AnalysisHandler.java

License:Apache License

private Map<String, List<SolrDocument>> findDocuments(SolrQueryRequest req,
        Function<SolrDocument, String> collectionKey) throws IOException {
    String query = req.getParams().get(CommonParams.Q);
    Set<String> fields = getFields(req.getParams().get(CommonParams.FL));

    //query and collect all documents
    DocList result = docListProvider.doSimpleQuery(query, req, 0, Integer.MAX_VALUE);
    SolrDocumentList docs = docListProvider.docListToSolrDocumentList(result, req.getSearcher(), fields, null);
    return AnalysisDocumentBuilder.collect(docs, collectionKey);
}

From source file:de.qaware.chronix.solr.query.ChronixQueryHandler.java

License:Apache License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    ModifiableSolrParams modifiableSolrParams = new ModifiableSolrParams(req.getParams());

    String originQuery = modifiableSolrParams.get(CommonParams.Q);

    long[] startAndEnd = dateRangeParser.getNumericQueryTerms(originQuery);
    long queryStart = or(startAndEnd[0], -1, 0);
    long queryEnd = or(startAndEnd[1], -1, Long.MAX_VALUE);

    modifiableSolrParams.set(ChronixQueryParams.QUERY_START_LONG, String.valueOf(queryStart));
    modifiableSolrParams.set(ChronixQueryParams.QUERY_END_LONG, String.valueOf(queryEnd));

    String query = dateRangeParser.replaceRangeQueryTerms(originQuery);

    modifiableSolrParams.set(CommonParams.Q, query);

    //Set the min required fields if the user define a sub set of fields
    modifiableSolrParams.set(CommonParams.FL, minRequiredFields(modifiableSolrParams.get(CommonParams.FL)));
    //Set the updated query
    req.setParams(modifiableSolrParams);

    //check the filter queries
    String[] filterQueries = modifiableSolrParams.getParams(CommonParams.FQ);

    //if we have an isAggregation
    if (contains(filterQueries, ChronixQueryParams.AGGREGATION_PARAM)
            || contains(filterQueries, ChronixQueryParams.ANALYSIS_PARAM)) {
        analysisHandler.handleRequestBody(req, rsp);

    } else {/*from   ww  w  .  j  a  v  a  2 s .com*/
        //let the default search handler do its work
        searchHandler.handleRequestBody(req, rsp);
    }

    //add the converted start and end to the response
    rsp.getResponseHeader().add(ChronixQueryParams.QUERY_START_LONG, queryStart);
    rsp.getResponseHeader().add(ChronixQueryParams.QUERY_END_LONG, queryEnd);
}

From source file:de.uni_tuebingen.ub.ixTheo.handler.component.FacetPrefixSortComponent.java

License:Apache License

/**
 * Actually run the query// ww  w .  j  a  v a 2s . c  o  m
 */
@Override
public void process(ResponseBuilder rb) throws IOException {
    if (rb.doFacets) {
        final ModifiableSolrParams params = new ModifiableSolrParams();
        final SolrParams origParams = rb.req.getParams();
        final Iterator<String> iter = origParams.getParameterNamesIterator();
        setCollator(origParams.get("lang"));
        while (iter.hasNext()) {
            final String paramName = iter.next();
            // Deduplicate the list with LinkedHashSet, but _only_ for facet
            // params.
            if (!paramName.startsWith(FacetParams.FACET)) {
                params.add(paramName, origParams.getParams(paramName));
                continue;
            }
            final HashSet<String> deDupe = new LinkedHashSet<>(Arrays.asList(origParams.getParams(paramName)));
            params.add(paramName, deDupe.toArray(new String[deDupe.size()]));
        }

        final SimplePrefixSortFacets facets = new SimplePrefixSortFacets(rb.req, rb.getResults().docSet, params,
                rb);
        final NamedList<Object> counts = org.apache.solr.handler.component.FacetComponent
                .getFacetCounts(facets);

        final String[] pivots = params.getParams(FacetParams.FACET_PIVOT);
        if (pivots != null && pivots.length > 0) {
            PivotFacetProcessor pivotProcessor = new PivotFacetProcessor(rb.req, rb.getResults().docSet, params,
                    rb);
            SimpleOrderedMap<List<NamedList<Object>>> v = pivotProcessor.process(pivots);
            if (v != null) {
                counts.add(PIVOT_KEY, v);
            }
        }

        // Check whether we have to reorder out results
        // according to prefix

        final String sort = params.get(FacetParams.FACET_SORT);
        if (FacetPrefixSortParams.FACET_SORT_PREFIX.equals(sort)) {

            // Determine a score relative to the original query

            // Determine the query and make it compatible with our metric
            // class
            // by splitting the single terms
            String[] queryTerms = params.getParams(CommonParams.Q);
            final Collection<String> queryTermsCollection = new ArrayList<>();
            for (String s : queryTerms) {
                // Split at whitespace except we have a quoted term
                Matcher matcher = WHITE_SPACES_WITH_QUOTES_SPLITTING_PATTERN.matcher(s);
                while (matcher.find()) {
                    queryTermsCollection.add(matcher.group().replaceAll("^\"|\"$", ""));
                }
            }

            // In some contexts, i.e. in KWC that are derived from ordinary
            // keywords or if
            // wildcards occur, also add all the query terms as a single
            // phrase term
            // with stripped wildcards
            StringBuilder sb = new StringBuilder();
            for (String s : queryTermsCollection) {
                s = s.replace("*", "");
                sb.append(s);
                sb.append(" ");
            }

            queryTermsCollection.add(sb.toString().trim());

            final ArrayList<String> queryList = new ArrayList<>(queryTermsCollection);
            final String facetfield = params.get(FacetParams.FACET_FIELD);

            // Get the current facet entry and make it compatible with our
            // metric class
            // "facet_fields" itself contains a NamedList with the
            // facet.field as key

            final NamedList<Object> facetFieldsNamedList = (NamedList<Object>) counts.get("facet_fields");
            final NamedList<Object> facetFields = (NamedList<Object>) facetFieldsNamedList.get(facetfield);

            final List<Entry<Entry<String, Object>, Double>> facetPrefixListScored = new ArrayList<>();
            for (final Entry<String, Object> entry : facetFields) {
                final String facetTerms = entry.getKey();

                // Split up each KWC and calculate the scoring

                ArrayList<String> facetList = new ArrayList<>(
                        Arrays.asList(facetTerms.split("(?<!" + Pattern.quote("\\") + ")/")));

                // For usability reasons sort the result facets according to
                // the order of the search
                facetList = KeywordSort.sortToReferenceChain(queryList, facetList);

                final double score = KeywordChainMetric.calculateSimilarityScore(queryList, facetList);

                // Collect the result in a sorted list and throw away
                // garbage
                if (score > 0) {
                    String facetTermsSorted = StringUtils.join(facetList, "/");
                    Map.Entry<String, Object> sortedEntry = new AbstractMap.SimpleEntry<>(facetTermsSorted,
                            entry.getValue());
                    facetPrefixListScored.add(new AbstractMap.SimpleEntry<>(sortedEntry, score));
                }
            }

            Collections.sort(facetPrefixListScored, ENTRY_COMPARATOR);

            // Extract all the values wrap it back to NamedList again and
            // replace in the original structure

            facetFieldsNamedList.clear();
            NamedList<Object> facetNamedListSorted = new NamedList<>();

            // We had to disable all limits and offsets sort according
            // Handle this accordingly now

            int offset = (params.getInt(FacetParams.FACET_OFFSET) != null)
                    ? params.getInt(FacetParams.FACET_OFFSET)
                    : 0;
            int limit = (params.getInt(FacetParams.FACET_LIMIT) != null)
                    ? params.getInt(FacetParams.FACET_LIMIT)
                    : 100;

            // Strip uneeded elements
            int s = facetPrefixListScored.size();
            int off = (offset < s) ? offset : 0;
            limit = (limit < 0) ? s : limit; // Handle a negative limit
            // param, i.e. unlimited results
            int lim = (offset + limit <= s) ? (offset + limit) : s;

            final List<Entry<Entry<String, Object>, Double>> facetPrefixListScoredTruncated = facetPrefixListScored
                    .subList(off, lim);

            for (Entry<Entry<String, Object>, Double> e : facetPrefixListScoredTruncated) {
                facetNamedListSorted.add(e.getKey().getKey(), e.getKey().getValue());
            }

            facetFieldsNamedList.add(facetfield, facetNamedListSorted);
            NamedList<Object> countList = new NamedList<>();
            countList.add("count", facetPrefixListScored.size());
            facetFieldsNamedList.add(facetfield + "-count", countList);

            counts.remove("facet_fields");
            counts.add("facet_fields", facetFieldsNamedList);
        }

        rb.rsp.add("facet_counts", counts);
    }
}

From source file:edu.toronto.cs.cidb.solr.SolrScriptService.java

License:Open Source License

/**
 * Convert a Lucene query string into a map of Solr parameters. More specifically, places the input query under the
 * "q" parameter, and adds parameters for requesting a spellcheck result.
 *
 * @param query the lucene query string to use
 * @param sort the sort criteria ("fiel_name order')
 * @param rows the number of items to return, or -1 to use the default number of results
 * @param start the number of items to skip, i.e. the index of the first hit to return, 0-based
 * @return a map of Solr query parameter ready to be used for constructing a {@link MapSolrParams} object
 *///www . jav  a  2  s .  c o m
private Map<String, String> getSolrQuery(String query, String sort, int rows, int start) {
    Map<String, String> result = new HashMap<String, String>();
    result.put(CommonParams.START, start + "");
    if (rows > 0) {
        result.put(CommonParams.ROWS, rows + "");
    }
    result.put(CommonParams.Q, query);
    if (!StringUtils.isBlank(sort)) {
        result.put(CommonParams.SORT, sort);
    }
    result.put("spellcheck", Boolean.toString(true));
    result.put("spellcheck.collate", Boolean.toString(true));
    result.put("spellcheck.onlyMorePopular", Boolean.toString(true));
    return result;
}

From source file:edu.toronto.cs.phenotips.solr.AbstractSolrScriptService.java

License:Open Source License

/**
 * Perform a search, falling back on the suggested spellchecked query if the original query fails to return any
 * results./*from   ww  w .j a v a 2  s  . co  m*/
 * 
 * @param params the Solr parameters to use, should contain at least a value for the "q" parameter; use
 *            {@link #getSolrQuery(String, int, int)} to get the proper parameter expected by this method
 * @return the list of matching documents, empty if there are no matching terms
 */
private SolrDocumentList search(MapSolrParams params) {
    try {
        NamedList<Object> newParams = params.toNamedList();
        if (newParams.get(CommonParams.FL) == null) {
            newParams.add(CommonParams.FL, "* score");
        }
        QueryResponse response = this.server.query(MapSolrParams.toSolrParams(newParams));
        SolrDocumentList results = response.getResults();
        if (response.getSpellCheckResponse() != null
                && !response.getSpellCheckResponse().isCorrectlySpelled()) {
            String suggestedQuery = response.getSpellCheckResponse().getCollatedResult();
            if (StringUtils.isEmpty(suggestedQuery)) {
                return results;
            }
            Pattern p = Pattern.compile("(\\w++):(\\w++)\\*$", Pattern.CASE_INSENSITIVE);
            Matcher originalStub = p.matcher((String) newParams.get(CommonParams.Q));
            newParams.remove(CommonParams.Q);
            Matcher newStub = p.matcher(suggestedQuery);
            if (originalStub.find() && newStub.find()) {
                suggestedQuery += ' ' + originalStub.group() + "^1.5 " + originalStub.group(2) + "^1.5";
                String boostQuery = (String) newParams.get(DisMaxParams.BQ);
                if (boostQuery != null) {
                    boostQuery += ' ' + boostQuery.replace(originalStub.group(2), newStub.group(2));
                    newParams.remove(DisMaxParams.BQ);
                    newParams.add(DisMaxParams.BQ, boostQuery);
                }
            }
            newParams.add(CommonParams.Q, suggestedQuery);
            SolrDocumentList spellcheckResults = this.server.query(MapSolrParams.toSolrParams(newParams))
                    .getResults();
            if (results.getMaxScore() < spellcheckResults.getMaxScore()) {
                results = spellcheckResults;
            }
        }
        return results;
    } catch (SolrServerException ex) {
        this.logger.error("Failed to search: {}", ex.getMessage(), ex);
    }
    return null;
}