【问题标题】:how to user query should and must_not in elasticseacrh如何在elasticsearch中用户查询应该和must_not
【发布时间】:2020-06-22 01:56:08
【问题描述】:

我有这样的弹性数据

{
  "xId" : "25b35718-1804-428e-87fc-e215422d21a0",
  "date" : "2019-10-13T07:07:07.558Z",
  "type" : "promo",
  "accountId" : 50,
  "readBy" : [
    "b",
    "a"
  ]
}

我想检索具有多个条件的数据 ,所以我使用了should和must_not的查询,当我使用该查询时,数组返回空,但现在数据仍然出现,是不是缺少了什么?

这是我的示例查询

{"query": {
          "constant_score": {
            "filter": {
              "bool": {
                "should": [
                  {
                    "terms": {
                      "accountId": [50]
                    }
                  },{
                    "terms": {
                      "type": ["promo"]
                    }
                  },
                  {
                    "match": {
                      "userId": "a"
                    }
                  }
                ],
                "must_not": [
                  {
                    "terms": {
                      "type": []
                    }
                  }, {
                    "terms": {
                      "readBy": ["a"]
                    }
                  }

                ]
              }
            }
          }
        }
      }

【问题讨论】:

  • 您的示例文档中没有任何 userId 字段。
  • 另外,你为什么要说‘terms type:[]’?是否要排除空数组?

标签: elasticsearch elasticsearch-query term-query


【解决方案1】:

解决了 我使用这样的查询

{"query": {
      "constant_score": {
        "filter": {
          "bool": {
            "should": [
              {
                "terms": {
                  "accountId": [50]
                }
              },{
                "terms": {
                  "type": ["promo"]
                }
              },
              {
                "match": {
                  "userId": "a"
                }
              }
            ],
            "must_not": [
              {
                "terms": {
                  "type": []
                }
              }, {
                "term": {
                  "readBy.keyword": "a"
                }
              }

            ]
          }
        }
      }
    }
  }

【讨论】:

    猜你喜欢
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多