{"rsdb":{"rid":"244646","subhead":"","postdate":"0","aid":"168340","fid":"76","uid":"1","topic":"1","content":"
\n
\n \u539f\u6587\u51fa\u5904\uff1a \n \u7434\u6c34\u7389<\/a>\n <\/div> \n

\u5728\u8ba2\u5355\u641c\u7d22\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u5b9e\u73b0\u590d\u5408\u641c\u7d22\uff0c\u6bd4\u5982 ( A must B ) or ( C must D ) \u6216\u8005 (A or C) must ( B or D ) \u3002 \u8fd9\u5c31\u9700\u8981\u80fd\u591f\u7075\u6d3b\u5730\u7ec4\u5408\u6761\u4ef6\uff0c\u6761\u4ef6\u53ef\u4ee5\u662f\u539f\u5b50\u7684\u6216\u590d\u5408\u7684\u3002\u53ef\u4ee5\u4f7f\u7528\u7ec4\u5408\u6a21\u5f0f\u6765\u5b9e\u73b0\u3002<\/p> \n

\u601d\u8def<\/h3> \n

\u8981\u5b9e\u73b0\u590d\u5408\u641c\u7d22\u6761\u4ef6\u7684\u6784\u5efa\uff0c\u9700\u8981\u89e3\u51b3\u4e24\u4e2a\u95ee\u9898\uff1aA. \u5982\u4f55\u8868\u793a\u590d\u5408\u641c\u7d22\u6761\u4ef6\uff1b B. \u5982\u4f55\u5c06\u590d\u5408\u641c\u7d22\u6761\u4ef6\u8f6c\u6362\u4e3a\u5408\u9002\u7684ES\u67e5\u8be2\u5bf9\u8c61\u3002\u5bf9\u4e8eA\u6765\u8bf4\uff0c\u5173\u952e\u5c31\u662f\u641c\u7d22\u6761\u4ef6\u53ef\u7075\u6d3b\u7ec4\u5408\uff0c\u7528\u7ec4\u5408\u6a21\u5f0f\u518d\u5408\u9002\u4e0d\u8fc7\uff1b\u5bf9\u4e8eB\u6765\u8bf4\uff0c\u9700\u8981\u77e5\u9053ES\u5982\u4f55\u8868\u793a\u8fd9\u4e9b\u590d\u5408\u641c\u7d22\u3002<\/p> \n

(A must B ) or ( C must D) \u7684 ES \u8868\u793a\u4e3a\uff1a<\/p> \n

{"query":{"bool":{"should":[{"bool":{"must":[{"term":{"shop_id":63077}},{"terms":{"state":[1,2,3,4,5]}}]}},{"bool":{"must":[{"term":{"shop_id":63077}},{"range":{"book_time":{"gt":1516550400}}},{"terms":{"order_tags":["IS_SECURED_TRANSACTIONS"]}}]}}],"minimum_should_match":1}},"from":0,"size":10}<\/pre> \n 

( A or B ) must ( C or D ) \u7684 ES \u8868\u793a\u662f\uff1a<\/p> \n

bool:{must:[{bool:{should:[{A},{C}],minimum_should_match: 1}},{bool:{should:[{D},{B}], minimum_should_match: 1}}]}<\/pre> \n 

\u7ec4\u5408\u6a21\u5f0f\u7684\u8981\u70b9\u662f\uff1a\u539f\u5b50\u6761\u4ef6\u548c\u590d\u5408\u6761\u4ef6\u5177\u5907\u76f8\u540c\u7684\u884c\u4e3a\u63a5\u53e3\uff0c\u4ece\u800c\u80fd\u591f\u7ec4\u5408\u548c\u53e0\u52a0\u3002<\/p> \n

\u5b9e\u73b0<\/h3> \n

\u7ec4\u5408\u6a21\u5f0f<\/h4> \n

STEP1\uff1a \u9996\u5148\u5b9a\u4e49 Condition \u63a5\u53e3\uff0c \u76ee\u524d\u4ec5\u652f\u6301 \u4e0e \u548c \u6216 \u64cd\u4f5c\uff0c\u4ee5\u53ca\u67e5\u8be2\u5bf9\u8c61\u8f6c\u6362\u3002<\/p> \n

\/**\r\n * Created by shuqin on 18\/2\/7.\r\n *\/\r\npublic interface Condition {\r\n\r\n  Condition and(Condition c);\r\n  Condition or(Condition c, Integer shouldMinimumMatch);\r\n  Map expr();    \/\/ ES \u67e5\u8be2\u5bf9\u8c61\r\n\r\n  default String json() {\r\n    return JSON.toJSONString(this);\r\n  }\r\n\r\n}<\/pre> \n 

STEP2\uff1a \u539f\u5b50\u6761\u4ef6 EsCondition \u5b9e\u73b0<\/p> \n

package zzz.study.patterns.composite.escondition;\r\n\r\nimport com.google.common.collect.ImmutableMap;\r\nimport com.google.common.collect.Lists;\r\n\r\nimport java<\/a>.io.Serializable;\r\nimport java<\/a>.util.List;\r\nimport java.util.Map;\r\n\r\nimport lombok.Data;\r\n\r\n\/**\r\n * Created by shuqin on 18\/2\/8.\r\n *\/\r\n@Data\r\npublic class EsCondition implements Condition, Serializable {\r\n\r\n  private static final long serialVersionUID = -209082552315760372L;\r\n\r\n  \/** ES \u5b57\u6bb5\u540d\u79f0 *\/\r\n  private String fieldName;\r\n\r\n  \/** \u5339\u914d\u7b26 *\/\r\n  private Op op;\r\n\r\n  \/**\r\n   *\r\n   * \u8981\u5339\u914d\u7684\u503c,\u7528\u4e8e eq, neq, range, in, match\r\n   *\r\n   * eq \u4f20 \u5355\u4e2a\u503c\u5bf9\u8c61\uff0c\u6bd4\u5982 Integer, String , etc\r\n   * in \u4f20 List \u5bf9\u8c61\r\n   * range \u4f20 Range \u5bf9\u8c61\r\n   * match \u4f20 Match \u5bf9\u8c61\r\n   *\r\n   *\/\r\n  private Object value;\r\n\r\n  public EsCondition() {\r\n  }\r\n\r\n  public EsCondition(String fieldName, Op op, Object value) {\r\n    this.fieldName = fieldName;\r\n    this.op = op;\r\n    this.value = value;\r\n  }\r\n\r\n  public String getFieldName() {\r\n    return fieldName;\r\n  }\r\n\r\n  public Op getOp() {\r\n    return op;\r\n  }\r\n\r\n  public Object getValue() {\r\n    return value;\r\n  }\r\n\r\n  @Override\r\n  public String toString() {\r\n    return "EsCondition{" +\r\n           "fieldName='" + fieldName + '\\'' +\r\n           ", op=" + op +\r\n           ", value=" + value +\r\n           '}';\r\n  }\r\n\r\n  @Override\r\n  public Condition and(Condition c) {\r\n    return new CompositeMustCondition(Lists.newArrayList(c, this));\r\n  }\r\n\r\n  @Override\r\n  public Condition or(Condition c, Integer shouldMinimumMatch) {\r\n    List<Condition> shouldConditions = Lists.newArrayList(c, this);\r\n    return new CompositeShouldCondition(shouldConditions, shouldMinimumMatch);\r\n  }\r\n\r\n  private static Map<String, String> op2EsKeyMap = ImmutableMap.of(\r\n      Op.eq.name(), "term",\r\n      Op.neq.name(), "term",\r\n      Op.in.name(), "terms",\r\n      Op.range.name(), "range",\r\n      Op.match.name(), "match"\r\n  );\r\n\r\n  @Override\r\n  public Map expr() {\r\n    return buildEsExpr(op2EsKeyMap.get(op.name()));\r\n  }\r\n\r\n  private Map buildEsExpr(String esKey) {\r\n    r","orderid":"0","title":"\u8fd0\u7528\u7ec4\u5408\u6a21\u5f0f\u5b9e\u73b0\u590d\u5408\u641c\u7d22\u6761\u4ef6\u6784\u5efa(\u4e00)","smalltitle":"","mid":"0","fname":"JAVA","special_id":"0","bak_id":"0","info":"0","hits":"552","pages":"3","comments":"0","posttime":"2018-02-22 14:32:41","list":"1519281161","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u8fd0\u7528<\/A> \u7ec4\u5408<\/A> \u6a21\u5f0f<\/A> \u5b9e\u73b0<\/A> \u590d\u5408<\/A> \u641c\u7d22<\/A> \u6761\u4ef6<\/A> \u6784\u5efa<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.20.29.21","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u8fd0\u7528\u7ec4\u5408\u6a21\u5f0f\u5b9e\u73b0\u590d\u5408\u641c\u7d22\u6761\u4ef6\u6784\u5efa","lastview":"1706126287","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}