Example usage for org.apache.solr.client.solrj SolrQuery setFacet

List of usage examples for org.apache.solr.client.solrj SolrQuery setFacet

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery setFacet.

Prototype

public SolrQuery setFacet(boolean b) 

Source Link

Document

enable/disable faceting.

Usage

From source file:fr.hoteia.qalingo.core.solr.service.impl.StoreSolrServiceImpl.java

License:Apache License

/**
  * //from   w ww . java  2 s.c  o m
  */
public StoreResponseBean searchStore() throws SolrServerException, IOException {

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.setQuery("*");
    solrQuery.setFacet(true);
    solrQuery.setFacetMinCount(1);
    solrQuery.setFacetLimit(8);
    solrQuery.addFacetField("businessname");

    SolrRequest request = new QueryRequest(solrQuery, METHOD.POST);
    request.setPath(getRequestPath());
    QueryResponse response = new QueryResponse(solrServer.request(request), solrServer);
    List<StoreSolr> storeSolrList = response.getBeans(StoreSolr.class);
    List<FacetField> storeSolrFacetFieldList = response.getFacetFields();

    StoreResponseBean storeResponseBean = new StoreResponseBean();
    storeResponseBean.setStoreSolrList(storeSolrList);
    storeResponseBean.setStoreSolrFacetFieldList(storeSolrFacetFieldList);

    return storeResponseBean;
}

From source file:fr.paris.lutece.plugins.search.solr.business.SolrSearchEngine.java

License:Open Source License

/**
 * Return the result with facets. Does NOT support authentification yet.
 * @param strQuery the query//from   w  ww.  j  a v a  2 s.  c  om
 * @param facetQueries The selected facets
 * @param sortName The facet name to sort by
 * @param sortOrder "asc" or "desc"
 * @param nLimit Maximal number of results.
 * @return the result with facets
 */
public SolrFacetedResult getFacetedSearchResults(String strQuery, String[] facetQueries, String sortName,
        String sortOrder, int nLimit, int nCurrentPageIndex, int nItemsPerPage, Boolean bSpellCheck) {
    SolrFacetedResult facetedResult = new SolrFacetedResult();

    SolrClient solrServer = SolrServerService.getInstance().getSolrServer();
    List<SolrSearchResult> results = new ArrayList<SolrSearchResult>();
    Hashtable<Field, List<String>> myValuesList = new Hashtable<Field, List<String>>();

    if (solrServer != null) {
        SolrQuery query = new SolrQuery(strQuery);
        query.setHighlight(true);
        query.setHighlightSimplePre(SOLR_HIGHLIGHT_PRE);
        query.setHighlightSimplePost(SOLR_HIGHLIGHT_POST);
        query.setHighlightSnippets(SOLR_HIGHLIGHT_SNIPPETS);
        query.setHighlightFragsize(SOLR_HIGHLIGHT_FRAGSIZE);
        query.setFacet(true);
        query.setFacetLimit(SOLR_FACET_LIMIT);
        //            query.setFacetMinCount( 1 );

        for (Field field : SolrFieldManager.getFacetList().values()) {
            //Add facet Field
            if (field.getEnableFacet()) {
                if (field.getName().equalsIgnoreCase("date")
                        || field.getName().toLowerCase().endsWith("_date")) {
                    query.setParam("facet.date", field.getName());
                    query.setParam("facet.date.start", SOLR_FACET_DATE_START);
                    query.setParam("facet.date.gap", SOLR_FACET_DATE_GAP);
                    query.setParam("facet.date.end", SOLR_FACET_DATE_END);
                    query.setParam("facet.date.mincount", "0");
                } else {
                    query.addFacetField(field.getSolrName());
                    query.setParam("f." + field.getSolrName() + ".facet.mincount",
                            String.valueOf(field.getFacetMincount()));
                }
                myValuesList.put(field, new ArrayList<String>());
            }
        }

        //Facet intersection
        List<String> treeParam = new ArrayList<String>();

        for (FacetIntersection intersect : SolrFieldManager.getIntersectionlist()) {
            treeParam.add(intersect.getField1().getSolrName() + "," + intersect.getField2().getSolrName());
        }

        //(String []) al.toArray (new String [0]);
        query.setParam("facet.tree", (String[]) treeParam.toArray(new String[0]));
        query.setParam("spellcheck", bSpellCheck);

        //sort order
        if ((sortName != null) && !"".equals(sortName)) {
            if (sortOrder.equals("asc")) {
                query.setSort(sortName, ORDER.asc);
            } else {
                query.setSort(sortName, ORDER.desc);
            }
        } else {
            for (Field field : SolrFieldManager.getSortList()) {
                if (field.getDefaultSort()) {
                    query.setSort(field.getName(), ORDER.desc);
                }
            }
        }

        //Treat HttpRequest
        //FacetQuery
        if (facetQueries != null) {
            for (String strFacetQuery : facetQueries) {
                //                    if ( strFacetQuery.contains( DATE_COLON ) )
                //                    {
                //                        query.addFilterQuery( strFacetQuery );
                //                    }
                //                    else
                //                    {
                String myValues[] = strFacetQuery.split(":", 2);
                if (myValues != null && myValues.length == 2) {
                    myValuesList = getFieldArrange(myValues, myValuesList);
                }
                //strFacetQueryWithColon = strFacetQuery.replaceFirst( SolrConstants.CONSTANT_COLON, COLON_QUOTE );
                //strFacetQueryWithColon += SolrConstants.CONSTANT_QUOTE;
                //                        query.addFilterQuery( strFacetQuery );
                //                    }
            }

            for (Field tmpFieldValue : myValuesList.keySet()) {
                List<String> strValues = myValuesList.get(tmpFieldValue);
                String strFacetString = "";
                if (strValues.size() > 0) {
                    strFacetString = extractQuery(strValues, tmpFieldValue.getOperator());
                    if (tmpFieldValue.getName().equalsIgnoreCase("date")
                            || tmpFieldValue.getName().toLowerCase().endsWith("_date")) {
                        strFacetString = strFacetString.replaceAll("\"", "");
                    }
                    query.addFilterQuery(tmpFieldValue.getName() + ":" + strFacetString);
                }
            }
        }

        try {

            // count query
            query.setRows(0);
            QueryResponse response = solrServer.query(query);

            int nResults = (int) response.getResults().getNumFound();
            facetedResult.setCount(nResults > nLimit ? nLimit : nResults);

            query.setStart((nCurrentPageIndex - 1) * nItemsPerPage);
            query.setRows(nItemsPerPage > nLimit ? nLimit : nItemsPerPage);

            query.setParam("defType", DEF_TYPE);
            String strWeightValue = generateQueryWeightValue();
            query.setParam("qf", strWeightValue);

            response = solrServer.query(query);

            //HighLight
            Map<String, Map<String, List<String>>> highlightsMap = response.getHighlighting();
            SolrHighlights highlights = null;

            if (highlightsMap != null) {
                highlights = new SolrHighlights(highlightsMap);
            }

            //resultList
            List<SolrItem> itemList = response.getBeans(SolrItem.class);
            results = SolrUtil.transformSolrItemsToSolrSearchResults(itemList, highlights);

            //set the spellcheckresult
            facetedResult.setSolrSpellCheckResponse(response.getSpellCheckResponse());

            //Date facet
            if ((response.getFacetDates() != null) && !response.getFacetDates().isEmpty()) {
                facetedResult.setFacetDateList(response.getFacetDates());
            }

            //FacetField
            facetedResult.setFacetFields(response.getFacetFields());

            //Facet intersection (facet tree)
            NamedList<Object> resp = (NamedList<Object>) response.getResponse().get("facet_counts");

            if (resp != null) {
                NamedList<NamedList<NamedList<Integer>>> trees = (NamedList<NamedList<NamedList<Integer>>>) resp
                        .get("trees");
                Map<String, ArrayList<FacetField>> treesResult = new HashMap<String, ArrayList<FacetField>>();

                if (trees != null) {
                    for (Entry<String, NamedList<NamedList<Integer>>> selectedFacet : trees) { //Selected Facet (ex : type,categorie )
                                                                                               //System.out.println(selectedFacet.getKey());

                        ArrayList<FacetField> facetFields = new ArrayList<FacetField>(
                                selectedFacet.getValue().size());

                        for (Entry<String, NamedList<Integer>> facetField : selectedFacet.getValue()) {
                            FacetField ff = new FacetField(facetField.getKey());

                            //System.out.println("\t" + facetField.getKey());
                            for (Entry<String, Integer> value : facetField.getValue()) { // Second Level
                                ff.add(value.getKey(), value.getValue());

                                //System.out.println("\t\t" + value.getKey() + " : " + value.getValue());
                            }

                            facetFields.add(ff);
                        }

                        treesResult.put(selectedFacet.getKey(), facetFields);
                    }
                }

                facetedResult.setFacetIntersection(treesResult);
            }
        } catch (SolrServerException e) {
            AppLogService.error(e.getMessage(), e);
        } catch (IOException e) {
            AppLogService.error(e.getMessage(), e);
        }
    } else {
        facetedResult.setFacetFields(new ArrayList<FacetField>());
    }

    facetedResult.setSolrSearchResults(results);

    return facetedResult;
}

From source file:io.logspace.hq.core.solr.agent.SolrAgentService.java

License:Open Source License

@Override
public Set<String> getEventPropertyNames(String... globalAgentIds) {
    SolrQuery solrQuery = new SolrQuery(ALL_DOCS_QUERY);
    solrQuery.setRows(0);/*w w  w  . ja  v  a  2 s. c o  m*/

    StringBuilder globalAgentIdFilterQuery = new StringBuilder();
    globalAgentIdFilterQuery.append(FIELD_GLOBAL_AGENT_ID);
    globalAgentIdFilterQuery.append(":(");
    for (String eachGlobalAgentId : globalAgentIds) {
        globalAgentIdFilterQuery.append(escapeSolr(eachGlobalAgentId));
        globalAgentIdFilterQuery.append(" OR ");
    }
    globalAgentIdFilterQuery.setLength(globalAgentIdFilterQuery.length() - 4);
    globalAgentIdFilterQuery.append(')');
    solrQuery.addFilterQuery(globalAgentIdFilterQuery.toString());

    solrQuery.setFacet(true);
    solrQuery.setFacetMinCount(1);
    solrQuery.addFacetField(FIELD_PROPERTY_ID);

    try {
        QueryResponse response = this.solrClient.query(solrQuery);

        Set<String> result = new TreeSet<>();
        for (Count eachCount : response.getFacetField(FIELD_PROPERTY_ID).getValues()) {
            result.add(eachCount.getName());
        }

        return result;
    } catch (SolrException | SolrServerException | IOException e) {
        throw new DataRetrievalException("Failed to retrieve event property names", e);
    }
}

From source file:io.vertigo.dynamo.plugins.search.solr.SolrStatement.java

License:Apache License

private static void appendFacetDefinition(final FacetedQueryDefinition queryDefinition,
        final SolrQuery solrQuery, final IndexFieldNameResolver indexFieldNameResolver) {
    Assertion.checkNotNull(solrQuery);//ww  w.  j ava 2s  .c  om
    //---------------------------------------------------------------------
    //Activation des facettes
    final boolean hasFacet = !queryDefinition.getFacetDefinitions().isEmpty();
    solrQuery.setFacet(hasFacet);

    for (final FacetDefinition facetDefinition : queryDefinition.getFacetDefinitions()) {
        //Rcupration des noms des champs correspondant aux facettes.
        final DtField dtField = facetDefinition.getDtField();
        if (facetDefinition.isRangeFacet()) {
            //facette par range
            for (final FacetValue facetRange : facetDefinition.getFacetRanges()) {
                solrQuery.addFacetQuery(translateToSolr(facetRange.getListFilter(), indexFieldNameResolver));
            }
        } else {
            //facette par field
            solrQuery.addFacetField(indexFieldNameResolver.obtainIndexFieldName(dtField));
        }
    }
}

From source file:kbSRU.kbSRU.java

License:Open Source License

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType(XML_RESPONSE_HEADER); // Talkback happens in XML form.
    response.setCharacterEncoding("UTF-8"); // Unicode++
    request.setCharacterEncoding("UTF-8");

    PrintWriter out = null; // The talkback buffer.

    // handle startrecord 
    Integer startRecord = 0;/*from   w w  w.  j  a v  a2  s.c  om*/

    if (!(request.getParameter("startRecord") == null)) {
        try {
            startRecord = Integer.parseInt(request.getParameter("startRecord")) - 1;
        } catch (NumberFormatException e) {
            startRecord = 0;
        }
    }

    // maximumrecords
    Integer maximumRecords = Integer.parseInt(this.config.getProperty("default_maximumRecords"));
    if (!(request.getParameter("maximumRecords") == null)) {
        maximumRecords = Integer.parseInt(request.getParameter("maximumRecords"));
    }

    // operation 
    String operation = request.getParameter("operation");

    // x_collection
    String x_collection = request.getParameter("x-collection");
    if (x_collection == null)
        x_collection = this.config.getProperty("default_x_collection");
    if (x_collection == null)
        operation = null;

    // sortkeys
    String sortKeys = request.getParameter("sortKeys");

    // sortorder
    String sortOrder = request.getParameter("sortOrder");

    // recordschema 
    String recordSchema = request.getParameter("recordSchema");
    if (recordSchema == null)
        recordSchema = "dc";

    if (recordSchema.equalsIgnoreCase("dcx")) {
        recordSchema = "dcx";
    }

    if (recordSchema.equalsIgnoreCase("solr")) {
        recordSchema = "solr";
    }

    // query request 
    String query = request.getParameter("query");
    String q = request.getParameter("q");

    // who is requestor ?
    String remote_ip = request.getHeader("X-FORWARDED-FOR");

    if (remote_ip == null) {
        remote_ip = request.getRemoteAddr().trim();
    } else {
        remote_ip = request.getHeader("X-FORWARDED-FOR");
    }

    // handle debug 
    Boolean debug = Boolean.parseBoolean(request.getParameter("debug"));
    if (!debug) {
        out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
    }

    // handle query
    if ((query == null) && (q != null)) {
        query = q;
    } else {
        if ((query != null) && (q == null)) {
            q = query;
        } else {
            operation = null;
        }
    }

    // handle operation
    if (operation == null) {
        if (query != null) {
            operation = "searchRetrieve";
        } else {
            operation = "explain";
        }
    }

    //  searchRetrieve 
    if (operation.equalsIgnoreCase("searchRetrieve")) {
        if (query == null) {
            operation = "explain";
            log.debug(operation + ":" + query);
        }
    }

    // start talking back.
    String[] sq = { "" };
    String solrquery = "";

    // facet

    String facet = null;
    List<FacetField> fct = null;

    if (request.getParameter("facet") != null) {
        facet = request.getParameter("facet");
        log.debug("facet : " + facet);
    }

    if (operation == null) {
        operation = "searchretrieve";
    } else { // explain response
        if (operation.equalsIgnoreCase("explain")) {
            log.debug("operation = explain");
            out.write("<srw:explainResponse xmlns:srw=\"http://www.loc.gov/zing/srw/\">");
            out.write("</srw:explainResponse>");
        } else { // DEBUG routine
            operation = "searchretrieve";

            String triplequery = null;

            if (query.matches(".*?\\[.+?\\].*?")) { // New symantic syntax
                triplequery = symantic_query(query);
                query = query.split("\\[")[0] + " " + triplequery;
                log.fatal(triplequery);

                solrquery = CQLtoLucene.translate(query, log, config);
            } else {
                solrquery = CQLtoLucene.translate(query, log, config);
            }
            log.debug(solrquery);

            if (debug == true) {
                response.setContentType(HTML_RESPONSE_HEADER);
                out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
                out.write("<html><body>\n\n");
                out.write("'" + remote_ip + "'<br>\n");
                out.write("<form action='http://www.kbresearch.nl/kbSRU'>");
                out.write("<input type=text name=q value='" + query + "' size=120>");
                out.write("<input type=hidden name=debug value=True>");
                out.write("<input type=submit>");
                out.write("<table border=1><tr><td>");
                out.write("q</td><td>" + query + "</td></tr><tr>");
                out.write("<td>query out</td><td>" + URLDecoder.decode(solrquery) + "</td></tr>");
                out.write("<tr><td>SOLR_URL</td><td> <a href='"
                        + this.config.getProperty("collection." + x_collection.toLowerCase() + ".solr_baseurl")
                        + "/?q=" + solrquery + "'>"
                        + this.config.getProperty("collection." + x_collection.toLowerCase() + ".solr_baseurl")
                        + "/select/?q=" + solrquery + "</a><br>" + this.config.getProperty("solr_url")
                        + solrquery + "</td></tr>");
                out.write("<b>SOLR_QUERY</b> : <BR> <iframe width=900 height=400 src='"
                        + this.config.getProperty("collection." + x_collection.toLowerCase() + ".solr_baseurl")
                        + "/../?q=" + solrquery + "'></iframe><BR>");
                out.write("<b>SRU_QUERY</b> : <BR> <a href=" + this.config.getProperty("baseurl") + "?q="
                        + query + "'>" + this.config.getProperty("baseurl") + "?q=" + query
                        + "</a><br><iframe width=901 height=400 src='http://www.kbresearch.nl/kbSRU/?q=" + query
                        + "'></iframe><BR>");
                out.write("<br><b>JSRU_QUERY</b> : <BR><a href='http://jsru.kb.nl/sru/?query=" + query
                        + "&x-collection=" + x_collection + "'>http://jsru.kb.nl/sru/?query=" + query
                        + "&x-collection=GGC</a><br><iframe width=900 height=400 src='http://jsru.kb.nl/sru/?query="
                        + query + "&x-collection=GGC'></iframe>");

            } else { // XML SearchRetrieve response
                String url = this.config
                        .getProperty("collection." + x_collection.toLowerCase() + ".solr_baseurl");
                String buffer = "";
                CommonsHttpSolrServer server = null;
                server = new CommonsHttpSolrServer(url);
                log.fatal("URSING " + url);
                server.setParser(new XMLResponseParser());
                int numfound = 0;
                try {
                    SolrQuery do_query = new SolrQuery();
                    do_query.setQuery(solrquery);
                    do_query.setRows(maximumRecords);
                    do_query.setStart(startRecord);

                    if ((sortKeys != null) && (sortKeys.length() > 1)) {
                        if (sortOrder != null) {
                            if (sortOrder.equals("asc")) {
                                do_query.setSortField(sortKeys, SolrQuery.ORDER.asc);
                            }
                            if (sortOrder.equals("desc")) {
                                do_query.setSortField(sortKeys, SolrQuery.ORDER.desc);
                            }
                        } else {
                            for (String str : sortKeys.trim().split(",")) {
                                str = str.trim();
                                if (str.length() > 1) {
                                    if (str.equals("date")) {
                                        do_query.setSortField("date_date", SolrQuery.ORDER.desc);
                                        log.debug("SORTORDERDEBUG | DATE! " + str + " | ");
                                        break;
                                    } else {
                                        do_query.setSortField(str + "_str", SolrQuery.ORDER.asc);
                                        log.debug("SORTORDERDEBUG | " + str + " | ");
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (facet != null) {
                        if (facet.indexOf(",") > 1) {
                            for (String str : facet.split(",")) {
                                if (str.indexOf("date") > 1) {
                                    do_query.addFacetField(str);
                                } else {
                                    do_query.addFacetField(str);
                                }
                                //do_query.setParam("facet.method", "enum");
                            }
                            //q.setFacetSort(false); 
                        } else {
                            do_query.addFacetField(facet);
                        }
                        do_query.setFacet(true);
                        do_query.setFacetMinCount(1);
                        do_query.setFacetLimit(-1);
                    }

                    log.fatal(solrquery);

                    QueryResponse rsp = null;
                    boolean do_err = false;
                    boolean do_sugg = false;
                    SolrDocumentList sdl = null;
                    String diag = "";
                    StringBuffer suggest = new StringBuffer("");

                    String content = "1";

                    SolrQuery spellq = do_query;
                    try {
                        rsp = server.query(do_query);
                    } catch (SolrServerException e) {
                        String header = this.SRW_HEADER.replaceAll("\\$numberOfRecords", "0");
                        out.write(header);
                        diag = this.SRW_DIAG.replaceAll("\\$error", e.getMessage());
                        do_err = true;
                        rsp = null;
                    }

                    log.fatal("query done..");
                    if (!(do_err)) { // XML dc response

                        SolrDocumentList docs = rsp.getResults();
                        numfound = (int) docs.getNumFound();
                        int count = startRecord;
                        String header = this.SRW_HEADER.replaceAll("\\$numberOfRecords",
                                Integer.toString(numfound));
                        out.write(header);
                        out.write("<srw:records>");

                        Iterator<SolrDocument> iter = rsp.getResults().iterator();

                        while (iter.hasNext()) {
                            count += 1;
                            if (recordSchema.equalsIgnoreCase("dc")) {
                                SolrDocument resultDoc = iter.next();
                                content = (String) resultDoc.getFieldValue("id");
                                out.write("<srw:record>");
                                out.write("<srw:recordPacking>xml</srw:recordPacking>");
                                out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                                out.write(
                                        "<srw:recordData xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/loc.terms/relators/OTH\" xmlns:facets=\"info:srw/extension/4/facets\" >");
                                StringBuffer result = new StringBuffer("");

                                construct_lucene_dc(result, resultDoc);

                                out.write(result.toString());
                                out.write("</srw:recordData>");
                                out.write("<srw:recordPosition>" + Integer.toString(count)
                                        + "</srw:recordPosition>");
                                out.write("</srw:record>");
                            }

                            if (recordSchema.equalsIgnoreCase("solr")) {
                                SolrDocument resultDoc = iter.next();
                                content = (String) resultDoc.getFieldValue("id");
                                out.write("<srw:record>");
                                out.write("<srw:recordPacking>xml</srw:recordPacking>");
                                out.write("<srw:recordSchema>info:srw/schema/1/solr</srw:recordSchema>");
                                out.write("<srw:recordData xmlns:expand=\"http://www.kbresearch.nl/expand\">");
                                StringBuffer result = new StringBuffer("");
                                construct_lucene_solr(result, resultDoc);
                                out.write(result.toString());

                                out.write("</srw:recordData>");
                                out.write("<srw:recordPosition>" + Integer.toString(count)
                                        + "</srw:recordPosition>");
                                out.write("</srw:record>");
                            }

                            if (recordSchema.equalsIgnoreCase("dcx")) { // XML dcx response
                                out.write("<srw:record>");
                                out.write("<srw:recordPacking>xml</srw:recordPacking>");
                                out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                                out.write(
                                        "<srw:recordData><srw_dc:dc xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/marc.relators/\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                                SolrDocument resultDoc = iter.next();
                                content = (String) resultDoc.getFieldValue("id");

                                String dcx_data = helpers.getOAIdcx(
                                        "http://services.kb.nl/mdo/oai?verb=GetRecord&identifier=" + content,
                                        log);
                                if (x_collection.equalsIgnoreCase("ggc-thes")) {
                                    dcx_data = helpers.getOAIdcx(
                                            "http://serviceso.kb.nl/mdo/oai?verb=GetRecord&identifier="
                                                    + content,
                                            log);
                                }

                                if (!(dcx_data.length() == 0)) {
                                    out.write(dcx_data);
                                } else {
                                    // Should not do this!!

                                    out.write("<srw:record>");
                                    out.write("<srw:recordPacking>xml</srw:recordPacking>");
                                    out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                                    out.write(
                                            "<srw:recordData xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/loc.terms/relators/OTH\" >");
                                    StringBuffer result = new StringBuffer("");

                                    construct_lucene_dc(result, resultDoc);

                                    out.write(result.toString());
                                    out.write("</srw:recordData>");
                                    out.write("<srw:recordPosition>" + Integer.toString(count)
                                            + "</srw:recordPosition>");
                                    out.write("</srw:record>");

                                }

                                out.write("</srw_dc:dc>");

                                StringBuffer expand_data;
                                boolean expand = false;

                                if (content.startsWith("GGC-THES:AC:")) {
                                    String tmp_content = "";
                                    tmp_content = content.replaceFirst("GGC-THES:AC:", "");
                                    log.fatal("calling get");
                                    expand_data = new StringBuffer(
                                            helpers.getExpand("http://www.kbresearch.nl/general/lod_new/get/"
                                                    + tmp_content + "?format=rdf", log));
                                    log.fatal("get finini");

                                    if (expand_data.toString().length() > 4) {

                                        out.write(
                                                "<srw_dc:expand xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                                        out.write(expand_data.toString());
                                        expand = true;
                                    }
                                } else {
                                    expand_data = new StringBuffer(helpers
                                            .getExpand("http://www.kbresearch.nl/ANP.cgi?q=" + content, log));
                                    if (expand_data.toString().length() > 0) {
                                        if (!expand) {
                                            out.write(
                                                    "<srw_dc:expand xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                                            expand = true;
                                        }
                                        out.write(expand_data.toString());
                                    }
                                }
                                if (expand) {
                                    out.write("</srw_dc:expand>");
                                }

                                out.write("</srw:recordData>");
                                out.write("<srw:recordPosition>" + Integer.toString(count)
                                        + "</srw:recordPosition>");
                                out.write("</srw:record>");
                            }
                        }
                    }

                    if ((do_err) || (numfound == 0)) {
                        log.fatal("I haz suggestions");

                        try {
                            spellq.setParam("spellcheck", true);
                            spellq.setQueryType("/spell");
                            server = new CommonsHttpSolrServer(url);
                            rsp = server.query(spellq);
                            sdl = rsp.getResults();
                            SpellCheckResponse spell;
                            spell = rsp.getSpellCheckResponse();
                            List<SpellCheckResponse.Suggestion> suggestions = spell.getSuggestions();
                            if (suggestions.isEmpty() == false) {
                                suggest.append("<srw:extraResponseData>");
                                suggest.append("<suggestions>");

                                for (SpellCheckResponse.Suggestion sugg : suggestions) {
                                    suggest.append("<suggestionfor>" + sugg.getToken() + "</suggestionfor>");
                                    for (String item : sugg.getSuggestions()) {
                                        suggest.append("<suggestion>" + item + "</suggestion>");
                                    }
                                    suggest.append("</suggestions>");
                                    suggest.append("</srw:extraResponseData>");
                                }
                                do_sugg = true;
                            }
                        } catch (Exception e) {
                            rsp = null;
                            //log.fatal(e.toString());
                        }
                        ;
                    }
                    ;

                    if (!do_err) {
                        if (facet != null) {

                            try {
                                fct = rsp.getFacetFields();
                                out.write("<srw:facets>");

                                for (String str : facet.split(",")) {
                                    out.write("<srw:facet>");
                                    out.write("<srw:facetType>");
                                    out.write(str);
                                    out.write("</srw:facetType>");

                                    for (FacetField f : fct) {
                                        log.debug(f.getName());
                                        //if (f.getName().equals(str+"_str") || (f.getName().equals(str+"_date")) ) {
                                        List<FacetField.Count> facetEnties = f.getValues();
                                        for (FacetField.Count fcount : facetEnties) {
                                            out.write("<srw:facetValue>");
                                            out.write("<srw:valueString>");
                                            out.write(helpers.xmlEncode(fcount.getName()));
                                            out.write("</srw:valueString>");
                                            out.write("<srw:count>");
                                            out.write(Double.toString(fcount.getCount()));
                                            out.write("</srw:count>");
                                            out.write("</srw:facetValue>");
                                            //   }
                                        }

                                    }
                                    out.write("</srw:facet>");
                                }
                                out.write("</srw:facets>");
                                startRecord += 1;
                            } catch (Exception e) {
                            }

                            //log.fatal(e.toString()); }
                        }
                    } else {
                        out.write(diag);
                    }
                    out.write("</srw:records>"); // SearchRetrieve response footer
                    String footer = this.SRW_FOOTER.replaceAll("\\$query", helpers.xmlEncode(query));
                    footer = footer.replaceAll("\\$startRecord", (startRecord).toString());
                    footer = footer.replaceAll("\\$maximumRecords", maximumRecords.toString());
                    footer = footer.replaceAll("\\$recordSchema", recordSchema);
                    if (do_sugg) {
                        out.write(suggest.toString());
                    }
                    out.write(footer);
                } catch (MalformedURLException e) {
                    out.write(e.getMessage());
                } catch (IOException e) {
                    out.write("TO ERR is Human");
                }
            }
        }
    }
    out.close();
}

From source file:net.cloudfree.apps.shop.internal.app.ListingServlet.java

License:Open Source License

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    final IListingManager manager = ModelUtil.getManager(IListingManager.class, getContext());

    final ISolrQueryExecutor queryExecutor = (ISolrQueryExecutor) manager.getAdapter(ISolrQueryExecutor.class);
    if (null == queryExecutor) {
        resp.sendError(404);// w ww  . java  2 s  .c  o m
        return;
    }

    final List<String> selectedFacets = new ArrayList<String>();

    final SolrQuery query = new SolrQuery();
    boolean facet = true;
    boolean checkVariations = false;

    final String path = req.getPathInfo();
    if ((null != path) && (path.length() > 1)) {
        query.setFilterQueries(Document.URI_PATH + ":" + path.substring(1));
        query.setFields("id", "title", "price", "name", "score", "img480", "uripath", "description");
        facet = false;
        checkVariations = true;
    } else {
        final String q = req.getParameter("q");
        if (StringUtils.isNotBlank(q)) {
            query.setQuery(q);
        }

        query.setFields("id", "title", "price", "name", "score", "img48", "uripath");

        // ignore variations
        final String f = req.getParameter("f");
        if (StringUtils.isNotBlank(f)) {
            query.addFilterQuery(f);
        } else {
            query.addFilterQuery("-type:variation");
        }

        // facet narrowing?
        final String[] narrows = req.getParameterValues("narrow");
        if (null != narrows) {
            for (final String narrow : narrows) {
                if (StringUtils.isBlank(narrow)) {
                    continue;
                }
                final String[] split = StringUtils.split(narrow, ':');
                if (split.length != 2) {
                    continue;
                }
                final String name = split[0];
                final String value = split[1];
                if (StringUtils.isBlank(name) || StringUtils.isBlank(value)) {
                    continue;
                }
                final FacetFilter filter = facetFilters.get(name);
                if (null != filter) {
                    if (filter.addFilter(query, value)) {
                        selectedFacets.add(name + ":" + value);
                    }
                }
            }
        }
    }

    query.setQueryType("dismax");
    query.set("debug", true);

    // facet fields
    if (facet) {
        query.setFacet(true);
        for (final FacetFilter filter : facetFilters.values()) {
            filter.defineFilter(query);
        }
    }

    final QueryResponse response = queryExecutor.query(query);
    final SolrDocumentList results = response.getResults();

    resp.setContentType("text/html");
    resp.setCharacterEncoding("UTF-8");

    final PrintWriter writer = resp.getWriter();

    writer.println("<html><head>");
    writer.println("<title>");
    writer.println(path);
    writer.println(" - Shop Listings</title>");
    writer.println("</head><body>");

    writer.println("<h1>Found Listings</h1>");
    writer.println("<p>");
    writer.println("CloudFree found <strong>" + results.getNumFound() + "</strong> products in "
            + (response.getQTime() < 1000 ? "less than a second." : (response.getQTime() + "ms.")));
    if (results.size() < results.getNumFound()) {
        writer.println("<br/>");
        if (results.getStart() == 0) {
            writer.println("Only the first " + results.size() + " products are shown.");
        } else {
            writer.println("Only products " + results.getStart() + " till "
                    + (results.getStart() + results.size()) + " will be shown.");
        }
    }
    writer.println("</p>");

    final List<FacetField> facetFields = response.getFacetFields();
    if ((null != facetFields) && !facetFields.isEmpty()) {
        writer.println("<p>");
        writer.println("You can filter the results by: </br>");
        for (final FacetField facetField : facetFields) {
            final List<Count> values = facetField.getValues();
            if ((null != values) && !values.isEmpty()) {
                writer.println("<div style=\"float:left;\">");
                writer.print("<em>");
                writer.print(facetField.getName());
                writer.print("</em>");
                writer.println("<ul style=\"margin:0;\">");
                int filters = 0;
                for (final Count count : values) {
                    if (count.getCount() == 0) {
                        continue;
                    }
                    writer.print("<li>");
                    writer.print(count.getName());
                    writer.print(" (");
                    writer.print(count.getCount());
                    writer.print(")");
                    writer.print("</li>");
                    filters++;
                }
                if (filters == 0) {
                    writer.print("<li>none</li>");
                }
                writer.println("</ul>");
                writer.println("</div>");
            }
        }
        writer.println("<div style=\"clear:both;\">&nbsp;</div>");
        writer.println("</p>");
    }

    writer.println("<p>");
    if (!results.isEmpty()) {
        for (final SolrDocument listing : results) {
            writeListing(listing, writer, req);

            if (checkVariations) {
                final SolrQuery query2 = new SolrQuery();
                query2.setQuery("parentid:" + listing.getFirstValue("id"));
                query2.setFields("id", "title", "price", "name", "score", "img48", "uripath", "color", "size");
                final QueryResponse response2 = queryExecutor.query(query2);
                final SolrDocumentList results2 = response2.getResults();
                if ((null != results2) && !results2.isEmpty()) {
                    writer.println("There are " + results2.size() + " variations available.");
                    for (final SolrDocument variation : results2) {
                        writeListing(variation, writer, req);
                    }
                }
            }
        }
    } else {
        writer.println("No listings found!");
    }
    writer.println("</p>");
    writer.println("</body>");

}

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  .j  ava2  s.c  o 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.cora.federate.solr.connector.AbstractSolrConnector.java

License:Open Source License

/**
 * check if a given document, identified by url hash as document id exists
 * @param id the url hash and document id
 * @return metadata if any entry in solr exists, null otherwise
 * @throws IOException//w w w. j a  va 2s. c  om
 */
@Override
public LoadTimeURL getLoadTimeURL(String id) throws IOException {
    // construct raw query
    final SolrQuery params = new SolrQuery();
    //params.setQuery(CollectionSchema.id.getSolrFieldName() + ":\"" + id + "\"");
    String q = "{!cache=false raw f=" + CollectionSchema.id.getSolrFieldName() + "}" + id;
    params.setQuery(q);
    params.setRows(1);
    params.setStart(0);
    params.setFacet(false);
    params.clearSorts();
    params.setFields(CollectionSchema.id.getSolrFieldName(), CollectionSchema.sku.getSolrFieldName(),
            CollectionSchema.load_date_dt.getSolrFieldName());
    params.setIncludeScore(false);

    // query the server
    final SolrDocumentList sdl = getDocumentListByParams(params);
    if (sdl == null || sdl.getNumFound() <= 0)
        return null;
    SolrDocument doc = sdl.iterator().next();
    LoadTimeURL md = getLoadTimeURL(doc);
    return md;
}

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

License:Open Source License

/**
 * get the number of results when this query is done.
 * This should only be called if the actual result is never used, and only the count is interesting
 * @param querystring/*from  w ww  .  j  av a  2  s.  c  o m*/
 * @return the number of results for this query
 */
@Override
public long getCountByQuery(String querystring) throws IOException {
    // construct query
    final SolrQuery params = new SolrQuery();
    params.setQuery(querystring);
    params.setRows(0); // essential to just get count
    params.setStart(0);
    params.setFacet(false);
    params.clearSorts();
    params.setFields(CollectionSchema.id.getSolrFieldName());
    params.setIncludeScore(false);

    // query the server
    final SolrDocumentList sdl = getDocumentListByParams(params);
    return sdl == null ? 0 : sdl.getNumFound();
}

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

License:Open Source License

/**
 * get facets of the index: a list of lists with values that are most common in a specific field
 * @param query a query which is performed to get the facets
 * @param fields the field names which are selected as facet
 * @param maxresults the maximum size of the resulting maps
 * @return a map with key = facet field name, value = an ordered map of field values for that field
 * @throws IOException// w w  w.  java 2s. c o  m
 */
@Override
public LinkedHashMap<String, ReversibleScoreMap<String>> getFacets(String query, int maxresults,
        final String... fields) throws IOException {
    // construct query
    assert fields.length > 0;
    final SolrQuery params = new SolrQuery();
    params.setQuery(query);
    params.setRows(0);
    params.setStart(0);
    params.setFacet(true);
    params.setFacetMinCount(1); // there are many 0-count facets in the uninverted index cache
    params.setFacetLimit(maxresults);
    params.setFacetSort(FacetParams.FACET_SORT_COUNT);
    params.setParam(FacetParams.FACET_METHOD, FacetParams.FACET_METHOD_fc /*FACET_METHOD_fcs*/);
    params.setFields(fields);
    params.clearSorts();
    params.setIncludeScore(false);
    for (String field : fields)
        params.addFacetField(field);

    // query the server
    QueryResponse rsp = getResponseByParams(params);
    LinkedHashMap<String, ReversibleScoreMap<String>> facets = new LinkedHashMap<String, ReversibleScoreMap<String>>(
            fields.length);
    for (String field : fields) {
        FacetField facet = rsp.getFacetField(field);
        ReversibleScoreMap<String> result = new ClusteredScoreMap<String>(UTF8.insensitiveUTF8Comparator);
        List<Count> values = facet.getValues();
        if (values == null)
            continue;
        for (Count ff : values)
            if (ff.getCount() > 0)
                result.set(ff.getName(), (int) ff.getCount());
        facets.put(field, result);
    }
    return facets;
}