public static Response GetItemsByName(string name, string league, int from, int size, ElasticsearchClient client) { string query = QueryStringItemByName(name, league, from, size); var result = client.Search<string>(query); Response response = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Response>(result.Response); return response; }
static public bool ShouldAdd(ElasticsearchClient client, ref CandidateFile candidate) { var searchQuery = new { query = new { term = new { _id = candidate.AliasedPath } } }; ElasticsearchResponse<string> response; var timer = new ScopedTimer(); using (timer) { response = client.Search<string>(Media.IndexName, JsonConvert.SerializeObject(searchQuery)); } if (!FirstSearchMsec.HasValue) { FirstSearchMsec = timer.TotalMilliseconds; } if (!response.Success) { response.LogFailure(logger); return false; } candidate.Signature = CalculateSignature(candidate.FullFilename); candidate.LengthInBytes = new FileInfo(candidate.FullFilename).Length; var searchResponse = JsonConvert.DeserializeObject<SearchResponse>(response.Response); // if ((timer.TotalMilliseconds - searchResponse.Took) > 200) // { // logger.Warn("Search took {1} milliseconds for: {0}", candidate.AliasedPath, timer.TotalMilliseconds); // } // If there's more than one matching path or the signature is different, go through the add process // If it's not already in the index, add it. if (searchResponse.Hits.Total != 1) return true; var media = searchResponse.Hits.Hits.First().Media; if (media.Signature != candidate.Signature || media.LengthInBytes != candidate.LengthInBytes) { logger.Error("Add {0} due to mis-match ({0}, {1} -- {2}, {3})", candidate.AliasedPath, media.Signature, media.LengthInBytes, candidate.Signature, candidate.LengthInBytes); return true; } return false; }
public void Calling_A_Null_Field_Should_Not_Throw_With_ElasticsearchNet_Serializer() { var client = new ElasticsearchClient(); var result = client.Search("{ size: 10}"); var hit = result.Response["hits"]["hits"][0]; Assert.DoesNotThrow(() => { var x = hit["testfield"]; }); var exists = false; Assert.DoesNotThrow(() => { exists = hit["testfield"] != null; }); exists.Should().BeFalse(); Assert.DoesNotThrow(() => { exists = hit["_index"] != null; }); exists.Should().BeTrue(); var source = hit["_source"]; ((object) source).Should().NotBeNull(); Assert.DoesNotThrow(() => { var x = source["name"]; }); string field = source["name"]; field.Should().NotBeNullOrWhiteSpace(); }
/** * The following is a list of available connection configuration options: */ public void AvailableOptions() { //hide var client = new ElasticsearchClient(); //endhide var config = new ConnectionConfiguration() .DisableAutomaticProxyDetection() /** Disable automatic proxy detection. Defaults to true. */ .EnableHttpCompression() /** * Enable compressed request and reesponses from Elasticsearch (Note that nodes need to be configured * to allow this. See the [http module settings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-http.html) for more info). */ .DisableDirectStreaming() /** * By default responses are deserialized off stream to the object you tell it to. * For debugging purposes it can be very useful to keep a copy of the raw response on the result object. */; var result = client.Search<SearchResponse<object>>(new { size = 12 }); var raw = result.ResponseBodyInBytes; /** This will only have a value if the client configuration has ExposeRawResponse set */ /** * Please note that this only make sense if you need a mapped response and the raw response at the same time. * If you need a `string` or `byte[]` response simply call: */ var stringResult = client.Search<string>(new { }); //hide config = config //endhide .GlobalQueryStringParameters(new NameValueCollection()) /** * Allows you to set querystring parameters that have to be added to every request. For instance, if you use a hosted elasticserch provider, and you need need to pass an `apiKey` parameter onto every request. */ .Proxy(new Uri("http://myproxy"), "username", "pass") /** Sets proxy information on the connection. */ .RequestTimeout(TimeSpan.FromSeconds(4)) /** * Sets the global maximum time a connection may take. * Please note that this is the request timeout, the builtin .NET `WebRequest` has no way to set connection timeouts * (see http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout(v=vs.110).aspx). */ .ThrowExceptions() /** * As an alternative to the C/go like error checking on `response.IsValid`, you can instead tell the client to throw * exceptions. * * There are three category of exceptions thay may be thrown: * * 1) ElasticsearchClientException: These are known exceptions, either an exception that occurred in the request pipeline * (such as max retries or timeout reached, bad authentication, etc...) or Elasticsearch itself returned an error (could * not parse the request, bad query, missing field, etc...). If it is an Elasticsearch error, the `ServerError` property * on the response will contain the the actual error that was returned. The inner exception will always contain the * root causing exception. * * 2) UnexpectedElasticsearchClientException: These are unknown exceptions, for instance a response from Elasticsearch not * properly deserialized. These are usually bugs and should be reported. This excpetion also inherits from ElasticsearchClientException * so an additional catch block isn't necessary, but can be helpful in distinguishing between the two. * * 3) Development time exceptions: These are CLR exceptions like ArgumentException, NullArgumentException etc... that are thrown * when an API in the client is misused. These should not be handled as you want to know about them during development. * */ .PrettyJson() /** * Forces all serialization to be indented and appends `pretty=true` to all the requests so that the responses are indented as well */ .BasicAuthentication("username", "password") /** Sets the HTTP basic authentication credentials to specify with all requests. */; /** * **Note:** This can alternatively be specified on the node URI directly: */ var uri = new Uri("http://*****:*****@localhost:9200"); var settings = new ConnectionConfiguration(uri); /** * ...but may become tedious when using connection pooling with multiple nodes. */ }
public async Task SearchAllElasticSearch() { var node = new Uri("http://localhost:9200"); var settings = new ConnectionConfiguration(node); var client = new ElasticsearchClient(settings); var query = "{\"query\" : { \"term\": { \"name\" : \"safale\"}}}"; var res = client.Search<string>(Setting.ElasticSearchIndex, query); dynamic json = JsonConvert.DeserializeObject(res.Response); var result = json.hits.hits; }
/** * The following is a list of available connection configuration options: */ public void AvailableOptions() { //hide var client = new ElasticsearchClient(); //endhide var config = new ConnectionConfiguration() .DisableAutomaticProxyDetection() /** Disable automatic proxy detection. Defaults to true. */ .EnableHttpCompression() /** * Enable compressed request and reesponses from Elasticsearch (Note that nodes need to be configured * to allow this. See the [http module settings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-http.html) for more info). */ .EnableMetrics() /** Enable more meta data to be returned per API call about requests (ping, sniff, failover, and general stats). */ .EnableTrace() /** * Will cause `Elasticsearch.Net` to write connection debug information on the TRACE output of your application. */ .DisableDirectStreaming() /** * By default responses are deserialized off stream to the object you tell it to. * For debugging purposes it can be very useful to keep a copy of the raw response on the result object. */; var result = client.Search<SearchResponse<object>>(new { size = 12 }); var raw = result.ResponseBodyInBytes; /** This will only have a value if the client configuration has ExposeRawResponse set */ /** * Please note that this only make sense if you need a mapped response and the raw response at the same time. * If you need a `string` or `byte[]` response simply call: */ var stringResult = client.Search<string>(new { }); //hide config = config //endhide .SetConnectionStatusHandler(s => { }) /** * Allows you to pass a `Action<IElasticsearchResponse>` that can eaves drop every time a response (good or bad) is created. If you have complex logging needs * this is a good place to add that in. */ .SetGlobalQueryStringParameters(new NameValueCollection()) /** * Allows you to set querystring parameters that have to be added to every request. For instance, if you use a hosted elasticserch provider, and you need need to pass an `apiKey` parameter onto every request. */ .SetProxy(new Uri("http://myproxy"), "username", "pass") /** Sets proxy information on the connection. */ .RequestTimeout(TimeSpan.FromSeconds(4)) /** * Sets the global maximum time a connection may take. * Please note that this is the request timeout, the builtin .NET `WebRequest` has no way to set connection timeouts * (see http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout(v=vs.110).aspx). */ //TODO document this properly once we figure out exceptions .ThrowExceptions() /** * As an alternative to the C/go like error checking on `response.IsValid`, you can instead tell the client to always throw * an `ElasticsearchServerException` when a call resulted in an exception on the Elasticsearch server. Reasons for * such exceptions could be search parser errors and index missing exceptions. */ .PrettyJson() /** * Forces all serialization to be indedented and appends `pretty=true` to all the requests so that the responses are indented as well */ .SetBasicAuthentication("username", "password") /** Sets the HTTP basic authentication credentials to specify with all requests. */; /** * **Note:** This can alternatively be specified on the node URI directly: */ var uri = new Uri("http://*****:*****@localhost:9200"); var settings = new ConnectionConfiguration(uri); /** * ...but may become tedious when using connection pooling with multiple nodes. */ }