Skip to content
This repository has been archived by the owner on Aug 7, 2021. It is now read-only.

Commit

Permalink
src/server: add endpoint to remove a single document
Browse files Browse the repository at this point in the history
  • Loading branch information
RoxasShadow committed Aug 25, 2016
1 parent 12a8940 commit 24dfef1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use serde::ser::Serialize;

use rs_es::Client;
use rs_es::operations::index::IndexResult;
use rs_es::operations::mapping::*;
use rs_es::operations::delete::DeleteResult;
use rs_es::operations::mapping::MappingResult;
use rs_es::error::EsError;

use params::*;
Expand All @@ -20,6 +21,9 @@ pub trait Resource: Send + Sync + Any + Deserialize + Debug {
/// Respond to POST requests indexing given entity
fn index(&self, mut es: &mut Client, index: &str) -> Result<IndexResult, EsError>;

/// Respond to DELETE requests deleting all the entities from given index
/// Respond to DELETE requests on given id deleting it from given index
fn delete(mut es: &mut Client, id: &str, index: &str) -> Result<DeleteResult, EsError>;

/// Respond to DELETE requests rebuilding and reindexing given index
fn reset_index(mut es: &mut Client, index: &str) -> Result<MappingResult, EsError>;
}
21 changes: 21 additions & 0 deletions src/resources/talent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::rs_es::Client;
use super::rs_es::query::Query;
use super::rs_es::operations::search::{Sort, SortField, Order};
use super::rs_es::operations::index::IndexResult;
use super::rs_es::operations::delete::DeleteResult;
use super::rs_es::operations::mapping::*;
use super::rs_es::query::full_text::MatchQueryType;
use super::rs_es::error::EsError;
Expand Down Expand Up @@ -306,6 +307,12 @@ impl Resource for Talent {
}
}

/// Delete the talent associated to given id.
fn delete(mut es: &mut Client, id: &str, index: &str) -> Result<DeleteResult, EsError> {
es.delete(index, ES_TYPE, id)
.send()
}

/// Reset the given index. All the data will be destroyed and then the index
/// will be created again. The map that will be used is hardcoded.
#[allow(unused_must_use)]
Expand Down Expand Up @@ -635,8 +642,10 @@ mod tests {
#[test]
fn test_search() {
let mut client = make_client();

assert!(Talent::reset_index(&mut client, &*config.es.index).is_ok());
refresh_index(&mut client);

assert!(populate_index(&mut client));
refresh_index(&mut client);

Expand All @@ -647,6 +656,18 @@ mod tests {
assert!(results.highlights().iter().all(|r| r.is_none()));
}

{
assert!(Talent::delete(&mut client, "1", &*config.es.index).is_ok());
assert!(Talent::delete(&mut client, "4", &*config.es.index).is_ok());
refresh_index(&mut client);

let results = Talent::search(&mut client, &*config.es.index, &Map::new());
assert_eq!(vec![5, 2], results.ids());

assert!(populate_index(&mut client));
refresh_index(&mut client);
}

// a non existing index is given
{
let mut map = Map::new();
Expand Down
41 changes: 40 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,37 @@ impl<R: Resource> Handler for IndexableHandler<R> {
}
}

pub struct DeletableHandler<R> {
config: Config,
resource: PhantomData<R>
}

impl<R: Resource> DeletableHandler<R> {
fn new(config: Config) -> Self {
DeletableHandler::<R> { resource: PhantomData, config: config }
}
}

impl<R: Resource> WritableEndpoint for DeletableHandler<R> {}

impl<R: Resource> Handler for DeletableHandler<R> {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
if !self.is_authorized(self.config.auth.to_owned(), &req.headers) {
unauthorized!();
}

let mut client = Client::new(&*self.config.es.host, self.config.es.port);
let ref id = try_or_422!(req.extensions.get::<Router>().unwrap()
.find("id")
.ok_or("DELETE#:id not found"));

match R::delete(&mut client, id, &*self.config.es.index) {
Ok(_) => Ok(Response::with(status::NoContent)),
Err(_) => Ok(Response::with(status::UnprocessableEntity))
}
}
}

pub struct ResettableHandler<R> {
config: Config,
resource: PhantomData<R>
Expand Down Expand Up @@ -208,6 +239,9 @@ impl<R: Resource> Server<R> {
router.post(&self.endpoint, IndexableHandler::<R>::new(self.config.to_owned()));
router.delete(&self.endpoint, ResettableHandler::<R>::new(self.config.to_owned()));

let deletable_endpoint = format!("{}/:id", self.endpoint);
router.delete(deletable_endpoint, DeletableHandler::<R>::new(self.config.to_owned()));

match env::var("DYNO") { // for some reasons, chain::link makes heroku crash
Ok(_) => Iron::new(router).http(&*host),
Err(_) => {
Expand All @@ -227,7 +261,8 @@ mod tests {

use rs_es::Client;
use rs_es::operations::index::IndexResult;
use rs_es::operations::mapping::*;
use rs_es::operations::delete::DeleteResult;
use rs_es::operations::mapping::{MappingOperation, MappingResult};
use rs_es::error::EsError;

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand All @@ -251,6 +286,10 @@ mod tests {
.send()
}

fn delete(mut es: &mut Client, id: &str, index: &str) -> Result<DeleteResult, EsError> {
es.delete(index, ES_TYPE, id)
.send()
}

fn reset_index(mut es: &mut Client, index: &str) -> Result<MappingResult, EsError> {
MappingOperation::new(&mut es, index).send()
Expand Down

0 comments on commit 24dfef1

Please sign in to comment.