5 unstable releases

0.3.2 Apr 1, 2024
0.3.1 Nov 7, 2023
0.3.0 Sep 14, 2023
0.2.0 Sep 6, 2023
0.1.0 Aug 31, 2023

#155 in HTTP client

Download history 24/week @ 2024-07-23 11/week @ 2024-07-30 4/week @ 2024-09-24 1/week @ 2024-10-01

209 downloads per month

MIT license

58KB
1K SLoC

RSolr

A Solr client for Rust.

Rsolr provides capabilities to manipulate and form requests to the Solr server, and contains some shorthands for them, with support for some facet features and query builder. It uses the blocking version of the reqwest http client.

Select

You can retrieve documents as types with implemented Clone and Deserialize.

use serde_json::Value;
use rsolr::Client;
use rsolr::error::RSolrError;
use rsolr::solr_result::SolrResponse;

fn query_all() -> Result<SolrResponse<Value>, RSolrError> {
    let result = Client::new("http://solr:8983", "collection")
        .select("*:*")
        .run::<Value>();
    match result {
        Ok(solr_result) => Ok(solr_result.expect("Request is OK, but no response; in select it's a failure on Solr side.")),
        Err(e) => Err(e)
    }
}

Upload JSON doc(s)

You should use types with implemented Clone and Serialize.


use serde::Serialize;
use serde_json::Value;
use rsolr::Client;

#[derive(Serialize, Clone)]
struct SimpleDocument {
    field: Vec<String>
}

fn upload() {
    let document = SimpleDocument { field: vec!("nice".to_string(), "document".to_string()) };
    Client::new("http://solr:8983", "collection")
        .upload_json(document)
        .run().expect("request failed.");
}

Delete

use serde_json::Value;
use rsolr::Client;
fn delete() {
    Client::new("http://solr:8983", "collection")
        .delete("delete:query")
        .run::<Value>().expect("panic, request failed.");
}

Custom handler with params

You can define any handlers as well.


use serde_json::Value;
use rsolr::Client;
use rsolr::error::RSolrError;
use rsolr::solr_result::SolrResponse;
fn more_like_this()  -> Result<SolrResponse<Value>, RSolrError> {
    let result = Client::new("http://solr:8983", "collection")
        .request_handler("mlt")
        .add_query_param("mlt.fl", "similarity_field")
        .add_query_param("mlt.mintf", "4")
        .add_query_param("mlt.minwl", "3")
        .run::<Value>();
    match result {
        Ok(solr_result) => Ok(solr_result.expect("Request is OK, but no response; in select it's a failure on Solr side.")),
        Err(e) => Err(e)
    }
}

Cursor-based pagination

Paginated results can be fetched iteratively with the use of solr cursor

use serde_json::Value;
use rsolr::Client;
use rsolr::solr_response::SolrResponse;
fn cursor_fetch_all_pages() -> Vec<SolrResponse<Value>> {
    let mut responses = Vec::new();
    let mut client = Client::new("http://solr:8983", "collection");
    let result = client
        .select("*:*")
        .sort("id asc")
        .cursor()
        .run();
    let mut cursor = result.expect("request failed").expect("no cursor");
    while cursor.next::<Value>().expect("request failed").is_some() {
        responses.push(cursor.get_response::<Value>().expect("parsing failed"));
    }
    responses
}

Development

I use Cargo Run Script to setup and manage a Solr locally, specified the latest Solr to stay up-to-date. Solr 8+ is supported. You'll also need a Docker. After checkout you should run

cargo run-script solr-start
cargo run-script solr-provision

Now you can reach your local Solr on http://localhost:8983. For testing I created a default collection without any schema def. Practically it means every value will be multivalue by default.

Dependencies

~7–19MB
~276K SLoC