Elasticsearch不止一个应该,必须和结合在一起

时间:2018-11-28 23:30:25

标签: elasticsearch nest

我正在学习Elasticsearch,但是我不知道如何结合和必须结合。我需要一个具有某些值的字段,而另一个具有多个值的字段,这种情况可以重复多次。例如,

(Field1 == 1 and (Field2 == "a" or Field2 == "b" )) or
(Field1 == 2 and (Field2 == "c" or Field2 == "d" )) or
(Field1 == 3 and (Field2 == "a" or Field2 == "c" )) .... etc

我尝试了许多方法,但是我不能使用布尔型,必须或必须处于同一水平。例如,

{
    "query": {
        "bool": {
            "should": [
                {
                    "bool": {
                        "must":  [
                                { "term": { "Field1": 1 } },
                                {
                                    "bool": {
                                        "should": [
                                            { "terms": { "Field2" : [ "a", "b" ] } }
                                        ]
                                    }
                                }
                        ]
                    },
                    "bool": {
                        "must": [
                                { "term": { "Field1": 2 } },
                                {
                                    "bool": {
                                        "should": [
                                            { "terms": { "Field2" : [ "c", "d" ] } }
                                        ]
                                    }
                                }
                        ]
                    },
                    "bool": {
                        "must": [
                                { "term": { "Field1": 2 } },
                                {
                                    "bool": {
                                        "should": [
                                            { "terms": { "Field2" : [ "a", "c" ] } }
                                        ]
                                    }
                                }
                        ]
                    }
                }
            ]
        }
    }
}

我真的很需要NEST,但是我想从这里开始。

1 个答案:

答案 0 :(得分:1)

这是您可以执行的操作:

{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "field1": 1
                      }
                    },
                    {
                      "terms": {
                        "field2": [
                          "a",
                          "b"
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "field1": 2
                      }
                    },
                    {
                      "terms": {
                        "field2": [
                          "c",
                          "d"
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "field1": 3
                      }
                    },
                    {
                      "terms": {
                        "field2": [
                          "a",
                          "c"
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

如果您想要a OR b OR c,其中abc代表任何类型的查询,则在弹性查询中表示为:

{
  "bool": {
    "should": [
      {
        a
      },
      {
        b
      },
      {
        c
      }
    ]
  }
}

用精确的查询替换abc

相关问题