Filter nested objects in document (very hard)

Hello there!

I am trying to achieve not common thing and I am struggling with it hard.

I have such document:

{
  "_index": "users",
  "_type": "_doc",
  "_id": "07e4588c-6102-4be2-9518-cabaa40726e0",
  "_score": 1,
  "_source": {
    "UserId": "07e4588c-6102-4be2-9518-cabaa40726e0",
...
    "UserField": [
      {
        "Id": "3fa8f6aa-e4d7-4762-9287-d53de515954a",
        "Value": "",
        "UserId": "07e4588c-6102-4be2-9518-cabaa40726e0",
        "FieldId": "3acca766-9f4a-4d7e-9ffb-262b7a26c971",
        "TenantKey": "b1ef9a58-bcbf-4069-ae4a-3efea1a51bba"
      },
      {
        "Id": "7b9219c7-4157-465c-9904-4e529cbf2f66",
        "Value": "Mason",
        "UserId": "07e4588c-6102-4be2-9518-cabaa40726e0",
        "FieldId": "f5f67cb3-c7e6-42a9-a299-06511240bda0",
        "TenantKey": "b1ef9a58-bcbf-4069-ae4a-3efea1a51bba"
      },
      {
        "Id": "b65ba0e9-64b7-4203-8ac1-0984dad642a5",
        "Value": "Alex",
        "UserId": "07e4588c-6102-4be2-9518-cabaa40726e0",
        "FieldId": "43bf99f4-20cf-48c3-9d09-d4488e9f0933",
        "TenantKey": "b1ef9a58-bcbf-4069-ae4a-3efea1a51bba"
      },
}
    ],
...
      }
    }
  }
}

As you can see here, I have an array of UserFields and this is nested object in Elasticsearch. My goal is to search by Values of that fields, but I need particular FieldId and which always the same, described in static class.

So In my .NET code I can write ..x.FieldId == FieldIds.Firstname. Is it possible somehow to get an advantage of search, firstly filtering needed fields and then search by their Value property.

What I've already tried?

     var filters = new List<Func<QueryContainerDescriptor<UserSearchItem>, QueryContainer>>();
        
            filters.Add(fq => fq.Terms(t => t.Field(Infer.Field<UserSearchItem>(x => x.UserField.First(x => x.FieldId == FieldIds.Firstname)))));


            var searchResponse =
                _client.Search<UserSearchItem>(x => x.Query(q => q
                    .Nested(t => t
                        .Path(Infer.Field<UserSearchItem>(x => x.UserField))
                        .Query(q => q.Bool(f => f.Filter(filters))))));

No luck

And this one, even QueryContainer not applied by operator &

  container &= new NestedQuery
            {
                Path = Infer.Field<UserSearchItem>(x => x.UserField),
                Query = new BoolQuery()
                {
                    Should = new List<QueryContainer>()
                    {
                        new MatchQuery()
                        {
                            Field =
                                Infer.Field<UserSearchItem>(x => x.UserField.First(x => x.FieldId == FieldIds.Firstname)).Name,
                            
                            Query = $"{req.Name}"
                        },
                        new MatchQuery()
                        {
                            Field =
                                Infer.Field<UserSearchItem>(x => x.UserField.First(x => x.FieldId == FieldIds.Lastname)).Name,
                            
                            Query = $"{req.Name}"
                        },
                        new MatchQuery()
                        {
                            Field =
                                Infer.Field<UserSearchItem>(x => x.UserField.First(x => x.FieldId == FieldIds.Middlename)).Name,
                            
                            Query = $"{req.Name}"
                        }
                    }
                }};

Thanks for any help in advance!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.