#query-builder #lucene #structs

lucene_query_builder

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

7 releases

0.3.0 Jul 14, 2021
0.2.4 Mar 11, 2021
0.2.2 Dec 18, 2019
0.1.0 Dec 12, 2019

#866 in Rust patterns

Download history 183/week @ 2024-06-17 141/week @ 2024-06-24 82/week @ 2024-07-01 124/week @ 2024-07-08 71/week @ 2024-07-15 67/week @ 2024-07-22 105/week @ 2024-07-29 97/week @ 2024-08-05 162/week @ 2024-08-12 154/week @ 2024-08-19 141/week @ 2024-08-26 138/week @ 2024-09-02 129/week @ 2024-09-09 85/week @ 2024-09-16 119/week @ 2024-09-23 103/week @ 2024-09-30

442 downloads per month
Used in 5 crates (3 directly)

MIT license

6KB
50 lines

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]

Dependencies

~1.5MB
~35K SLoC