2013-03-11 16 views
6

Durante l'uso di SOLRJ mi piacerebbe sapere come posso convertire l'oggetto SolrQuery nella sua rappresentazione URL con la sintassi della query SOLR. Ho provato ad usare il metodo .toString() ma non restituisce la corretta rappresentazione della query. C'è un altro modo come farlo?Come posso trasformare SolrQuery (SOLRJ) in URL?

risposta

7

Raccomando ClientUtils.toQueryString per questo argomento.

@Test 
public void solrQueryToURL() { 
    SolrQuery tmpQuery = new SolrQuery("some query"); 
    Assert.assertEquals("?q=some+query", ClientUtils.toQueryString(tmpQuery, false)); 
} 

All'interno del codice sorgente di HttpSolrServer si può vedere che questo è usato dallo stesso codice di Solrj per questo motivo.

public NamedList<Object> request(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException { 

    // ... other code left out 

    if(SolrRequest.METHOD.GET == request.getMethod()) { 
    if(streams != null) { 
     throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!"); 
    } 
    method = new HttpGet(baseUrl + path + ClientUtils.toQueryString(params, false)); 

    // ... other code left out 

    } 
1

SolrJ (testato la versione 6.6.0) è:

@Test 
public void solrQueryToURL() { 
    SolrQuery query = new SolrQuery("query"); 
    Assert.assertEquals("?q=query", query.toQueryString()); 
} 
Problemi correlati