I am trying to map a graph using the .NET Client API from NuGet "Elastic.Clients.Elasticsearch" 8.13.8. See code below.
The graph nodes are each represented as a document in ES. On each document there is supposed to be an array of a nested "relations" object field, each of which holds the relation's target ID and relationship type.
I am able to map the node name as a keyword as shown below. But when I try to "dot" my way through the mappings API, I am unable to unlock access to the type of the relation class.
How can I map the property Type
in KnowledgeGraphRelation
as a keyword, or in general, how do I map anything inside an array of nested objects inside the main document type?
namespace ElasticTest
{
public class KnowledgeGraphNode
{
public Guid Id { get; private set; }
public string Name { get; private set; }
public List<KnowledgeGraphRelation> Relations { get; private set; }
public KnowledgeGraphNode(string name)
{
Id = Guid.NewGuid();
Name = name;
Relations = new List<KnowledgeGraphRelation>();
}
}
public class KnowledgeGraphRelation
{
public Guid Target { get; private set; }
public string Type { get; private set; }
public KnowledgeGraphRelation(Guid target, string type)
{
Target = target;
Type = type;
}
}
public class GraphRepository
{
public async Task TestMapping()
{
ElasticsearchClient Client = new ElasticsearchClient();
IndexName indexName = "MyGraph";
await Client.Indices.CreateAsync(indexName, ConfigureIndex);
}
void ConfigureIndex(CreateIndexRequestDescriptor descriptor)
{
descriptor.Mappings(md =>
md.Properties<KnowledgeGraphNode>(pd =>
{
pd.Keyword(pn => pn.Name)
.Nested(pn => pn.Relations,
npd => npd.);
}));
}
async Task Search()
{
ElasticsearchClient Client = new ElasticsearchClient();
IndexName indexName = "MyGraph";
var response = await Client.SearchAsync<KnowledgeGraphNode>(indexName, d => d.Query(
q => q.Nested(
nq => nq.Path(n => n.Relations).Query(
rq => rq.Term(rt => rt.Field())))));
// ----^ rt.Field has type "node" instead of "relation".
}
}
}
I have the same issue when trying to query the nested fields.