Example usage for org.apache.solr.response SolrQueryResponse add

List of usage examples for org.apache.solr.response SolrQueryResponse add

Introduction

In this page you can find the example usage for org.apache.solr.response SolrQueryResponse add.

Prototype

public void add(String name, Object val) 

Source Link

Document

Appends a named value to the list of named values to be returned.

Usage

From source file:alba.components.FilteredShowFileRequestHandler.java

License:Apache License

private void showFromZooKeeper(SolrQueryRequest req, SolrQueryResponse rsp, CoreContainer coreContainer)
        throws KeeperException, InterruptedException, UnsupportedEncodingException {

    SolrZkClient zkClient = coreContainer.getZkController().getZkClient();

    String adminFile = getAdminFileFromZooKeeper(req, rsp, zkClient, hiddenFiles);

    if (adminFile == null) {
        return;//from  w ww.jav a 2  s  .  c  o  m
    }

    // Show a directory listing
    List<String> children = zkClient.getChildren(adminFile, null, true);
    if (children.size() > 0) {

        NamedList<SimpleOrderedMap<Object>> files = new SimpleOrderedMap<>();
        for (String f : children) {
            if (isHiddenFile(req, rsp, f, false, hiddenFiles)) {
                continue;
            }

            SimpleOrderedMap<Object> fileInfo = new SimpleOrderedMap<>();
            files.add(f, fileInfo);
            List<String> fchildren = zkClient.getChildren(adminFile + "/" + f, null, true);
            if (fchildren.size() > 0) {
                fileInfo.add("directory", true);
            } else {
                // TODO? content type
                fileInfo.add("size", f.length());
            }
            // TODO: ?
            // fileInfo.add( "modified", new Date( f.lastModified() ) );
        }
        rsp.add("files", files);
    } else {
        // Include the file contents
        // The file logic depends on RawResponseWriter, so force its use.
        ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
        params.set(CommonParams.WT, "raw");
        req.setParams(params);

        ContentStreamBase content = new ContentStreamBase.ByteArrayStream(
                zkClient.getData(adminFile, null, null, true), adminFile);
        content.setContentType(req.getParams().get(USE_CONTENT_TYPE));

        // Velocity parsing here!

        // http://velocity.apache.org/engine/devel/developer-guide.html#The_Context
        Velocity.init();

        VelocityContext context = new VelocityContext();

        //add some vars??
        //context.put( "context", new String("Velocity") );

        for (int i = 0; i < rsp.getValues().size(); i++) {
            context.put(rsp.getValues().getName(i), rsp.getValues().getVal(i));
        }

        Template template = null;

        String fname = req.getParams().get("file", null);

        try {
            //TODO what if fname is null?
            template = Velocity.getTemplate(fname);
        } catch (ResourceNotFoundException rnfe) {
            // couldn't find the template, try to load it

            // TODO it should be fired only for SOME mimetypes (..through an annotation??)
            StringBuilder sb = this.getTemplate(content);

            RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
            StringReader reader = new StringReader(sb.toString());
            SimpleNode node = null;
            try {
                node = runtimeServices.parse(reader, fname);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                logger.error("error while parsing new template", e);
            }

            template = new Template();

            template.setRuntimeServices(runtimeServices);

            if (node != null) {
                template.setData(node);
            } else {
                logger.error("node null, can't set on template");
            }

            template.initDocument();

        } catch (ParseErrorException pee) {
            // syntax error: problem parsing the template
            logger.error("error while parsing template: ", pee);

        } catch (MethodInvocationException mie) {
            // something invoked in the template
            // threw an exception
            logger.error("error while parsing temaplate: ", mie);
        } catch (Exception e) {
            logger.error("error while parsing temaplate: ", e);
        }

        StringWriter sw = new StringWriter();

        template.merge(context, sw);

        // http://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte
        content = new ContentStreamBase.ByteArrayStream(
                sw.getBuffer().toString().getBytes(Charset.forName("UTF-8")), adminFile);
        content.setContentType(req.getParams().get(USE_CONTENT_TYPE));

        rsp.add(RawResponseWriter.CONTENT, content);
    }
    rsp.setHttpCaching(false);
}

From source file:alba.components.FilteredShowFileRequestHandler.java

License:Apache License

private void showFromFileSystem(SolrQueryRequest req, SolrQueryResponse rsp) {
    File adminFile = getAdminFileFromFileSystem(req, rsp, hiddenFiles);

    if (adminFile == null) { // exception already recorded
        return;/*www.ja va 2  s  .  c  om*/
    }

    // Make sure the file exists, is readable and is not a hidden file
    if (!adminFile.exists()) {
        log.error("Can not find: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]");
        rsp.setException(new SolrException(ErrorCode.NOT_FOUND,
                "Can not find: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]"));
        return;
    }
    if (!adminFile.canRead() || adminFile.isHidden()) {
        log.error("Can not show: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]");
        rsp.setException(new SolrException(ErrorCode.NOT_FOUND,
                "Can not show: " + adminFile.getName() + " [" + adminFile.getAbsolutePath() + "]"));
        return;
    }

    // Show a directory listing
    if (adminFile.isDirectory()) {
        // it's really a directory, just go for it.
        int basePath = adminFile.getAbsolutePath().length() + 1;
        NamedList<SimpleOrderedMap<Object>> files = new SimpleOrderedMap<>();
        for (File f : adminFile.listFiles()) {
            String path = f.getAbsolutePath().substring(basePath);
            path = path.replace('\\', '/'); // normalize slashes

            if (isHiddenFile(req, rsp, f.getName().replace('\\', '/'), false, hiddenFiles)) {
                continue;
            }

            SimpleOrderedMap<Object> fileInfo = new SimpleOrderedMap<>();
            files.add(path, fileInfo);
            if (f.isDirectory()) {
                fileInfo.add("directory", true);
            } else {
                // TODO? content type
                fileInfo.add("size", f.length());
            }
            fileInfo.add("modified", new Date(f.lastModified()));
        }
        rsp.add("files", files);
    } else {
        // Include the file contents
        //The file logic depends on RawResponseWriter, so force its use.
        ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
        params.set(CommonParams.WT, "raw");
        req.setParams(params);

        ContentStreamBase content = new ContentStreamBase.FileStream(adminFile);
        content.setContentType(req.getParams().get(USE_CONTENT_TYPE));

        rsp.add(RawResponseWriter.CONTENT, content);
    }
    rsp.setHttpCaching(false);
}

From source file:alba.solr.searchcomponents.AlbaRequestHandler.java

License:Apache License

public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    // TODO Auto-generated method stub

    /* List<SearchComponent> components = new ArrayList<SearchComponent>();
            /*w w  w.j a  va2s  .c  o  m*/
    MySearchComponent msc = new MySearchComponent();
            
    ResponseBuilder rb = new ResponseBuilder(req, rsp, components);
            
    msc.process(rb);*/

    //rsp.add("hello", rb.rsp.getValues());

    req.getContext().put(Loader.FUNCTIONS, functions);

    Object params[] = new Object[2];

    params[0] = req;
    params[1] = rsp;

    // what if this method calls rsp.add( .. ) ????
    Object result = this.function.getMethod().invoke(this.function.getInstance(), params);

    if (Map.class.isAssignableFrom(result.getClass())) {
        // if we got a Map, just return it as-is
        rsp.add(this.sectionName, result);
    } else // if we got anything else, try to serialize it!
    if (List.class.isAssignableFrom(result.getClass())) {
        for (Object o : (List) result) {
            DocumentObjectBinder dob = new DocumentObjectBinder();
            SolrInputDocument sd = dob.toSolrInputDocument(o);
            SolrDocument dest = ClientUtils.toSolrDocument(sd);

            HashMap<Object, Object> nl = (HashMap<Object, Object>) dest.get("functionDescriptor");

            //rsp.add(nl.get("name").toString(), dest2);

            rsp.add(null, dest);
        }
    }
    if (StaticResource.class.isAssignableFrom(result.getClass())) {
        FilteredShowFileRequestHandler file = new FilteredShowFileRequestHandler();

        file.init(new NamedList()); //to initialize internal variables - but in this way it will NOT get the proper configuration from SolrConfig!

        ModifiableSolrParams solrParams = new ModifiableSolrParams(req.getParams());

        StaticResource resource = ((StaticResource) result);
        solrParams.set("file", resource.getName());
        //TODO Proper mapping here!!
        //solrParams.set("contentType", "text/xml;charset=utf-8");

        solrParams.set("contentType", resource.getContentType());
        req.setParams(solrParams);

        file.handleRequest(req, rsp);
        //   logger.error("returning the content of " + );
    } else {
        // unable to do any kind of serialization.. just add the result and let the ResponseWriter handle it
        rsp.add(null, result);
    }

}

From source file:at.newmedialab.lmf.util.solr.suggestion.service.SuggestionService.java

License:Apache License

public void run(SolrQueryResponse rsp, String query, String df, String[] fields, String[] fqs, int limit,
        boolean multivalue) {

    //analyze query in advance
    query = FieldAnalyzerService.analyzeString(solrCore, df, query);

    SuggestionResult result = null;//from  ww  w .  j  av  a2 s  .com

    Object spellcheck_result = null;

    SolrQueryResponse response = query(query, df, fields, fqs);

    //of no results, try spellcheck (if defined and if spellchecked query differs from original)
    if (((ResultContext) response.getValues().get("response")).docs.size() > 0) {
        result = SuggestionResultFactory.createResult(solrCore, response, fields, query, df, multivalue);
    } else if (spellcheck_enabled) {
        String spellchecked_query = getSpellcheckedQuery(query, response);
        spellcheck_result = response.getValues().get("spellcheck");

        //query with spellchecked query
        if (spellchecked_query != null) {
            response = query(spellchecked_query, df, fields, fqs);
            if (((ResultContext) response.getValues().get("response")).docs.size() > 0) {
                result = SuggestionResultFactory.createResult(solrCore, response, fields, spellchecked_query,
                        df, multivalue);
            }
        }
    }

    //add result of spellcheck component
    if (spellcheck_result != null) {
        //TODO remove * on last position of collation
        rsp.add("spellcheck", spellcheck_result);
    }

    //create an empty result
    if (result == null)
        result = new SuggesionResultSingle();

    rsp.add(SuggestionResultParams.SUGGESTIONS, result.write());
}

From source file:at.newmedialab.lmf.util.solr.SuggestionRequestHandler.java

License:Apache License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {

    SolrParams params = req.getParams();

    if (params.getBool(SuggestionRequestParams.SUGGESTION, SUGGESTION)) {

        String q = params.get(CommonParams.Q);
        if (q == null) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'q' parameter"));
            return;
        }//from  ww  w . j a va2 s.c o  m

        String[] fields = params.getParams(SuggestionRequestParams.SUGGESTION_FIELD) != null
                ? params.getParams(SuggestionRequestParams.SUGGESTION_FIELD)
                : FIELDS;
        if (fields == null) {
            rsp.add("error",
                    error(400, "SuggestionRequest needs to have at least one 'suggestion.field' parameter"));
            return;
        }

        String df = params.get(SuggestionRequestParams.SUGGESTION_DF, DF);
        if (df == null) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'df' parameter"));
            return;
        }

        int limit = params.getInt(SuggestionRequestParams.SUGGESTION_LIMIT, LIMIT);
        if (limit < 1) {
            rsp.add("error", error(400, "SuggestionRequest needs to have a 'suggestion.limit' greater than 0"));
            return;
        }

        String[] fqs = params.getParams(CommonParams.FQ) != null ? params.getParams(CommonParams.FQ) : FQS;

        Boolean multivalue = params.getBool(SuggestionRequestParams.SUGGESTION_MULTIVALUE, MULTIVALUE);

        //TODO replace
        if (multivalue) {
            rsp.add("error", error(500, "Multivalue suggestions are not yet supported!"));
            return;
        }

        suggestionService.run(rsp, q, df, fields, fqs, limit, multivalue);

    } else {
        super.handleRequestBody(req, rsp);
    }
}

From source file:com.adr.bigdata.search.product.fe.BaseSuggestionHandler.java

@Override
public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {
    String cacheKey = SolrParamUtils.transform(req.getParams());
    try {/*from w  ww  .  j a  v  a 2s.  com*/
        String cacheResponse = this.rdModel.get(cacheKey);
        if (cacheResponse == null) {
            //cacheMiss
            try {
                final Object[] terms = new Object[1];
                suggestionLogic.execute(req, rsp, new Callable() {
                    @Override
                    public void call(Object... args) {
                        terms[0] = args[0];
                    }
                });
                SolrQueryResponse _rsp = new SolrQueryResponse();
                super.handleRequest(req, _rsp);
                suggestionLogic.writeRsp(req, _rsp, terms[0]);
                String result = outerString(req, _rsp);
                if (!zeroResult(_rsp)) {
                    this.rdModel.put(cacheKey, result);
                }
                ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
                params.set("vincache", true);
                SolrParams _params = SolrParams.wrapDefaults(params, defaults);
                _params = SolrParams.wrapAppended(_params, appends);
                req.setParams(_params);
                rsp.add("vincache", result);
            } catch (Exception e) {
                error(req, rsp);
                getLogger().error("", e);
            }

        } else {
            //cache hit
            ModifiableSolrParams solrParam = new ModifiableSolrParams(req.getParams());
            solrParam.set("vincache", true);
            SolrParams params = SolrParams.wrapAppended(solrParam, appends);
            params = SolrParams.wrapDefaults(params, defaults);
            req.setParams(params);
            rsp.add("vincache", cacheResponse);
        }

    } catch (Exception cacheEx) {
        getLogger().error("fail to get from redis cache.....{}", cacheEx.getMessage());
        try {
            final Object[] terms = new Object[1];

            suggestionLogic.execute(req, rsp, new Callable() {
                @Override
                public void call(Object... args) {
                    terms[0] = args[0];
                }
            });
            super.handleRequest(req, rsp);
            suggestionLogic.writeRsp(req, rsp, terms[0]);
        } catch (Exception e) {
            error(req, rsp);
            getLogger().error("", e);
        }
    }
}

From source file:com.basho.yokozuna.handler.EntropyData.java

License:Open Source License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp)
        throws Exception, InstantiationException, IllegalAccessException {

    String contParam = req.getParams().get("continue");
    BytesRef cont = contParam != null ? decodeCont(contParam) : DEFAULT_CONT;

    // TODO: Make before required in handler config
    String before = req.getParams().get("before");
    if (before == null) {
        throw new Exception("Parameter 'before' is required");
    }/*from   w  w  w .  j  av a 2s.com*/
    int n = req.getParams().getInt("n", DEFAULT_N);
    SolrDocumentList docs = new SolrDocumentList();

    // Add docs here and modify object inline in code
    rsp.add("response", docs);

    try {
        SolrIndexSearcher searcher = req.getSearcher();
        AtomicReader rdr = searcher.getAtomicReader();
        BytesRef tmp = null;
        Terms terms = rdr.terms(ENTROPY_DATA_FIELD);
        TermsEnum te = terms.iterator(null);

        if (isContinue(cont)) {
            log.debug("continue from " + cont);

            TermsEnum.SeekStatus status = te.seekCeil(cont, true);

            if (status == TermsEnum.SeekStatus.END) {
                rsp.add("more", false);
                return;
            } else if (status == TermsEnum.SeekStatus.FOUND) {
                // If this term has already been seen then skip it.
                tmp = te.next();

                if (endOfItr(tmp)) {
                    rsp.add("more", false);
                    return;
                }
            } else if (status == TermsEnum.SeekStatus.NOT_FOUND) {
                tmp = te.next();
            }
        } else {
            tmp = te.next();
        }

        String text = null;
        String[] vals = null;
        String ts = null;
        String docId = null;
        String vectorClock = null;
        int count = 0;
        BytesRef current = null;

        while (!endOfItr(tmp) && count < n) {
            current = BytesRef.deepCopyOf(tmp);
            text = tmp.utf8ToString();
            log.debug("text: " + text);
            vals = text.split(" ");
            ts = vals[0];

            // TODO: what if null?
            if (!(ts.compareTo(before) < 0)) {
                rsp.add("more", false);
                docs.setNumFound(count);
                return;
            }

            docId = vals[1];
            vectorClock = vals[2];
            SolrDocument tmpDoc = new SolrDocument();
            tmpDoc.addField("doc_id", docId);
            tmpDoc.addField("base64_vclock", Base64.encodeBase64String(sha(vectorClock)));
            docs.add(tmpDoc);
            count++;
            tmp = te.next();
        }

        if (count < n) {
            rsp.add("more", false);
        } else {
            rsp.add("more", true);
            String newCont = Base64.encodeBase64URLSafeString(current.bytes);
            // The continue context for next req to start where
            // this one finished.
            rsp.add("continuation", newCont);
        }

        docs.setNumFound(count);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.search.MySearchHandler.java

License:Apache License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    // int sleep = req.getParams().getInt("sleep",0);
    // if (sleep > 0) {log.error("SLEEPING for " + sleep);
    // Thread.sleep(sleep);}

    /*** Pre-processing of the Query by REGEX starts here -------------- ***/
    SolrParams originalParams = req.getOriginalParams();
    SolrParams psuedoParams = req.getParams(); // These psuedoParams keep
    // changing//from   w w  w .  ja  v a 2 s.  co m
    if (originalParams.get(CommonParams.Q) != null) {
        String finalQuery;
        String originalQuery = originalParams.get(CommonParams.Q);
        rsp.add("Original query", originalQuery);
        SchemaField keyField = null;
        keyField = req.getCore().getLatestSchema().getUniqueKeyField();
        if (keyField != null) {
            fieldSet.add(keyField.getName());
        }
        /***
         * START CODE TO PARSE QUERY
         * 
         * Arafath's code to prepare query starts here The query should be
         * in the following format ->
         * 
         * 
         * 
         * Example : Original Query:
         * "Which musical object did russ conway play" Temporary Query :
         * "relations:instrument AND entity:enty" // Generate the relation
         * Final Query : "name:"russ conway" AND occupation:musician"
         */
        ParsedQuestion parsedq = new ParsedQuestion();
        parsedq = parseQues(originalQuery);
        if (parsedq != null) {
            System.out.println(parsedq);
            Map<Integer, String> relationstr = getRelation(parsedq.getRelationKeyWord(), parsedq.getWhtype(),
                    req);

            /**
             * END CODE TO PARSE QUERY
             */

            /*** Final Phase starts here ***/
            finalQuery = "title:\"" + parsedq.getSearchName() + "\"";
            NamedList finalparamsList = req.getParams().toNamedList();
            finalparamsList.setVal(finalparamsList.indexOf(CommonParams.Q, 0), finalQuery);
            psuedoParams = SolrParams.toSolrParams(finalparamsList);
            if (psuedoParams.get(CommonParams.SORT) == null) {
                finalparamsList.add(CommonParams.SORT, "score desc");
            } else {
                finalparamsList.setVal(finalparamsList.indexOf(CommonParams.SORT, 0), "score desc");
            }
            int documentsRetrieved = 0;
            if (relationstr != null) {
                rsp.add("total relations retrieved", relationstr.size());
                rsp.add("relations", relationstr);
                NamedList finaldocresults = new NamedList();
                NamedList forwarddocresults = new NamedList();
                Set<String> checkDocuments = new HashSet<String>();

                for (int i = 0; i < relationstr.size() && (documentsRetrieved < 10); i++) {
                    NamedList relationdocresults = new NamedList();
                    String desiredField = relationstr.get(i);
                    Set<String> tempFieldSet = new HashSet<String>();
                    int docsRetrievedforThisRelation = 0;
                    tempFieldSet.add(desiredField);
                    psuedoParams = SolrParams.toSolrParams(finalparamsList);
                    if (psuedoParams.get(CommonParams.FL) == null) {
                        finalparamsList.add(CommonParams.FL, desiredField);
                    } else {
                        finalparamsList.setVal(finalparamsList.indexOf(CommonParams.FL, 0), desiredField);
                    }
                    SolrQueryRequest finalreq = new LocalSolrQueryRequest(req.getCore(), finalparamsList);
                    rsp.add("Final Query", finalreq.getParams().get(CommonParams.Q));

                    SolrQueryResponse finalrsp = new SolrQueryResponse();

                    ResponseBuilder finalrb = new ResponseBuilder(finalreq, finalrsp, components);
                    for (SearchComponent c : components) {
                        c.prepare(finalrb);
                        c.process(finalrb);
                    }

                    DocList finaldocs = finalrb.getResults().docList;
                    if (finaldocs == null || finaldocs.size() == 0) {
                        log.debug("No results");
                        // support for reverse query
                    } else {
                        DocIterator finaliterator = finaldocs.iterator();
                        Document finaldoc;
                        for (int j = 0; j < finaldocs.size(); j++) {
                            try {
                                if (finaliterator.hasNext()) {
                                    int finaldocid = finaliterator.nextDoc();
                                    finaldoc = finalrb.req.getSearcher().doc(finaldocid, tempFieldSet);
                                    if (!checkDocuments.contains(finaldoc.get("id"))) {
                                        if (finaldoc.get(desiredField) != null) {
                                            checkDocuments.add(finaldoc.get("id"));
                                            docsRetrievedforThisRelation++;
                                            documentsRetrieved++;
                                            relationdocresults.add(finaldoc.get("title"), finaldoc);
                                            if (documentsRetrieved >= 10) {
                                                break;
                                            }
                                        }
                                    }
                                }
                            } catch (IOException ex) {
                                java.util.logging.Logger.getLogger(MySearchHandler.class.getName())
                                        .log(Level.SEVERE, null, ex);
                            }
                        }
                        if (docsRetrievedforThisRelation > 0) {
                            rsp.add("docs retrieved for : " + desiredField, docsRetrievedforThisRelation);
                            forwarddocresults.add(desiredField, relationdocresults);
                        }
                    }
                    finalreq.close();
                    if (documentsRetrieved > 0) {
                        rsp.add("type", "forward");
                        rsp.add("final results", forwarddocresults);
                    }
                }
                if (documentsRetrieved == 0) {
                    NamedList reversedocresults = new NamedList();
                    relationstr = getRelationReverse(parsedq.getRelationKeyWord(), req);
                    System.out.println(relationstr);
                    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);
                    String reversequery = "";
                    for (int i = 0; i < relationstr.size(); i++) {
                        QueryParser relationsparser = new QueryParser(Version.LUCENE_46, relationstr.get(i),
                                analyzer);
                        try {
                            reversequery += relationsparser.parse(parsedq.getSearchName()).toString() + " ";
                        } catch (ParseException e) {

                            e.printStackTrace();
                        }
                    }
                    QueryParser relationsparser = new QueryParser(Version.LUCENE_46, "infotype", analyzer);
                    reversequery += relationsparser.parse(parsedq.getWhtype().firstKey().toLowerCase());

                    NamedList reverseList = req.getParams().toNamedList();
                    psuedoParams = SolrParams.toSolrParams(reverseList);
                    reverseList.setVal(reverseList.indexOf(CommonParams.Q, 0), reversequery);
                    SolrQueryRequest reversereq = new LocalSolrQueryRequest(req.getCore(), reverseList);
                    SolrQueryResponse reversersp = new SolrQueryResponse();
                    ResponseBuilder reverserb = new ResponseBuilder(reversereq, reversersp, components);
                    for (SearchComponent c : components) {
                        try {
                            c.prepare(reverserb);
                            c.process(reverserb);
                        } catch (IOException ex) {

                            java.util.logging.Logger.getLogger(MySearchHandler.class.getName())
                                    .log(Level.SEVERE, null, ex);
                        }
                    }
                    DocList reversedocs = reverserb.getResults().docList;
                    if (reversedocs == null || reversedocs.size() == 0) {
                        log.debug("No results");
                        // GET SECOND entry from WHTYPE .. search with that ..
                    } else {
                        //   NamedList docresults = new NamedList();
                        DocIterator reverseiterator = reversedocs.iterator();
                        Document reversedoc;
                        int docScore = 0;
                        for (int m = 0; m < reversedocs.size(); m++) {
                            try {
                                int reversedocid = reverseiterator.nextDoc();
                                reversedoc = reverserb.req.getSearcher().doc(reversedocid, fieldSet);
                                if (reversedoc.get("title") != null) {
                                    documentsRetrieved++;
                                    reversedocresults.add(reversedoc.get("title"), reversedoc);
                                    if (documentsRetrieved >= 10) {
                                        break;
                                    }
                                }
                            } catch (IOException ex) {
                                java.util.logging.Logger.getLogger(MySearchHandler.class.getName())
                                        .log(Level.SEVERE, null, ex);
                            }
                        }
                    }
                    if (documentsRetrieved == 0) {
                        rsp.add("message", "No Results found. Try another query!");
                    } else {
                        rsp.add("type", "reverse");
                        rsp.add("final results", reversedocresults);
                    }
                    reversereq.close();
                }
            } else {
                if (documentsRetrieved == 0) {
                    rsp.add("message", "No Results found. Please rephrase the query!");
                }
            }
        } else {
            rsp.add("message", "This is not a valid query!");
        }
    } else {
        rsp.add("message", "User should provide at least one word as a query!");
    }
}

From source file:com.search.MySearchHandlerTest.java

License:Apache License

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    // int sleep = req.getParams().getInt("sleep",0);
    // if (sleep > 0) {log.error("SLEEPING for " + sleep);  Thread.sleep(sleep);}

    /*** Pre-processing of the Query by REGEX starts here --------------***/
    SolrParams originalParams = req.getOriginalParams();
    SolrParams psuedoParams = req.getParams(); // These psuedoParams keep changing
    if (originalParams.get(CommonParams.Q) != null) {
        String finalQuery;/*from  www  .ja v  a  2s .  c  o  m*/

        String originalQuery = originalParams.get(CommonParams.Q);

        rsp.add("Original query", originalQuery);

        /*** Arafath's code to prepare query starts here
         * The query should be in the following format ->
         * Example : 
         *       Original Query: "Which musical object did russ conway play"
         *      Temporary Query : "relations:instrument AND entity:enty" // Generate the relation
         *       Final Query : "name:"russ conway" AND occupation:musician"
         */

        String tempQuery = "relations:instrumental AND entity:enty";
        rsp.add("Temporary query", tempQuery);
        String desiredField = null;
        Set<String> fieldSet = null;
        SchemaField keyField = null;

        NamedList tempparamsList = req.getParams().toNamedList();
        tempparamsList.setVal(tempparamsList.indexOf(CommonParams.Q, 0), tempQuery);
        psuedoParams = SolrParams.toSolrParams(tempparamsList);
        //      if (psuedoParams.get(CommonParams.SORT) == null) {
        //         tempparamsList.add(CommonParams.SORT, "score desc");
        //      } else {
        //         tempparamsList.setVal(tempparamsList.indexOf(CommonParams.SORT, 0), "score desc");
        //      }

        SolrQueryRequest firstreq = new LocalSolrQueryRequest(req.getCore(), tempparamsList);
        SolrQueryResponse firstrsp = new SolrQueryResponse();
        firstrsp.setAllValues(rsp.getValues());
        ResponseBuilder firstrb = new ResponseBuilder(firstreq, firstrsp, components);

        for (SearchComponent c : components) {
            c.prepare(firstrb);
            c.process(firstrb);
        }
        rsp.add("response", firstrb.getResults().docList);
        /***
                 DocList docs = firstrb.getResults().docList;
                 if (docs == null || docs.size() == 0) {
                    log.debug("No results");
                 } else {
                    fieldSet = new HashSet <String> ();
                    keyField = firstrb.req.getCore().getLatestSchema().getUniqueKeyField();
                    if (keyField != null) {
                       fieldSet.add(keyField.getName());
                    }
                    fieldSet.add("fieldid");
                    fieldSet.add("relations");
                    fieldSet.add("entity");
                    fieldSet.add("count");
                    NamedList docresults = new NamedList();
                    DocIterator iterator = docs.iterator();
                    Document doc;
                    int docScore = 0;
                    rsp.add("doc retrieved ", docs.size());
                    for (int i=0; i<docs.size(); i++) {
                       try {
          int docid = iterator.nextDoc();
          doc = firstrb.req.getSearcher().doc(docid, fieldSet);
          if (Integer.parseInt(doc.get("count")) > docScore) {
             docScore = Integer.parseInt(doc.get("count"));
             desiredField = doc.get("fieldid");
          }
          docresults.add(String.valueOf(docid), doc);
                       } catch (IOException ex) {
          java.util.logging.Logger.getLogger(CustomQueryComponent.class.getName()).log(Level.SEVERE, null,ex);
                       }
                    }
                    fieldSet.clear();
                    rsp.add("Intermediate results", docresults);
                    if (desiredField != null) {
                       rsp.add("Required Field", desiredField);
                    }
                 } ***/

        firstreq.close();

        /*** Final Phase starts here ***/
        /***   finalQuery = "name:\"russ conway\" AND occupation:musician";
              NamedList finalparamsList = req.getParams().toNamedList();
              finalparamsList.setVal(finalparamsList.indexOf(CommonParams.Q, 0), finalQuery);
              psuedoParams = SolrParams.toSolrParams(finalparamsList);
              if (psuedoParams.get(CommonParams.SORT) == null) {
                 finalparamsList.add(CommonParams.SORT, "score desc");
              } else {
                 finalparamsList.setVal(finalparamsList.indexOf(CommonParams.SORT, 0), "score desc");
              }   
           //   if (desiredField != null) {
           //      if (psuedoParams.get(CommonParams.FL) != null) {
           //         finalparamsList.setVal(finalparamsList.indexOf(CommonParams.FL, 0), desiredField);
           //      } else {
           //         finalparamsList.add(CommonParams.FL, desiredField);
           //      }
           //   }
                
              SolrQueryRequest finalreq = new LocalSolrQueryRequest(req.getCore(), finalparamsList);
              rsp.add("Final Query", finalreq.getParams().get(CommonParams.Q));
              ResponseBuilder rb = new ResponseBuilder(finalreq,rsp,components);
              for (SearchComponent c : components) {
                 c.prepare(rb);
                 c.process(rb);
              } ***/
        /*** testing
                 DocList finaldocs = rb.getResults().docList;
                 if (finaldocs == null || finaldocs.size() == 0) {
                    log.debug("No results");
                 } else {
                    keyField = rb.req.getCore().getLatestSchema().getUniqueKeyField();
                    if (keyField != null) {
                       fieldSet.add(keyField.getName());
                    }
                    if (desiredField != null) {
                       fieldSet.add(desiredField);
                    }
                    fieldSet.add("name");
                    NamedList finaldocresults = new NamedList();
                    DocIterator finaliterator = finaldocs.iterator();
                    Document finaldoc;
                    rsp.add("finaldocs retrieved ", finaldocs.size());
                    for (int i=0; i<docs.size(); i++) {
                       try {
          if (finaliterator.hasNext()) {
             int finaldocid = finaliterator.nextDoc();
             finaldoc = rb.req.getSearcher().doc(finaldocid, fieldSet);
             finaldocresults.add(String.valueOf(finaldocid), finaldoc);
          }
                       } catch (IOException ex) {
          java.util.logging.Logger.getLogger(MySearchHandler.class.getName()).log(Level.SEVERE, null,ex);
                       }
                    }
                    rsp.add("final results", finaldocresults);
                 } ***/
        //   finalreq.close(); 
    } else {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
                "Need to give at least one word as query!");
    }
}

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

License:Apache License

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}