anchor.diff:  
   
   %% @doc
  -%% Function: process_search/3
  -%% Purpose: Does search for Datapoints for either search done with POST or GET
  -%% Returns: {true, ReqData, State} || {{error, Reason}, ReqData, State}
  -
  -%the POST range query should be structed as follows:
  -%	curl -XPOST http://localhost:8000/streams/1/data/_search -d '{
  -%		"size" : 100,
  -%		query:{
  -% 		   "filtered" : {
  -% 		       "query" : {
  -%  		          "term" : { "streamid" : Id }
  -%  		      }, "filter" : {  "range" : {    "timestamp" : {"gte" : timestampFromValue,"lte" : timestampToValue}}}}
  -%		 },"sort" : [{"timestamp" : {"order" : "asc"}}]  }'
  -%the GET range query should be structed as follows:
  -%	curl -XGET http://localhost:8000/streams/Id/data/_search    --   to return all the datapoints of the current stream
  -%or	curl -XGET http://localhost:8000/streams/Id/data/_search\?timestampFrom\=timestampFromValue\&timestampTo\=timestampToValue    --   for range query
  -%or 	curl -XGET http://localhost:8000/streams/Id/data/_search\?timestampFrom\=timestampFromValue   --   for lower bounded only range qquery
  -%% @end
  --spec process_search(ReqData::tuple(), State::string(), term()) ->
  -		{list(), tuple(), string()}.
  -process_search(ReqData, State, post) ->
  -		{Json,_,_} = api_help:json_handler(ReqData,State),
  -		case erlastic_search:search_json(#erls_params{},?INDEX, "datapoint", Json) of
  -				{error, Reason} -> {{error,Reason}, wrq:set_resp_body("{\"error\":\""++ atom_to_list(Reason) ++ "\"}", ReqData), State};
  -				{ok,JsonStruct} ->
  -						       FinalJson = lib_json:get_list_and_add_id(JsonStruct),
  -						       {true,wrq:set_resp_body(lib_json:encode(FinalJson),ReqData),State}
  -		end.
  -
  -
  -%% @doc
   %% Function: generate_date/2
   %% Purpose: Used to create a date valid in ES
   %%          from the input which should be the list
  
positive.code: reduce(VirtualStreamId, Streams, TimestampFrom, Function, ReqData, State) ->
  	% (aggregated *) average, min, max, std deviation, count, sum of squares, variance
  	Query = create_query(Function, Streams, TimestampFrom),
     
  	% should also update the connection of vstream <--> streams
  
  	% in the statistics query the I can get the "statistical" field to push
  		case erlastic_search:search_json(#erls_params{},?INDEX, "datapoint", lib_json:to_string(Query)) of
  				{error, Reason} -> {{error,Reason}, wrq:set_resp_body("{\"error\":\""++ atom_to_list(Reason) ++ "\"}", ReqData), State};
  				{ok,JsonStruct} ->
  					%%		check what happens if list is empty, improve with bulk posting
  					%%   	{true,wrq:set_resp_body(lib_json:encode(FinalJson),ReqData),State},
  			erlang:display(lib_json:to_string(JsonStruct)),
  							DatapointsList = lib_json:get_field(JsonStruct, "hits.hits"),
  						
  							NewDatapoints = lists:map(fun(Json) -> 
  														RmId = lib_json:get_field(Json, "_source"),
  														FinalDatapoint = lib_json:replace_field(RmId, "streamid", VirtualStreamId),
  													  	erlastic_search:index_doc(?INDEX, "datapoint", FinalDatapoint)
  													 end, DatapointsList),
  							
  							%%will post all datapoints one by one, improve with bulk posting should be like the following
  %% 							case erlastic_search:bulk_index_docs(?INDEX, "datapoint", NewDatapoints) of
  %% 									{error, Reason} -> {{error,Reason}, wrq:set_resp_body("{\"error\":\""++ atom_to_list(Reason) ++ "\"}", ReqData), State};
  %% 									{ok,List} -> {true, wrq:set_resp_body(lib_json:encode(List), ReqData), State}
  %% 							end
  							{true, wrq:set_resp_body("\"status\":\"ok\"", ReqData), State} %% need to fix message returned
  		end.
  
   
  %% @doc
  
negative.code: 						  	 {"facets.statistics", "{}"},
  						  	 {"facets.statistics.statistical", "{}"},
  						  	 {"facets.statistics.statistical.script", '(Long) _source.value'}]; %I have to get the value here in another way
  			_ -> %		["aggregate", "min" "max" "total(==sum)" ] ->		can also support custom calculations like *,+,-,/, ^(prob)
  					% as above
  					Facet = [{"facets", "{}"},
  							{"facets.tag_price_stats", "{}"},
  