亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Elasticsearch嵌套數組的多個過濾條件

Elasticsearch嵌套數組的多個過濾條件

C#
MMMHUHU 2023-08-20 11:05:39
也許這個問題在某處得到了回答,但我找不到它。所以我尋求幫助我的產品模型有一個嵌套的屬性列表。像這樣的東西[    {        ID: "Product1",        ...        Properties: [            { "Source": "Color", Value: "green"},            { "Source": "Size",  Value: "2"},        ]    },    {        ID: "Product2",        ...        Properties: [            { "Source": "Color", Value: "blue"},            { "Source": "Size", Value: "2"},        ]    },    {        ID: "Product3",        ....        Properties: [            { "Source": "Color", Value: "red"},            { "Source": "Size", Value: "1"},        ]    },]索引映射:"properties" : {        "type" : "nested",        "properties" : {          "source" : {            "type" : "text",            "fields" : {              "keyword" : {                "type" : "keyword",                "ignore_above" : 256              }            }          },          "value" : {            "type" : "text",            "fields" : {              "keyword" : {                "type" : "keyword",                "ignore_above" : 256              }            }          }        }      },我需要搜索查詢來僅查找顏色為綠色或藍色且尺寸為 2 的產品。我為我的搜索請求創建了此查詢,但它將返回空結果。您能幫助我構建這種查詢,我用錯了什么嗎?謝謝
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

嘗試用嵌套查詢包裝每個必須子句:


var query = new BoolQuery

{

    Must = new QueryContainer[]

    {

        new NestedQuery

        {

            Path = "properties",

            Query = new BoolQuery()

            {

                Must = new QueryContainer[]

                {

                    new TermQuery()

                    {

                        Field = new Nest.Field("properties.source.keyword"),

                        Value = "Color"

                    },

                    new TermsQuery()

                    {

                        Field = new Nest.Field("properties.value.keyword"),

                        Terms = new[] { "green", "blue"}

                    }

                }

            }

        },

        new NestedQuery

        {

            Path = "properties",

            Query = new BoolQuery()

            {

                Must = new QueryContainer[]

                {

                    new TermQuery()

                    {

                        Field = new Nest.Field("properties.source.keyword"),

                        Value = "Size"

                    },

                    new TermsQuery()

                    {

                        Field = new Nest.Field("properties.value.keyword"),

                        Terms = new[] {"2"}

                    }

                }

            }

        }

    }

};

完整的工作示例


class Program

{

    public class Document

    {

        public int Id { get; set; }

        [Nested]

        public List<Property> Properties { get; set; }

    }


    public class Property

    {

        public string Source { get; set; }

        public string Value { get; set; }


        public override string ToString() => $"Source: {Source} Value: {Value}";

    }


    static async Task Main(string[] args)

    {

        var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

        var connectionSettings = new ConnectionSettings(pool);

        connectionSettings.DefaultIndex("documents");

        connectionSettings.DisableDirectStreaming();

        connectionSettings.PrettyJson();


        var client = new ElasticClient(connectionSettings);


        var deleteIndexResponse = await client.Indices.DeleteAsync("documents");

        var createIndexResponse = await client.Indices.CreateAsync("documents", d => d

            .Map(m => m.AutoMap<Document>()));


        var indexDocument = await client

            .IndexDocumentAsync(new Document

            {

                Id = 1, 

                Properties = new List<Property>

                {

                    new Property {Source = "Color", Value = "green"},

                    new Property {Source = "Size", Value = "2"},

                }

            });

        indexDocument = await client

            .IndexDocumentAsync(new Document

            {

                Id = 2, 

                Properties = new List<Property>

                {

                    new Property {Source = "Color", Value = "blue"},

                    new Property {Source = "Size", Value = "2"},

                }

            });

        indexDocument = await client

            .IndexDocumentAsync(new Document

            {

                Id = 3, 

                Properties = new List<Property>

                {

                    new Property {Source = "Color", Value = "red"},

                    new Property {Source = "Size", Value = "1"},

                }

            });


        var refreshAsync = client.Indices.RefreshAsync();


        var query = new BoolQuery

        {

            Must = new QueryContainer[]

            {

                new NestedQuery

                {

                    Path = "properties",

                    Query = new BoolQuery()

                    {

                        Must = new QueryContainer[]

                        {

                            new TermQuery()

                            {

                                Field = new Nest.Field("properties.source.keyword"),

                                Value = "Color"

                            },

                            new TermsQuery()

                            {

                                Field = new Nest.Field("properties.value.keyword"),

                                Terms = new[] {"green", "blue"}

                            }

                        }

                    }

                },

                new NestedQuery

                {

                    Path = "properties",

                    Query = new BoolQuery()

                    {

                        Must = new QueryContainer[]

                        {

                            new TermQuery()

                            {

                                Field = new Nest.Field("properties.source.keyword"),

                                Value = "Size"

                            },

                            new TermsQuery()

                            {

                                Field = new Nest.Field("properties.value.keyword"),

                                Terms = new[] {"2"}

                            }

                        }

                    }

                }

            }

        };


        var response = client.Search<Document>(s => s.Query(q => query));


        foreach (var document in response.Documents)

        {

            Console.WriteLine($"Id: {document.Id}");

            document.Properties.ForEach(Console.WriteLine);

            Console.WriteLine();

        }

    }

}

印刷:


Id: 1

Source: Color Value: green

Source: Size Value: 2


Id: 2

Source: Color Value: blue

Source: Size Value: 2

希望有幫助。


查看完整回答
反對 回復 2023-08-20
  • 1 回答
  • 0 關注
  • 150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號