lucene_query_builder

Crates.iolucene_query_builder
lib.rslucene_query_builder
version0.3.0
sourcesrc
created_at2019-12-12 12:55:45.814341
updated_at2021-07-14 10:40:23.965188
descriptionA procmacro derive crate to generate lucene query builder for Rust structs :
homepage
repositoryhttps://github.com/oknozor/lucene_query_builder_rs
max_upload_size
id188790
size7,752
Paul Delafosse (oknozor)

documentation

https://docs.rs/lucene_query_builder

README

Lucene Query Builder Derive

A procmacro derive crate to generate lucene query builder for Rust structs :

Usage :

use lucene_query_builder::QueryBuilder;

#[derive(QueryBuilder)]
struct Person {
    name: String,
    age: i32,
}

Simple query

let query = Person::query_builder()
    .name("Bob")
    .or()
    .name("Alice")
    .build();

assert_eq!(query, "query=name:Bob OR name:Alice".to_string());

Nested query

let query = Person::query_builder()
    .expr(Person::query_builder().name("Bob").or().name("Alice"))
    .and()
    .age("22")
    .build();

assert_eq!(
    query,
    "query=(name:Bob OR name:Alice) AND age:22".to_string()
);

Range query

let query = Person::query_builder().age_range("7", "77").build();

assert_eq!(query, "query=age:[7 TO 77]".to_string());

Rename builder functions and field names

Use #[query_builder_rename = "name"] if you need to rename the generated method :

#[derive(QueryBuilder)]
struct Person {
    name: String,
    age: i32,
    #[query_builder_rename = "fullname"]
    complete_name: String,
}
let query = Person::query_builder()
    .name("Bob")
    .and()
    .fullname("Bob Marley")
    .build();

assert_eq!(
    query,
    "query=name:Bob AND complete_name:\"Bob Marley\"".to_string()
);

If you need to rename the generated lucene field you can use #[query_builder_field = "name"].

#[derive(QueryBuilder)]
struct Person {
    name: String,
    age: i32,
    #[query_builder_rename = "fullname"]
    #[query_builder_field = "fullname"]
    complete_name: String,
}
let query = Person::query_builder()
    .name("Bob")
    .and()
    .fullname("Bob Marley")
    .build();

assert_eq!(
    query,
    "query=name:Bob AND fullname:\"Bob Marley\"".to_string()
);

Ignore fields

If you need to ignore a field use the attribute macro : #[query_builder_ignore]

Commit count: 21

cargo fmt