本文整理汇总了C#中Nest.ElasticClient.SearchAsync方法的典型用法代码示例。如果您正苦于以下问题:C# ElasticClient.SearchAsync方法的具体用法?C# ElasticClient.SearchAsync怎么用?C# ElasticClient.SearchAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Nest.ElasticClient
的用法示例。
在下文中一共展示了ElasticClient.SearchAsync方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Get
public async Task<IEnumerable<Message>> Get(string q, int from, int size)
{
var url = ConfigurationManager.AppSettings["ES_URL"];
var setting = new ConnectionSettings(new Uri(url));
var client = new ElasticClient(setting);
// Parse the search query
string[] terms = q.Split(' ');
var personTerm = terms.SingleOrDefault(x => x.StartsWith("person:"));
if (!string.IsNullOrEmpty(personTerm))
{
terms = terms.Except(new string[] { personTerm }).ToArray();
personTerm = personTerm.Replace("person:", string.Empty);
}
var textTerms = string.Join(" ", terms);
var searchResults = await client.SearchAsync<Message>(s => s
.AllIndices()
.AllTypes()
.Size(size)
.From(from)
.SortAscending(f => f.Date)
.Query(qry =>
(qry.Term("from", personTerm) ||
qry.Term("to", personTerm)) &&
qry.Match(m => m
.OnField(f => f.Text)
.Query(textTerms)
.Operator(Operator.And))));
return searchResults.Documents;
}
示例2: SearchUsingSingleClient
public void SearchUsingSingleClient(string indexName, int port, int numberOfSearches)
{
var settings = this.CreateSettings(indexName, port);
var client = new ElasticClient(settings);
var tasks = new List<Task>();
for (var p = 0; p < numberOfSearches; p++)
{
var t = client.SearchAsync<Message>(s => s.MatchAll())
.ContinueWith(ta =>
{
if (!ta.Result.IsValid)
throw new ApplicationException(ta.Result.ConnectionStatus.ToString());
});
tasks.Add(t);
}
Task.WaitAll(tasks.ToArray());
}
示例3: UsingOnRequestCompletedForLogging
/**
* An example of using `OnRequestCompleted()` for complex logging. Remember, if you would also like
* to capture the request and/or response bytes, you also need to set `.DisableDirectStreaming()`
* to `true`
*/
[U]public async Task UsingOnRequestCompletedForLogging()
{
var list = new List<string>();
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(connectionPool, new InMemoryConnection())
.DisableDirectStreaming()
.OnRequestCompleted(response =>
{
// log out the request
if (response.RequestBodyInBytes != null)
{
list.Add(
$"{response.HttpMethod} {response.Uri} \n" +
$"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");
}
else
{
list.Add($"{response.HttpMethod} {response.Uri}");
}
// log out the response
if (response.ResponseBodyInBytes != null)
{
list.Add($"Status: {response.HttpStatusCode}\n" +
$"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" +
$"{new string('-', 30)}\n");
}
else
{
list.Add($"Status: {response.HttpStatusCode}\n" +
$"{new string('-', 30)}\n");
}
});
var client = new ElasticClient(settings);
var syncResponse = client.Search<object>(s => s
.Scroll("2m")
.Sort(ss => ss
.Ascending(SortSpecialField.DocumentIndexOrder)
)
);
list.Count.Should().Be(2);
var asyncResponse = await client.SearchAsync<object>(s => s
.Scroll("2m")
.Sort(ss => ss
.Ascending(SortSpecialField.DocumentIndexOrder)
)
);
list.Count.Should().Be(4);
list.ShouldAllBeEquivalentTo(new []
{
"POST http://localhost:9200/_search?scroll=2m \n{\"sort\":[{\"_doc\":{\"order\":\"asc\"}}]}",
"Status: 200\n------------------------------\n",
"POST http://localhost:9200/_search?scroll=2m \n{\"sort\":[{\"_doc\":{\"order\":\"asc\"}}]}",
"Status: 200\n------------------------------\n"
});
}