Querying array of strings by array of strings in elasticsearch.net

前端 未结 1 1943
别跟我提以往
别跟我提以往 2021-01-28 07:13

I\'m using elasticsearch.net library in C# and I\'m trying to query for objects matching specified filter.

I would like the query to return objects where at least one o

相关标签:
1条回答
  • 2021-01-28 07:42

    I think that your problem is that you tries to pass whole array to query. Instead of that you should treat that as OR expression.

    Below is the raw query that you should use:

    {
        "query": {
            "bool": {
                "should": [
                    { "term": {"names": "test" } },
                    { "term": {"names": "xyz" } }
                ]
            }
        }
    }
    

    And that the C# code to achive that. First I have defined helper function:

    private static QueryContainer TermAny<T>(QueryContainerDescriptor<T> descriptor, Field field, object[] values) where T : class
    {
        QueryContainer q = new QueryContainer();
        foreach (var value in values)
        {
            q |= descriptor.Term(t => t.Field(field).Value(value));
        }
        return q;
    }
    

    And now the query:

    string[] values = new[] { "test", "xyz" };
    client.Search<A>(x => x.Query(
        q => q.Bool(
            b => b.Should(s => TermAny(s, "names", values)))));
    
    0 讨论(0)
提交回复
热议问题