0

I'm new to SOLR and wonder if there is such a concept as query with parameters, like we have in sql select * from my_table where my_field = ?
Is it possible to use similar concept in SOLR, like String query = "my_field:?";
Otherwise how query parameters should be handled with sorlj, if I would like to implement something like this:

public List<Customer> findCustomersByLastName(String lastName) {
SolrQuery query = new SolrQuery();
query.setQuery( "lastName:?" );
...

Cannot believe that string concatenation should be used at this case.

1 Answers1

0

You can search for a term, which will be used for scoring (sorting) the results:

SolrQuery query = new SolrQuery();
query.setQuery( "lastName:konov" );

and you can filter your results using filter queries:

SolrQuery query = new SolrQuery();
query.setQuery( "lastName:konov" );
query.setParam("fq", "location:Russia OR location:USA");
Artem Lukanin
  • 556
  • 3
  • 15
  • Thank you, but it's more or less clear. I'm curious about parameterized queries. Actually I would like to change the value of lastName dynamically, not hard code it, do like in SQL. It feels like it's not possible at all and at this case I need to invent my own wheel in order to avoid using `query.setQuery( "lastName:" + lastName );` – vladimir konov Sep 10 '13 at 07:36
  • I don't think the question relates to Solr then. It's about formatting Java strings. Possibly [this will answer your question](http://stackoverflow.com/questions/3695230/how-to-use-java-string-format). – Artem Lukanin Sep 10 '13 at 08:05
  • Oh, probably Solr word is too different from SQL, and maybe I'm really asking something irrelevant, but what I'm trying to achieve is something like: `PreparedStatement st= con.prepareStatement("select * from my_table where my_field = ?"); st.setString(1,"Anders"); st.executeQuery();` And don't write escape syntax by myself... – vladimir konov Sep 10 '13 at 08:34
  • Looks like I found something that can solve the problem - Spring Data Solr, it allows to write queries like `@Query("inStock:?0") List findByAvailable(Boolean available);`. Going to investigate further. – vladimir konov Sep 10 '13 at 08:44