试试搜索一下吧

JAVA操作ES使用OR

Elasticsearch | 作者 ws853055677 | 发布于2020年09月14日 | 阅读数:2111

现在需求是首先是必要字段,然后是OR连接的,大概SQL就是select * from search_product where status=1 and endTime>now() and sDealType =1     (and  productFromType=3 and productStatus=2 or productFromType=1 or productFromType=2)
sql的意思就是如果productFromType=3 则必须productStatus必须=2,如果productFromType=1或者=2的时候则没有要求。
我写的ES语句如下:
GET /search_product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "productName.keyword": {
              "value": "*溜溜梅雪梅*"
            }
          }
        },{
          "range": {
            "endTime": {
              "gt": 1600063626974
            }
          }
        }
      ],
      "should": [
        {
          "term": {
            "productFromType": {
              "value": "1"
            }
          }
        },{
          "term": {
            "productFromType": {
              "value": "2"
            }
          }
        },{
          "bool": {
            "must": [
              {
                "term": {
                "productFromType": {
                  "value": "3"
                }
              }
                
              }, {
                "term": {
                "productStatus": {
                  "value": "2"
                }
              }
                
              }
            ]
          }
        }
      ]
    }
  }

}
 
但是不知道java要如何写。现在写的JAVA如下:
但是这个写出来不对。主要错在 后边。打印出来的结果如下:
GET /search_product/_search
{
  "query": {
 
  "bool" : {
    "must" : [
      {
        "term" : {
          "status" : {
            "value" : 1,
            "boost" : 1.0
          }
        }
      },
      {
        "range" : {
          "endTime" : {
            "from" : 1600065172087,
            "to" : null,
            "include_lower" : false,
            "include_upper" : true,
            "boost" : 1.0
          }
        }
      },
      {
        "term" : {
          "sDealType" : {
            "value" : 1,
            "boost" : 1.0
          }
        }
      },
      
      {
        "wildcard" : {
          "productName.keyword" : {
            "wildcard" : "*测试秒杀*",
            "boost" : 1.0
          }
        }
      }
    ],
    "should" : [
      {
        "term" : {
          "productFromType" : {
            "value" : 1,
            "boost" : 1.0
          }
        }
      },
      {
        "term" : {
          "productFromType" : {
            "value" : 2,
            "boost" : 1.0
          }
        }
      },
      {
        "term" : {
          "productFromType" : {
            "value" : 3,
            "boost" : 1.0
          }
        }
      }

    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

}
 
QQ图片20200914151301.png
已邀请:

Ombres

赞同来自:

bool.should(bool.must(term)
.must(term))
.should(term)
.should(term)
类似这样

要回复问题请先登录注册