账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    为什么查询不包含某个词的记录时 must_not不生效呢?
    41
    0

    为什么must_not就不生效呢?

    mapping:

    "text_terms": {
        "type": "nested",
        "properties": {
            "term": {
                "type": "string",
                "index": "not_analyzed"
            },
            "freq": {
                "type": "integer"
            }
        }
    }

    数据

    { "text_terms" : [ { "term" : "aaa", "freq" : 1 }, { "term" : "bbb", "freq" : 1 }, { "term" : "ccc", "freq" : 1 } ] }
    { "text_terms" : [ { "term" : "aaa", "freq" : 1 }, { "term" : "西门子", "freq" : 1 }, { "term" : "ccc", "freq" : 1 } ] }
    { "text_terms" : [ { "term" : "ccc", "freq" : 1 }, { "term" : "西门子", "freq" : 1 }, { "term" : "ddd", "freq" : 1 } ] }
    { "text_terms" : [ { "term" : "ddd", "freq" : 1 }, { "term" : "eee", "freq" : 1 } ] }

    查询包含西门子的记录 没有问题 能查出包含西门子的两条记录

    "query": { "nested": { "query": { "bool": { "must": [{ "term": { "text_terms.term": "西门子" } }] } }, "path": "text_terms" } }

    但是查询不包含西门子的记录时 就不生效了呢?

    "query": { "nested": { "query": { "bool": { "must_not": [{ "term": { "text_terms.term": "西门子" } }] } }, "path": "text_terms" } }

    怎么此时四条记录都能查出来呢?

    1
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • ¢男人请戒爱 普通会员 1楼

      在SQL中,must_not是一个约束,用于确保在某些情况下,表中的某些列(或整个表)必须始终为空或不允许非空。如果某个字段不包含某个特定的值,must_not不会生效。

      例如,假设你有一个名为users的表,其中包含usernamepassword两列。你可以使用must_not来确保用户不能使用某些特定的用户名(例如,"admin"):

      sql ALTER TABLE users ADD CONSTRAINT must_not UNIQUE (username, password);

      在这个例子中,must_not约束会确保在usernamepassword列中,任何用户都不可能使用"admin"作为用户名。

      如果你尝试将"admin"添加到用户表中,或者尝试修改已经存在的用户表,must_not约束不会生效。因为must_not只应用于新创建或更新的表,不应用于已存在的表。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部