1

situation is this. I have in elastic a group. each of these groups have a nested list of items. Both group and items have an attribute named serial, which are unique. I get a serial for the group and a serial for item, and with those 2 items i'm supposed to return the item.

Currently i'm doing it the following way:

public item findItem(string groupSerial, string itemSerial)
    {
        var searchResponse = _elasticClient.Search<Group>(s => s
            .Index(_config.groupIndexName)
            .Query(q => q
                .ConstantScore(cs => cs
                    .Filter(f => f
                        .Term(t => t
                            .Field(fi => fi.serial)
                            .Value(groupSerial)
                        )
                    )
                )
            ).Query(q => q
                .Nested(c => c
                    .InnerHits(i => i.Explain())
                    .Path(p => p.items)
                    .Query(nq => nq.Term(t => t
                        .Field(field => field.items.First().serial)
                        .Value(itemSerial)))))
        );

        var result = searchResponse.Documents.FirstOrDefault();

        return result?.items.Find(item => item.serial == itemSerial);
    }

I get the feeling that there is supposed to be a more efficient way. Like getting the item straight from the search in elastic. Does anyone know how?

ziraak
  • 131
  • 3
  • 14
  • 1
    Are you getting all the items in `result.items`? if that is the case try using [bool query](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/bool-query-usage.html) – Aman B May 24 '18 at 10:12

0 Answers0