总记录数

POST trade_prod_dy_*/_count
{
    "query": {
        "range": {
          "created":{
            "gte": "1646760441000"
          }
        }
    }
}

模糊搜索

-- 对这个查询词不进行分词,必须完全匹配查询词才可以作为结果显示
{
    "query" : {
        "match_phrase" : {
            "字段" : "搜索值"
        }
    }
}

-- 间隔1个单词以内也算是匹配的结果
{
  "query": {
    "match_phrase": {
      "product_name" : {
          "query" : "PHILIPS HX6730",
          "slop" : 1
      }
    }
  }
}

-- 类似 Google 搜索框,关键字输入推荐
-- max_expansions用来限定最多匹配多少个term,优化性能
{
  "query": {
    "match_phrase_prefix": {
      "product_name": "PHILIPS HX",
      "slop": 5,
      "max_expansions": 20
    }
  }
}

-- 对查询的词进行分词,再进行匹配,match_phrase性能没有match好
{
  "query": {
    "match": {"title": "串"}
  },
  "from": 10,
  "size": 10
}
-- 指定字段模糊搜索,从 title 和 tag 属性中去找
{
  "query": {
    "multi_match": {
      "query": "串串",
      "fields": [ "title", "tag"]
    }
  }
}

不分词匹配

{
  "query":{
    "multi_match":{
        "query":"aa",
        "type" : "phrase_prefix",
        "fields":["content_text"]
    }
  }
}

--50%命中其中两个词就返回
{
  "query": {
    "match": {
      "product_name": {
        "query": "java 程序员 书 推荐",
        "minimum_should_match": "50%"
      }
    }
  }
}

-- query_string 语法查询,全局搜索
{
  "query": {
    "query_string": {
      "query": "(水煮肉 and 回锅肉) or 西葫芦"
    }
  }
}

filter查询

-- filter查询,必须相等,过滤
{"query": {
    "bool": {
      "filter": {
        "term": {"id": "13"}
      }
    }
 }
}

-- bool使用terms
{"query": {
    "bool": {
      "must": [
       {"terms":{"node_standard_code":[1001,1002,1004,1007]}}
      ]
    }
 }
}

{"query": {
    "bool": {
      "filter": {
       {"terms":{"node_standard_code":[1001,1002,1004,1007]}}
      }
    }
 }
}

范围查询

-- 大于或小于,query 和 filter 一起使用的话,filter 会先执行
-- filter,只查询出搜索条件的数据,不计算相关度分数
-- query,查询出搜索条件的数据,并计算相关度分数,按照分数进行倒序排序
-- filter(性能更好,无排序),无需计算相关度分数,也就无需排序,内置的自动缓存最常使用查询结果的数据
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "product_name": "toothbrush"
        }
      },
      "filter": {
        "range": {
          "price": {
            "gt": 400,
            "lt": 700
          }
        }
      }
    }
  }
}

-- terms 用法,类似于数据库的 in
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "product_name": [
            "toothbrush",
            "shell"
          ]
        }
      }
    }
  }
}

空值查询

-- tags IS NULL
-- 已废弃
GET /my_index/posts/_search
{
    "query" : {
        "constant_score" : {
            "filter": {
                "missing" : { "field" : "tags" }
            }
        }
    }
}

-- 新版本查询
GET ent_search/_search
{
  "_source": ["eid","ent_name"], 
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "enttype_code"
                }
            }
        }
    }
}

-- tags IS NOT NULL
GET /my_index/posts/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "exists" : { "field" : "tags" }
            }
        }
    }
}

前缀查询

-- 前缀查询tag LIKE 'aa%'
GET /my_index/address/_search
{
    "query": {
        "prefix": {
            "postcode": "W1"
        }
    }
}

-- 全匹配查询:LIKE %word%
{
    "query": {
        "wildcard": {
            "affair_list.affairs_name.keyword": {
                "value": "*%*"
            }
        }
    }
}

-- 特殊字符问题
{
    "query": {
        "wildcard": {
            "affair_list.affairs_name.keyword": {
                "value": "*\\**"
            }
        }
    }
}

函数使用

-- 模拟数据库的LOCATE函数
{
  "query": {
    "bool": {
      "filter": {
        "script": {
           "script": "'10010001'.startsWith(doc['code.keyword'].value)"
        }
      }
    }
  }
}
#if ($locateCode != $null && ''= $locateCode)
  {
    "script": {
      "script": "'$locateCode'.startsWith(doc['code.keyword'].value)"
  },
#end

或者或其它查询

-- bool查询,must:是类似and,must_not:不等于,should:类似or
{
  "query" : {
    "bool": {
      "must": [
         {
           "term": {"user_id": 22}
      	  },
          {
           "term": {"delete_mark": false}
      	  },
         {
          "exists":{"field":"node_end_time"}
         }
      ],
      "must_not":[
        {
           "term": {"node_type": 4}
      	},
        {
           "term": {"node_type": 31}
      	},
        {
           "term": {"node_type": 2}
      	},
        {
           "term": {"node_standard_code": 1008 }
        }  
      ],
      "filter": {
          "range": {
            "modify_date": {
              "gt": 1596195050000
            }
          }
	  }
    }
  }
}
-- should 有一个特殊性,如果组合查询中没有 must 条件,那么 should 中必须至少匹配一个
-- 我们也还可以通过 minimum_should_match 来限制它匹配更多个
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "product_name": "java"
          }
        },
        {
          "match": {
            "product_name": "程序员"
          }
        },
        {
          "match": {
            "product_name": "书"
          }
        },
        {
          "match": {
            "product_name": "推荐"
          }
        }
      ],
      "minimum_should_match": 3
    }
  }
}

-- 实现数据库的OR条件
{
  "query": {
    "bool": {
    "fileter":[
        {"range":{"date.keyword":{"gt":"20170101","lt":"20170201"}}}
    ],
    "must":[
      {
      "bool":{
        "should": [
          {"term": {"A.keyword": "0000000000"}},
          {"term": {"B.keyword": "0000000001"}}
        ]
       }
      }
    ]
    }
  }
}

脚本使用

-- 文档内字段对比
{
  "query":{
    "script": {
        "script": {
            "inline": "doc['rr_mark_id'].value - doc['mr_mark_id'].value == 0",
            "lang": "painless"
        }
    }
  }
}

GET hockey/_search
{
  "query": {
    "match_all": {}
  },
  "sort": {
    "_script": {
      "type": "string",
      "order": "asc",
      "script": {
        "lang": "painless",
        "inline": "doc['first.keyword'].value + ' ' + doc['last.keyword'].value"
      }
    }
  }
}

sort排序

{
    "query" : {
      "bool": {
        "must": [
           {
             "term": {"user_id": $userId}
            },
            {
             "term": {"delete_mark": false}
            },
           {
            "exists":{"field":"node_end_time"}
           }
        ]
      }
    },
    "sort":[
      {
          "mark_id":"asc"
      },
      {
          "sort_code.keyword":"desc"
      }
    ]
}

-- 多字段排序
[
    {
        "sort_code": "asc"
    },
    {
        "sort_code": {
            "order":"desc",
            "missing": 0,
            "unmapped_type": "long"
        }
    }
]

-- 空字段排序,返回指定字段
#if ($sortList != $null && !$sortList.isEmpty())
,"sort":[
     #if ($sortList.affairsServiceName != $null)
        {
            "affairs_service_name.keyword":#if(${sortList.affairsServiceName}) "asc" #else "desc" #end
        },
    #end
    #if ($sortList.sortCode != $null)
        {
           "sort_code":{"order":#if(${sortList.sortCode}) "asc" #else "desc" #end,"missing": "0"}
        },
    #end
    #if ($sortList.sortCode != $null)
        {
           "sort_code":{#if(${sortList.sortCode}) "order":"asc", "missing": "_first" #else "order":"desc", "missing": "_last" #end}
        },
    #end
    {}
]
#end
,"_source" : ["pk_value"]

BBoss使用

-- IF、FOREACH使用
#if ($codes != $null && $codes.size() > 0)
  {
      "bool": {
        "should": [
          #foreach($code in $codes)
             #if ($velocityCount > 0)
              ,
             #end
             {
               "prefix": {
                 "code.keyword": "$code"
               }
             }
          #end
        ]
      }
  },
#end

-- 逗号问题
{
  "query": {
        "bool" : {
          "must": [
            {
              "terms":{
                  "affairs_id":
                    [
                    #foreach($affairsId in $affairsIds)
                      #if ($velocityCount > 0)
                       , $affairsId
                      #else
                       $affairsId
                      #end
                    #end
                   ]
              }
            }
          ]
        }
    },
  "size" : 1000,
  "_source" : ["affairs_id" , "affairs_name"]
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐