Reputation: 560
I'm trying to search following text:
"women held" (note " " is added for text representation)
What I need from the above text is all the item that contains the "women" and "held" words from the field stored in the elastic index.
What I tried was:
string searchText = "women held";
var filterQuery = Query<T>.Terms("summaryText", searchText.Split(' '));
var result = Client.Search<T>(s => s.Index(IndexName).Query(filterQuery).Size(1000));
But the above query result in getting either of the item as "Women" Or "held" from the "summaryText" field but what I want is "women and held" i.e. both text to be present in "summaryText" field.
Please let me know if this make sense. I have tried many combination but still haven't succeeded. Does anyone from Nest Guru can help??
Upvotes: 0
Views: 2335
Reputation: 560
This is what I got as solution, hope it would help anyone trying the similar ANDing search. Well not an elegant way but works for me:
string searchText = "women held";
string[] splitSearch = searchText.Split(' ');
List<QueryContainer> searchQueryList = new List<QueryContainer>();
foreach(var strText in splitSearch)
{
searchQueryList.Add(Query<T>.Term("summaryText", strText));
}
var filterQuery = Query<T>.Bool(b=> b.Must(searchQueryList.ToArray()));
var result = Client.Search<T>(s => s.Index(IndexName).Query(filterQuery).Size(1000));
Upvotes: 0
Reputation: 1233
If you want the exact match for "woman held" on a field then do not split the string:
string searchText = "women held";
var filterQuery = Query<T>.Terms("summaryText", searchText);
var result = Client.Search<T>(s => s.Index(IndexName).Query(filterQuery).Size(1000));
If what you are looking for is a "contains" functionality, then you need to use a regexp query (but has a performance overhead if your data is huge):
SearchDescriptor<T> descriptor = new SearchDescriptor<T>();
descriptor = descriptor.Index(IndexName).Size(1000)
.Query(query => query
.Regexp(r => r.OnField("summaryText").Value(".*" + "woman held" + ".*")));
var result = ElasticClient().Search<T>(s => descriptor);
Upvotes: 2