elastic / elasticsearch-rs

Official Elasticsearch Rust Client
https://www.elastic.co/guide/en/elasticsearch/client/rust-api/current/index.html
Apache License 2.0
705 stars 72 forks source link

[ENHANCEMENT] Provide non-array based SearchParts for searching single a index #149

Open oeed opened 3 years ago

oeed commented 3 years ago

Is your feature request related to a problem? Please describe. 95% of my queries are only ever on a single index and I've found it a bit difficult/unergonomic to try and search for these when they're not 'static. Because SearchParts::Index takes &[&str] you have to wrap the single string like &[my_str], which doesn't work in situation where you return the SearchParts by value, not reference, like this (it's a bit of a contrived example, but I've run in to it in various situations):

pub trait Index {
  fn name(&self) -> &str;

  fn search_parts<'a>(&'a self) -> SearchParts<'a> {
    SearchParts::Index(&[self.name()])
  }
}

You get a cannot return value referencing temporary value, returns a value referencing data owned by the current function error. My index names are dynamic and so can't be static, this becomes an issue because the variant requires a reference to an array of references, which I simply can't figure out how to do within a single struct.

I'm still a bit new to Rust though, so if there's actually a good solution to this I'd love to know, I haven't been able to find one though.

Describe the solution you'd like I'm far from a Rust expert as I've said, so there may be better ways; but the idea that comes to mind is to simply providing additional variants.

i.e.

pub enum SearchParts<'b> {
    ...
    SingleIndex(&'b str)
}

The implementation of SearchParts looks fairly straight forward so I'd be happy to provide a PR if open to the idea.

Describe alternatives you've considered With a few layers of indirection and nested structs this is possible, but it's rather messy and unergonomic.

russcam commented 3 years ago

Hi @oeed, I think #126 is going in the direction that you're after. Ideally, you'd be able to do

let response = client
    .search("my_index")
    .q("title:Elasticsearch")
    .send()
    .await?;

In its current form this isn't yet possible, the nearest being

let response = client
    .search(&["my_index"][..])
    .q("title:Elasticsearch")
    .send()
    .await?;

It'd be good to revisit, to see how we might go about this. I think the parts enums will likely end up owning their inputs for this to work.