前提:項目採用spring-data-elasticsearch 3.2.0spring
1、QueryBuilder 使用記錄elasticsearch
(1)fuzzyQuery
功能:模糊匹配
原理: fuzzy搜索技術,搜索的時候,可能輸入的搜索文本會出現誤拼寫的狀況自動將拼寫錯誤的搜索文本,進行糾正,糾正之後去嘗試匹配索引中的數據糾正在必定的範圍內若是差異大沒法搜索出來ui
Java:spa
xxxRepository.search(QueryBuilders.fuzzyQuery("name", "張三"))
ES寫法:code
GET /my_index/my_type/_search { "query": { "fuzzy": { "text": { "value": "surprize", "fuzziness": 2 // fuzziness 即爲最多糾正兩個字母而後去匹配,默認爲 auto(2) } } } }
(2)matchQueryorm
功能:根據分詞進行匹配blog
Java:索引
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", archiveListFilterDTO.getPersonName()); xxxRepository.search(matchQuery);
ES寫法:ci
GET my_index/my_type/_search { "query": { "match": { "xxx": "Quick Foxes!" } } }
(3)termQueryget
功能:精確查詢 徹底匹配
Java:
TermQueryBuilder termQuery = QueryBuilders.termQuery("cid",archiveListFilterDTO.getPersonCid()); xxxRepository.search(termQuery);
ES寫法:
GET my_index/_search { "query": { "term" : { "cid" : { "value" : "5137376667422s31000000" } } } }
(4)rangeQuery
功能:範圍查詢
Java:
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("dt") .gte(dateFormat.parse(snapMapDTO.getStartTime()).getTime()) .lte(dateFormat.parse(snapMapDTO.getEndTime()).getTime()); xxxRepository.search(rangeQueryBuilder);
ES寫法:
GET my_index/_search { "query": { "range" : { "personFileCreateTime" : { "from" : 1572331788000, "to" : 1572331789000, "include_lower" : true, "include_upper" : true } } } }