Search code examples
solrsolrj

search in solrj by particular id


I have a list of Id's and I want to search a document based on those Id and the format of Id is like this -

id:<382-84623-87309-184579-JHSAG@gmail.com>2016-04-22T21:12:30Z

I wrote a code-

SolrQuery query = new SolrQuery();
query.setQuery("id:<382-84623-87309-184579-JHSAG@gmail.com>2016-04-22T21:12:30Z");
query.setFields("id","from","subject");

QueryResponse response = server.query(query);  // server is a CloudSolrServer object

SolrDocumentList results = response.getResults();

for(i=0;i<results.size();i++) {
    system.out.println(results.get(i);
}

But I am getting error

org.apache.solr.common.SolrException: org.apache.solr.search.SyntaxError: Cannot parse 'name:': Encountered "<EOF>" at line 1, column 5.
Was expecting one of:
    <BAREOPER> ...
    "(" ...
    "*" ...
    <QUOTED> ...
    <TERM> ...
    <PREFIXTERM> ...
    <WILDTERM> ...
    <REGEXPTERM> ...
    "[" ...
    "{" ...
    <LPARAMS> ...
    <NUMBER> ...

I tried to add the escape character in Id value in this line

query.setQuery("id:<382-84623-87309-184579-JHSAG@gmail.com>2016-04-22T21:12:30Z");

but it doesn't find any document.. Can somebody please help me to write above line using escape character Or suggest some other way to achieve it.

FYI, I found 1 link on stackoverflow but that doesn't help me, I don't have that link now to paste it here.

Solr Version: 4.10.3


Solution

  • how to define field id in your solr's schema ?

    if your id is string field , then use double quote surround your search keyword , if your id is text field , then use lucene's escape in your keyword

    QueryParser.escape("")