多个应该查询必须查询

时间:2016-11-13 12:45:57

标签: elasticsearch nest elasticsearch-net elasticsearch-5

我正在构建一个对Elastic 5的查询(在.net中使用nest),我试图实现这个结果:

必须为value1且值为2

应该有value3或value4

并且应该具有value5或value6

这是我的问题:

    {
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "code": {
              "query": "value1"
            }
          }
        },
        {
          "match": {
            "code": {
              "query": "value2"
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "code": {
              "query": "value3"
            }
          }
        },
        {
          "match": {
            "code": {
              "query": "value4"
            }
          }
        }
        ],
        "should": [
        {
          "match": {
            "code": {
              "query": "value5"
            }
          }
        },
        {
          "match": {
            "code": {
              "query": "value6"
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

我没有得到想要的答案(例如我没有任何值5和值6但仍然得到结果)

谢谢

3 个答案:

答案 0 :(得分:6)

然后你需要这样的东西:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "code": {
              "query": "value1"
            }
          }
        },
        {
          "match": {
            "code": {
              "query": "value2"
            }
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "match": {
                  "code": {
                    "query": "value3"
                  }
                }
              },
              {
                "match": {
                  "code": {
                    "query": "value4"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "match": {
                  "code": {
                    "query": "value5"
                  }
                }
              },
              {
                "match": {
                  "code": {
                    "query": "value6"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

答案 1 :(得分:4)

这是NEST等同于Val's answer

void Main()
{
    var client = new ElasticClient();

    client.Search<MyDocument>(s => s
        .Query(q => q
            .Bool(b => b
                .Must(mu => mu
                    .Match(m => m
                        .Field(f => f.Code)
                        .Query("value1")
                    ), mu  => mu
                    .Match(m => m
                        .Field(f => f.Code)
                        .Query("value2")
                    ), mu => mu
                    .Bool(bb => bb
                        .MinimumShouldMatch(1)
                        .Should(sh => sh
                            .Match(m => m
                                .Field(f => f.Code)
                                .Query("value3")
                            ), sh => sh
                            .Match(m => m
                                .Field(f => f.Code)
                                .Query("value4")
                            )
                        )
                    ), mu => mu
                    .Bool(bb => bb
                        .MinimumShouldMatch(1)
                        .Should(sh => sh
                            .Match(m => m
                                .Field(f => f.Code)
                                .Query("value5")
                            ), sh => sh
                            .Match(m => m
                                .Field(f => f.Code)
                                .Query("value6")
                            )
                        )
                    )
                )
            )
        )
    );
}

public class MyDocument
{
    public string Code { get; set; }
}

答案 2 :(得分:0)

这是另一个使用更具体的示例数据的示例:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "author": {
              "query": "Anita Author"
            }
          }
        },
        {
          "match": {
            "author": {
              "query": "Bertha Booster"
            }
          }
        }
      ],
      "should": 
      [
          {
            "match": {
              "title": {
                "query": "A Fantastic Book"
              }
            }
          },
        {
          "match": {
            "title": {
              "query": "A Fantastic Book, Volume 2"
            }
          }
        },
        {
          "match": {
            "title": {
              "query": "Yet Another Fantastic Book"
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}