zoukankan      html  css  js  c++  java
  • elasticsearch查询should和must

    elasticsearch在should和must查询时不能精确查出数据,主要原因是在7.0版本后should查询时minimum_should_match默认为0,查出了非should条件中的数据。

    minimum_should_match可以控制查询精度,在should和must联合查询查询时必须使用,否则查不出精确的数据。

    {
        "size":20,
        "query":{
            "bool":{
                "should":[
                    {
                        "match_phrase":{
                            "Title":"关键词"
                        }
                    },
                    {
                        "match_phrase":{
                            "summary":"关键词"
                        }
                    },
                    {
                        "match_phrase":{
                            "Content":"关键词"
                        }
                    }
                ],
                "minimum_should_match":1,
                "must":{
                    "match_phrase":{
                        "categoryId":0
                    }
                }
            }
        }
    }

    minimum_should_match为1时,表示无管should有多少可选条件子句,至少满足1个条件。

    minimum_should_match为-1时,负数时,至少满足should可选子句的总数减去此数字应该是必需的。

    如果不使用minimum_should_match,还有一种解决方案。

    (categoryId=0&Title="关键词")||(categoryId=0&summary="关键词")||(categoryId=0&Content="关键词")

    {
        "size":20,
        "query":{
            "bool":{
                "should":[
                    {
                        "bool":{
                            "must":[
                                {
                                    "match_phrase":{
                                        "categoryId":0
                                    }
                                },
                                {
                                    "match_phrase":{
                                        "Title":"关键词"
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "bool":{
                            "must":[
                                {
                                    "match_phrase":{
                                        "categoryId":0
                                    }
                                },
                                {
                                    "match_phrase":{
                                        "summary":"关键词"
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "bool":{
                            "must":[
                                {
                                    "match_phrase":{
                                        "categoryId":0
                                    }
                                },
                                {
                                    "match_phrase":{
                                        "Content":"关键词"
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
  • 相关阅读:
    学习心得总结(1)
    Git使用总结
    Git管理分支
    Git 常用命令整理
    C#Windows窗体中添加了AxWindowsMediaPlayer的详细用法影响键盘操作的问题
    连接SQLsever数据库在C#中不能操作的问题
    C#中字符串的操作
    iOS TableView如何刷新指定的cell或section
    iOS所有的子视图
    iOStextFiled判断输入长度
  • 原文地址:https://www.cnblogs.com/yuejin/p/14684997.html
Copyright © 2011-2022 走看看