Reputation: 15
I am trying to use completion context with multiple value. Context accept only one value.
string contextValue = "10";
List<string> listOfContextValues = new List<string> {"10", "20","30"};
var a = Client.Search<Post>(s => s
.Suggest(su => su
.Completion("categories", cs => cs
.Field(f => f.CSuggest)
.Prefix(query)
.Contexts(co => co
.Context("sourceid",
cd => cd.Context(contextValue)
)
)
)
));
I don't know how to filter my suggest field with listOfContextValues.
Update
When i using a for loop and passing single context as filter and storing result in a list.
And also using Russ suggested code and storing result in a list. But Some result are missing in second list.
As you can see in below:
var socialNetworks = srvUser.GetUserSocialNetworks(userId);
List<string> tags = new List<string>();
foreach (var socialNetwork in socialNetworks)
{
var result = Client.Search<Post>(s => s
.Suggest(su => su
.Completion("categories", cs => cs
.Field(f => f.CSuggest)
.Prefix(query)
.Contexts(co => co
.Context("sourceid",
cd => cd.Context(socialNetwork)
)
)
)
));
List<string> br = result.Suggest["categories"].SelectMany(x => x.Options)
.Select(y => y.Text).Distinct().ToList();
tags.AddRange(br);
}
var searchResponse = Client.Search<Post>(s => s
.Suggest(su => su
.Completion("categories", cs => cs
.Field(f => f.CSuggest)
.Prefix(query)
.Contexts(co => co
.Context("sourceid", socialNetworks
.Select<string, Func<SuggestContextQueryDescriptor<Post>, ISuggestContextQuery>>(v => cd => cd.Context(v))
.ToArray()
)
)
)
)
);
List<string> sa = searchResponse.Suggest["categories"].SelectMany(x => x.Options).Select(y => y.Text).Distinct().ToList();
if (sa.Count != tags.Count)
{
// >>>>> counts are not the same
}
Upvotes: 1
Views: 530
Reputation: 125528
.Context(...)
takes a params Func<SuggestContextQueryDescriptor<T>, ISuggestContextQuery>[]
as the second argument, so for a collection of context values you can do
void Main()
{
var client = new ElasticClient();
var listOfContextValues = new List<string> { "10", "20", "30" };
var query = "query";
var searchResponse = client.Search<Post>(s => s
.Suggest(su => su
.Completion("categories", cs => cs
.Field(f => f.CSuggest)
.Prefix(query)
.Contexts(co => co
.Context("sourceid",
listOfContextValues
.Select<string, Func<SuggestContextQueryDescriptor<Post>, ISuggestContextQuery>>(v => cd => cd.Context(v))
.ToArray()
)
)
)
)
);
}
public class Post
{
public CompletionField CSuggest { get; set; }
}
Upvotes: 3
Reputation: 343
Are you looking for size ?
...s =>
s.Completion("categories",
cs=> cs.
Field(p => p.Suggestion)
.Contexts(
ctx =>
ctx.Context("sid", d => d.Context(value))
.Prefix(searchText)
.Size(10);
Upvotes: 0