Hello all!
I am just getting started with Elasticsearch in .NET (C# + NEST), and I have a question where I don't find a solution. Maybe you can help me or point me to some kind of tutorial where I find the answer?
My problem is this:
I have an index containing an object with some properties and one of these properties is a list of another object, which I created as a nested property (at least I think I did :)).
The objects look like this:
public class MainObject
{
public int Id { get; set; }
public string PropertyA { get; set; }
public string PropertyB { get; set; }
[Nested]
public List<NestedObject> Values { get; set; }
}
public class NestedObject
{
public string Key { get; set; }
public string Value { get; set; }
}
Now I want to set a search query which contains one or more words and each word should appear in either one of the properties of the main object or one of the properties of any of the nested values. In case I search for 3 words, I want each result containing all 3 words (regardless of which property they appear in) prioritized very high, results containing only 2 of the words prioritized medium, those containing only 1 of the words low, and those containing none not showing up. (The same of course with 2 or 4 or 5 words) The properties may contain whole sentences and they just have to contain the word, not be equal to the word.
So that's how I created the index:
var indexName = "mytestindex";
var settings = new ConnectionSettings(new Uri("http://localhost:9200")).DefaultIndex(indexName).DisableDirectStreaming();
var client = new ElasticClient(settings);
var createIndexResponse = client.Indices.Create(indexName, c =>
{
return c.Map<MainObject>(m =>
{
return m.Properties(ps =>
{
return ps.Nested<NestedObject>(n =>
{
return n.Name(nn => nn.Values);
});
});
});
});
This is how I index each object:
var response = client.IndexDocument(obj);
And this is how my search currently looks like (where I don't know how to change it to produce the result outlined above):
var searchPhrase = "these are the words to be searched";
var searchResponse = client.Search<MainObject>(s =>
{
return s.Query(q =>
{
return q.Nested(c =>
{
return c.Name("named_query")
.Boost(1.1)
.InnerHits(i => i.Explain())
.Path(p => p.Values)
.Query(nq =>
{
return nq.QueryString(qs =>
{
return qs.DefaultField(f => f.Values.First().Value)
.Query("*" + searchPhrase + "*");
});
})
.IgnoreUnmapped();
});
});
});
What would I have to change to produce the result I am looking for? Anyone got any idea?
Thank you so much for your time!
Kind regards,
Wolfgang
(PS: The application and data is in German, so if I have to change anything that Elasticsearch knows how to work with german words instead of english words, that might be relevant as well :))