creating ISearchResponse<T> from json string for unit test - elasticsearch

is there a way I can create ISearchResponse to simulate return by elastic search from JSON string? I need to write unittests for my API. The API building the query has histogram, date filters etc and hence response will be as per that and I want to simulate that.
Thanks

You can deserialize json into an instance of ISearchResponse<T> with
ISearchResponse<T> searchResponse = null;
using (var stream = File.OpenRead("path-to-json.json"))
{
searchResponse = client.Serializer.Deserialize<SearchResponse<T>>(stream);
}
If this is stub data, I'd be more inclined to have a stub implementation of ISearchResponse<T> in code as opposed to deserializing json to create an instance; maybe a little easier to maintain.

Related

Why is elasticsearch's Nest lowlevel Search method ignoring type and index name defined in SearchDescriptor<>() object

NEST/Elasticsearch.Net version:5.6.5
Elasticsearch version:5.4.3
We are trying to fetch result from our index using the LowLevelClient. We are using the below SearchAsync API
var searchDescriptor = new SearchDescriptor<MyType>()
.Type("mytype")
.Index("myindex")
.Query(....)
.Aggregation(ag => ag.Terms(... Aggregation(ag1 => ag1.Min(...TopHits(...)))));
var memoryStream = new MemoryStream();
_client.Serializer.Serialize(searchDescriptor, memoryStream);
var response = await _client.LowLevel.SearchAsync<byte[]>(memoryStream.ToArray()).ConfigureAwait(false);
//_client - instance of Nest.ElasticClient
//Next Step - Deserialize the response
This is giving me results from other indices also(a combination of results from the various indices) and my deserialization is breaking. The client is ignoring type and index name and calling POST /_search API instead of POST /myindex/mytype/_search on the elastic search
Note:
We need to call a lower-level client because we are using a custom deserializer for performance concern
What is the issue here?
Found a workaround
The SearchAsync<>() method have overloaded method _client.LowLevel.SearchAsync<T>(string indexName, string typeName, T t)
Passing the index name and type name will narrow to search to that particular index.
But the question still remains, why it is not taking the index and type name from SearchDescriptor object.

WebAPI OData and skip/paging in DocumentDB/CosmosDB

This question was previously posted here but I am repeating the question and a summary of the discussion here to have a complete question.
I have set up a ODataController in ASP.NET WebAPI with DocumentDB/CosmosDB as a backend. It works quite ok, I return the result of CreateDocumentQuery and $select,$top,$orderby and $filter work fine.
However, $skip does not. I know that this is a planned feature (Vote here) but I would like to know if there is any temporary workaround.
My plan was to have a "continuationToken" parameter to my OData controller action. I would then pass this using FeedOptions.RequestContinuation to CreateDocumentQuery. However, CreateDocumentQuery does not return a response token. I then tried with ReadDocumentFeedAsync, which does return a response token, but it does not return a IQueryable.
This is an example of api code inside a ODataController:
public IQueryable<Result> FindResults(Uri collectionUri, string continuationToken)
{
FeedOptions feedOptions = new FeedOptions();
feedOptions.MaxItemCount = 100;
feedOptions.RequestContinuation = continuationToken;
var query = client.CreateDocumentQuery<Result>(collectionUri, queryString, feedOptions).AsDocumentQuery();
return query as IQueryable<Result>;
}
However, what would the client code look like? I.e. what is the http-request required to keep the IQueryable alive to be able to use the continuationToken when paging?
I guess I could store the result of ReadDocumentFeedAsync in memory and return a IQueryable to that, but is there a better solution?
Did you look at the PageResult object?
For non-OData formats, it is still possible to support next-page links and inline count, by wrapping the query results in a PageResult object. However, it requires a bit more code. Here is an example:
public PageResult<Product> Get(ODataQueryOptions<Product> options)
{
ODataQuerySettings settings = new ODataQuerySettings()
{
PageSize = 5
};
IQueryable results = options.ApplyTo(_products.AsQueryable(), settings);
return new PageResult<Product>(
results as IEnumerable<Product>,
Request.GetNextPageLink(),
Request.GetInlineCount());
}
https://learn.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options

How to Make a PUT request to Elastic Search with a JSON file using REST Template

I am creating a SpringBoot Application, where I need to PUT a JSON schema in Elastic Search. The JSON schema will be in my resources folder(in my classpath). How to PUT the raw JSON file using REST Template.
Any Help?" As most of the example on internet are JUST assuming that we have a POJO class to send. But here I am not aware about the JSON Schema. I need to make the request with the raw JSON file.
Assuming the json schema contains the mapping/settings for an index. Then you can put the mapping like as shown below :
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(index);
// CREATE MAPPING
String mapping_json = new String(Files.readAllBytes(json_mapping_path));
createIndexRequestBuilder.addMapping("my_mapping", mapping_json);
CreateIndexResponse indexResponse = createIndexRequestBuilder.execute().actionGet();
For create index don't worry about index mapping json if you wish your json will not be changed ever you can directly create documents by using this code
for(listObject lObject:list){
XContentBuilder json;
try {
json = XContentFactory.jsonBuilder();
json.startObject();// Main Object Start
json.field(GlobalSearchCosntants.DOCUMENT_ID, lObject.getId());
json.field(GlobalSearchCosntants.DOCUMENT_NAME, lObject.getName());
json.field(GlobalSearchCosntants.DOCUMENT_TYPE, lObject.getType());
json.endObject();// Main Object Start
}catch (IOException e1) {
logger.error("Problem while creating document " + e1.getMessage());
}
client.prepareIndex(INDEX_NAME, GlobalSearchCosntants.INDEX_TYPE, id)
.setSource(json).execute().actionGet();
}

Returning Raw Json in ElasticSearch NEST query

I'm doing a small research about a client for elastic search in .net and I found that NEST is one of the most supported solutions for this matter.
I was looking at Nest's docummentation and I couldn´t find a way to output a raw json from a query and avoid the serialization into an object, because I'm using angularJs in the front end I don´t want to overload the process of sending the information to the client with some unnecessary steps.
......and also I'd like to know how can I overrdide the serialization process?
I found that NEST uses Json.NET which I would like to change for the servicestack json serielizer.
thanks!
Hi Pedro you can do this with NEST
var searchDescriptor = new SearchDescriptor<ElasticSearchProject>()
.Query(q=>q.MatchAll());
var request = this._client.Serializer.Serialize(searchDescriptor);
ConnectionStatus result = this._client.Raw.SearchPost(request);
Assert.NotNull(result);
Assert.True(result.Success);
Assert.IsNotEmpty(result.Result);
This allows you to strongly type your queries, but return the string .Result which is the raw response from elasticsearch as string to your
request can be an object or the string so if you are OK with the internal json serialize just pass searchDescriptor directly
Use RequestResponseSerializer instead of Serializer.
var searchDescriptor = ...;
...
byte[] b = new byte[60000];
using (MemoryStream ms = new MemoryStream(b))
{
this._client.RequestResponseSerializer.Serialize(searchDescriptor , ms);
}
var rawJson = System.Text.Encoding.Default.GetString(b);

Windows phone: what to do with webresponse object?

I am trying to create a simple rss feed reader. I got this webresponse object but how to extract text,links from this?
WebResponse response = request.EndGetResponse(result);
Also,can anyone tell me what is the underlying framework behind all the working of windows phone 8.Is it known as Silverlight?I want to know it so that I can make relevant Google searches and don't bother you people time and again?
It depends on what you want to do with it. You will need to get the Stream and then from there can turn it into a string (or other primitive type), just Json.Net to convert from json to an object, or you can use the stream to create an image.
using (var response = webRequest.EndGetResponse(asyncResult))
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
// need a string?
string result = reader.ReadToEnd();
// Need to convert from json?
MyObject obj = Newtonsoft.Json.JsonConvert.DeserializeObject<MyObject>(result);
}
}

Resources