1

I'm having a solr query "http://localhost:8080/solr/select/?q=A".I need to read the parameter "q" in my java code.How can I get this?

Thanks, Marshal

1
  • What kind of Java code is at /select part of the URL? Servlet? JSP? Filter? Commented Mar 9, 2011 at 12:23

3 Answers 3

7

request.getParameter("q"); would do the trick.

Sign up to request clarification or add additional context in comments.

1 Comment

I think i should be more specific.I'm trying to customizing QueryElevationComponent.java of solr. In that java file i need to get the q parameter.When i'm trying to use 'request.getParameter("q")',it showing error.Is there any packages that i should import or should i have to extend class?
1

If you have the query in URL form, you can use URL.getQuery(), and then would have to split the string at = and & to find the right element, like this:

public String getQueryPart(URL url, String key) {
    String query = url.getQuery();
    if(query == null)
       return null;
    String[] parts = query.split("[&=]");
    for(int i = 0; i < parts.length; i+=2) {
       if(parts[i].equals(key)) {
          return parts[i+1];
       }
    }
}

If you want to query multiple parameters, better store the split String once in a map and query this multiple times.

And of course, if you are using this in a servlet or similar server-side code called by this exactly URL, there are better ways to get the parameters in the servlet API (like Deepak wrote).

2 Comments

I think this is not the best way to do this since already the servlet api has a clean way from doing this. I suggest use the answer of @Deepak
The question did not really say he wants to do this in the code called by this URL - of course, if it is so, better use Deepaks answer.
0

Depends on you context.
From where are you trying to get this param? How are you using Solr exactly?

BTW, /select is simply a part of a URL used to map requests to the main Solr servlet called (wait for it...) SolrServlet - org.apache.solr.servlet.SolrServlet (deprecated from v4, now solr.StandardRequestHandler is used), which is, due to the fact that it handles search requests, also sometimes referred to as Solr Server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.