Since I am unfamiliar with the ElasticSearch
concept, I used some code as a guide to build my general elastic search NEST
query by injected IElasticClient
. that is, is following
public async Task<IEnumerable<T>> GetEsDataWithSearchParameterAsync<T>(string indexName, string searchParameter) where T : class
{
var searchResponse = await _elasticClient.SearchAsync<T>(s => s
.Index(indexName)
.Query(q => q.MatchAll())
.Query(q => q.QueryString(qs => qs.Query(searchParameter + "*")))
).ConfigureAwait(false);
var documents = searchResponse.Documents;
return documents;
}
I've read through this link, but it's hard to grasp. I'm looking for video lessons for comprehensive learning similar to those offered by Udemy, but I haven't been successful in finding any as of now.
So I need to write a generic method for get list of properties by array of string and string value, like
public async Task<IEnumerable<T>> GetEsDataByArrayOfValuesWithFieldNameAndParameterAsync<T>(string indexName,string searchParameter,string field, string[] values) where T : class
{
var searchResponse = await _elasticClient.SearchAsync<T>(s => s
.Index(indexName)
.Query(q=>q.Term("market", searchParameter))
.Query(q => q.Bool(b => b.Should(s => TermAny(s, field, values))))
);
var documents = searchResponse.Documents;
return documents;
}
private static QueryContainer TermAny<T>(QueryContainerDescriptor<T> descriptor, Field field, object[] values) where T : class
{
QueryContainer q = new QueryContainer();
foreach (var value in values)
{
q |= descriptor.Term(t => t.Field(field).Value(value));
}
return q;
}
As referred this link I updated my code but not getting any data, but by DSL queried its getting 2 records
query: {query_string: {query: "(clusterId: "/SE_003/","/SE_004/") AND (market:SE)"}}
query_string: {query: "(clusterId: "/SE_003/","/SE_004/") AND (market:SE)"}
query: "(clusterId: \"/SE_003/\",\"/SE_004/\") AND (market:SE)"
and also tried by
var searchResponse = await _elasticClient.SearchAsync<T>(s => s
.Index(indexName)
//.Query(q=>q.Bool(b=>b.Should(s=>
// s.Match(qs => qs.Field("market").Query(searchParameter)),
// s.Query(sq => sq.Terms(c=>c.Field(field).Terms<string>(values)))
// )))
.Query(q => q.Match(qs => qs.Field("market").Query(searchParameter)))
.Query(q => q.Terms(c => c.Field(field).Terms<string>(values)))
);
which is also getting null record.
var searchResponse = await _elasticClient.SearchAsync<T>(s => s
.Index(indexName)
.From(0)
.Size(10000)
.Query(q =>
q.QueryString(qs =>
qs.Query($"({firstField}:{firstFieldValue}) {operatorType} ({secondField}:{secondFieldValue})")))
.Sort(s => s.Ascending(SortLinks))
).ConfigureAwait(false);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.