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

    Es查询,解构类似:  A & (a & b & c) or (c & e) 

    JavaAPI写法:

    BoolQueryBuilder query = QueryBuilders.boolQuery();
    
    BoolQueryBuilder q1 = QueryBuilders.boolQuery();
        q1.must(QueryBuilders.termQuery("flag_best", 1));
        q1.must(QueryBuilders.termQuery("flag_fornew", 1));
    q1.must(QueryBuilders.termQuery("flag_off",0));

    BoolQueryBuilder q2 = QueryBuilders.boolQuery();
        q2.must(QueryBuilders.prefixQuery("sub_category", "sub"));
        q2.must(QueryBuilders.termQuery("flag_reviewed", 1));
    q2.must(QueryBuilders.termQuery("flag_off",0));
    query.should(q1); 
    query.should(q2);

    注意:如果此处存在一个共同的查询值,如flag_off,那么一定要在两个query中分别写出
    写成【query.
    must(QueryBuilders.termQuery("flag_off",0))】将会产生错误


    错误写法:(写法本身有问题,并且不能在后面连续进行must会溢出。应参照文章最后JSON结构来写Java代码)
    query.should(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("flag_best",1)).must(QueryBuilders.termQuery("flag_fornew",1)).must(QueryBuilders.termQuery("flag_off",0)));
    query.should(QueryBuilders.boolQuery().must((QueryBuilders.prefixQuery("sub_category","sub"))).must(query.must(QueryBuilders.termQuery("flag_reviewed", 1)).must(QueryBuilders.termQuery("flag_off",0))));

     JSON正确格式:

    {
      "from": 0,
      "size": 50,
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "flag_best": {
                        "value": 1,
                        "boost": 1
                      }
                    }
                  },
                  {
                    "term": {
                      "flag_fornew": {
                        "value": 1,
                        "boost": 1
                      }
                    }
                  },
                  {
                    "term": {
                      "flag_off": {
                        "value": 0,
                        "boost": 1
                      }
                    }
                  },
                  {
                    "range": {
                      "add_time": {
                        "from": "0",
                        "to": "2017-04-20",
                        "include_lower": true,
                        "include_upper": true,
                        "boost": 1
                      }
                    }
                  }
                ],
                "disable_coord": false,
                "adjust_pure_negative": true,
                "boost": 1
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "prefix": {
                      "sub_category": {
                        "value": "sub",
                        "boost": 1
                      }
                    }
                  },
                  {
                    "term": {
                      "flag_reviewed": {
                        "value": 1,
                        "boost": 1
                      }
                    }
                  },
                  {
                    "term": {
                      "flag_off": {
                        "value": 0,
                        "boost": 1
                      }
                    }
                  },
                  {
                    "range": {
                      "add_time": {
                        "from": "0",
                        "to": "2017-04-20",
                        "include_lower": true,
                        "include_upper": true,
                        "boost": 1
                      }
                    }
                  }
                ],
                "disable_coord": false,
                "adjust_pure_negative": true,
                "boost": 1
              }
            }
          ],
          "disable_coord": false,
          "adjust_pure_negative": true,
          "boost": 1
        }
      },
      "explain": true,
      "sort": [
        {
          "add_time": {
            "order": "desc"
          }
        },
        {
          "image_count": {
            "order": "desc"
          }
        }
      ]
    }

    JSON错误写法:

    {
      "from": 0,
      "size": 50,
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "flag_off": {
                  "value": 0,
                  "boost": 1
                }
              }
            },
            {
              "range": {
                "add_time": {
                  "from": "0",
                  "to": "2017-04-20",
                  "include_lower": true,
                  "include_upper": true,
                  "boost": 1
                }
              }
            }
          ],
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "flag_best": {
                        "value": 1,
                        "boost": 1
                      }
                    }
                  },
                  {
                    "term": {
                      "flag_fornew": {
                        "value": 1,
                        "boost": 1
                      }
                    }
                  }
                ],
                "disable_coord": false,
                "adjust_pure_negative": true,
                "boost": 1
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "prefix": {
                      "sub_category": {
                        "value": "sub",
                        "boost": 1
                      }
                    }
                  },
                  {
                    "term": {
                      "flag_reviewed": {
                        "value": 1,
                        "boost": 1
                      }
                    }
                  }
                ],
                "disable_coord": false,
                "adjust_pure_negative": true,
                "boost": 1
              }
            }
          ],
          "disable_coord": false,
          "adjust_pure_negative": true,
          "boost": 1
        }
      },
      "explain": true,
      "sort": [
        {
          "add_time": {
            "order": "desc"
          }
        },
        {
          "image_count": {
            "order": "desc"
          }
        }
      ]
    }
    View Code

    JSON主体结构:

    {"query": {
       "bool": {
         "should": [
           {"bool": {
             "must": [
               {}
             ]
           }
           },
           {"bool": {
             "must": [
               {}
             ]
           }}
         ]
       }
      }
    }
  • 相关阅读:
    DAPM之三:audio paths与asound.conf
    DAPM之二:audio paths与dapm kcontrol
    DAPM之一:概述
    snd_kcontrol探究
    Linux ALSA声卡驱动之八:ASoC架构中的Platform
    Linux ALSA声卡驱动之七:ASoC架构中的Codec
    吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Hibernate指定generated属性
    吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Hibernate映射属性
    吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Hibernate文件配置
    吴裕雄--天生自然轻量级JAVA EE企业应用开发Struts2Sping4Hibernate整合开发学习笔记:Hibernate项目创建
  • 原文地址:https://www.cnblogs.com/0xcafedaddy/p/6734224.html
Copyright © 2011-2022 走看看