1、restful api查詢css
1.一、查詢訂單前端
POST order/_search?q=P201903230001
1.二、更新訂單web
POST order / area / 1 / _update { "doc": { " price": "5", "payee": "tom" } }
1.三、刪除訂單(刪除order索引/庫)sql
DELETE order
1.四、增長訂單json
POST order / area / 1 { " price": "5", "payee": "tom" }
1.五、查詢索引數據,或者索引下某類型數據api
一、 GET /lib/user/_search : 查詢lib索引下的user類型的所有數據 二、 GET /lib/_search :查詢lib索引下的所有類型的數據 三、 GET /_search :查詢所有索引下的數據
2、DSL格式理解數組
2.一、DSL查詢語句屬性值是json數組仍是json,取決於你條件是多個仍是單個。 JSON數據格式靈活性安全
一個條件時候能夠用json must:{ "match": { "title": "Search" } } 多個條件時候能夠用json數組 must:[ { "match": { "title": "Search" }}, { "match": { "content": "Elasticsearch" }} ]
3、Query DSLrestful
Elasticsearch 提供了一個完整的 query DSL,而且是 JSON 形式的多線程
葉子查詢語句(Leaf Query):用於查詢某個特定的字段,如 match , term 或 range 等
複合查詢語句 (Compound query clauses):用於合併其餘的葉查詢或複合查詢語句,也就是說複合語句之間能夠嵌套,用來表示一個複雜的單一查詢
3.一、查詢order索引下全部數據
GET order / area / _search { "query": { "match_all": {} } }
3.二、分詞查詢:訂單、多小、商品,包含這三個詞中的一個或多個的文檔就會被搜索出來
GET / _search { "query": { "match": { "content": { "query": "個人訂單有多小種類商品" } } } }
3.三、分詞查詢:訂單、多小、商品,包含這三個詞中的文檔就會被搜索出來。
match與match_phrase區別:match_phrase匹配到全部分詞才能查詢出來
GET / _search { "query": { "match_phrase": { "content": { "query": "個人訂單有多小種類商品" } } } }
3.四、多字段匹配
GET order / area / _search { "query": { "multi_match": { "query": "this is a test", "fields": ["subject", "message"] } } }
3.五、徹底匹配
GET order / area / _search { "query": { "term": { "NUM": "P201903230001" } } }
3.六、邏輯運算
6.3 邏輯運算 若是有多個搜索關鍵字, Elastic 認爲它們是or關係。 $ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "軟件 系統" }} }' 上面代碼搜索的是軟件 or 系統。 若是要執行多個關鍵詞的and搜索,必須使用布爾查詢。 $ curl 'localhost:9200/accounts/person/_search' -d ' { "query": { "bool": { "must": [ { "match": { "desc": "軟件" } }, { "match": { "desc": "系統" } } ] } } }'
3.七、range範圍查詢。下面查詢出訂單銷售在100-200之間數據
GET order/area/_search { "query":{ "bool": { "must": [ { "range": { "sales": { "from": 100, "to": null } } }, { "range": { "sales": { "from": null, "to": 200 } } } ] } } } 或 GET order/area/_search { "query":{ "bool": { "must": [ { "range": { "sales": { "from": 100, "to": 200 } } } ] } } }
3.八、term精確匹配
3.8.一、term精確匹配:對字段進行確切值(exact value)的查詢,如數字、時間、bool、not_analyzed字段等。
3.8.二、注意:使用term要肯定的是這個字段是否「被分析」(analyzed),默認的字符串是被分析的。字段被分析將查詢不出內容儘管字符串徹底匹配
3.8.三、爲何有時候使用term查不出數據? 只是string類型才查不出,其它類型是能夠的。
緣由:字段被分析將查詢不出內容儘管字符串徹底匹配。須要修改爲:index:not_analyzed:將字段的原始值放入索引中,做爲一個獨立的term,它是除string字段之外的全部字段的默認值。
注意:在 elasticsearch2.x 版本,字符串數據只有string類型。ES更新到5版本後,取消了 string 數據類型,代替它的是 keyword 和 text 數據類型
官方解析
Add the field value to the index unchanged, as a single term. This is the default for all fields that support this option except for string fields. not_analyzed fields are usually used with term-level queries for structured search.
GET /megacorp/employee/_search { "query" :{ "bool": { "must" : [ {"term" : {"NAME":"我是小明"}} ] } } }
實踐:1:
只有分詞徹底匹配、徹底匹配、徹底匹配「大白菜」這三個字後才能夠返回數據。JAVA程序中採用QueryBuilders類的termQuery(String name, Object value)方法。
GET /my_index/ny_type/_search { "query":{ "term":{ "productName":"大白菜" } } }
實踐2:
第一次使用"term":{"productName":"大白菜" } 沒返回數據以爲很疑惑。明明存在數據「今天大白菜很新鮮」 ,確沒匹配到任何數據。
緣由:productName字段type=text而且使用默認分詞器,大白菜 => [大,白,菜]。分詞後變成3個字,而term查詢須要徹底匹配「大白菜」,大白菜做1個分詞去匹配查找數據。
因此最後沒有數據返回。
GET /my_index/ny_type/_search { "query":{ "term":{ "productName":"大白菜" } } }
3.8.四、字段text類型,怎樣可使用term精確查詢?
3.8.4.1. text類型:會分詞,先把對象進行分詞處理,而後再再存入到es中。
當使用多個單詞進行查詢的時候,固然查不到已經分詞過的內容!
3.8.4.2. keyword:不分詞,沒有把es中的對象進行分詞處理,而是存入了整個對象! 這時候固然能夠進行完整地查詢!默認是256個字符!
{ "mapping": { "my_type": { "properties": { "title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } }
GET my_index/my_type/_search { "query":{ "term": { "title.keyword": "elasticsearch is good" } } }
3.九、terms多詞精確匹配
sql中的in: select * from tbl where col in ("value1", "value2") sql中的in,在elasticsearch使用terms實現 term: {"field": "value"} terms: {"field": ["value1", "value2"]}
3.十、Bool Query
使用must語法,bool必須加上
咱們能夠在查詢條件中使用AND/OR/NOT操做符,這就是布爾查詢(Bool Query)。布爾查詢能夠接受一個must參數(等價於AND),一個must_not參數(等價於NOT),以及一個should參數(等價於OR)。好比,我想查詢about中出現music或者climb關鍵字的員工,員工的名字是John,但姓氏不是smith,咱們能夠這麼來查詢:
GET /megacorp/employee/_search { "query": { "bool": { "must": { "bool" : { "should": [ { "match": { "about": "music" }}, { "match": { "about": "climb" }} ] } }, "must": { "match": { "first_nale": "John" } }, "must_not": { "match": {"last_name": "Smith" } } } } } GET /megacorp/employee/_search { "query" :{ "bool": { "must" : [ {"match" : {"NAME":"我是小明"}}, {"match" : {"INTEREST":"足球"}} ] } } }
3.10.一、Bool Query語法
bool過濾:用來合併多個過濾條件的查詢結果的布爾邏輯,必須包含must和should中的一個或多個。它包含如下一些操做 must: 至關於and must_not: 至關於 not should: 至關於or(鏈接多個匹配條件,列表形式) { "query": { "bool":{ "must":{ //and age==50 "term":{"age":50} }, "must_not":{ //not date=2014-09-01 "term":{"date": "2014-09-01"} }, "should":[ // _id==8 or _id=9 (舉的不太恰當) {"term":{"_id":8}}, {"term":{"_id":19}} ] } } } 下面錯誤語法。bool裏面只能有must,must_not,should等相同單詞只能出現一次。 { "query": { "bool":{ "must":{ //and age==50 "term":{"age":50} }, "must":{ //and firstName==jack "term":{"firstName":"jack"} } "must_not":{ //not date=2014-09-01 "term":{"date": "2014-09-01"} }, "should":[ // _id==8 or _id=9 (舉的不太恰當) {"term":{"_id":8}}, {"term":{"_id":19}} ] } } }
3.十一、處理 Null 值:exists/missing
exists/missing做用:用來查找某個字段是否有值, 相似SQL中的 not is_null/is_null
它返回某個特定有值字段的文檔,用 SQL 的話就是用 IS NOT NULL 非空進行查詢: SELECT tags FROM posts WHERE tags IS NOT NULL GET /my_index/posts/_search { "query" : { "constant_score" : { "filter" : { "exists" : { "field" : "tags" } } } } } 這個 missing 查詢本質上與 exists 剛好相反: 它返回某個特定 _無_ 值字段的文檔,與如下 SQL 表達的意思相似: SELECT tags FROM posts WHERE tags IS NULL GET /my_index/posts/_search { "query" : { "constant_score" : { "filter": { "missing" : { "field" : "tags" } } } }
3.十二、檢查語法是否正確
GET my_index/my_type/_validate/query?explain
3.1三、multi_match多字段匹配查詢
//查詢title,content包含elasticsearch GET /website/article/_search { "query": { "multi_match": { "query": "elasticsearch", "fields": ["title","content"] } } }
3.1四、filter使用
//查找書名包含elasticsearch,而且價格在100-200之間 GET book/area/_search { "query": { "bool": { "must": [ {"match": { "name": "elasticsearch" }} ], "filter": { "range": { "price": { "gt": 100, "lt": 200 } } } } } }
3.1五、編寫高亮檢索代碼
public void highlightSearch() throws ParseException { //建立加載配置文件的客戶端工具,用來檢索文檔,單實例多線程安全 ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil(mappath); //設定查詢條件,經過map傳遞變量參數值,key對於dsl中的變量名稱 //dsl中有三個變量 // condition // startTime // endTime Map<String,Object> params = new HashMap<String,Object>(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //設置時間範圍,時間參數接受long值 params.put("startTime",dateFormat.parse("2017-09-02 00:00:00")); params.put("endTime",new Date()); params.put("condition","喜歡唱歌");//全文檢索條件,匹配上的記錄的字段值對應的匹配內容都會被高亮顯示 //執行查詢,demo爲索引表,_search爲檢索操做action ESDatas<Demo> esDatas = //ESDatas包含當前檢索的記錄集合,最多1000條記錄,由dsl中的size屬性指定 clientUtil.searchList("demo/_search",//demo爲索引表,_search爲檢索操做action "testHighlightSearch",//esmapper/demo.xml中定義的dsl語句 params,//變量參數 Demo.class);//返回的文檔封裝對象類型 //獲取總記錄數 long totalSize = esDatas.getTotalSize(); System.out.println(totalSize); //獲取結果對象列表,最多返回1000條記錄 List<Demo> demos = esDatas.getDatas(); for(int i = 0; demos != null && i < demos.size(); i ++){//遍歷檢索結果列表 Demo demo = demos.get(i); //記錄中匹配上檢索條件的全部字段的高亮內容 Map<String,List<Object>> highLights = demo.getHighlight(); Iterator<Map.Entry<String, List<Object>>> entries = highLights.entrySet().iterator(); while(entries.hasNext()){ Map.Entry<String, List<Object>> entry = entries.next(); String fieldName = entry.getKey(); System.out.print(fieldName+":"); List<Object> fieldHighLightSegments = entry.getValue(); for (Object highLightSegment:fieldHighLightSegments){ /** * 在dsl中經過<mark></mark>來標識須要高亮顯示的內容,而後傳到web ui前端的時候,經過爲mark元素添加css樣式來設置高亮的顏色背景樣式 * 例如: * <style type="text/css"> * .mark,mark{background-color:#f39c12;padding:.2em} * </style> */ System.out.println(highLightSegment); } } } }
3.1五、wildcard通配符查詢,中文只能支持關鍵字查詢,即只支持 type=keywork
#有數據返回,wildcard只支持type=keyword GET /my_index/my_type/_search{ "query": { "wildcard": {"name.keyword":"*標準化*"} } } #沒數據返回,wildcard只支持type=keyword GET /my_index/my_type/_search{ "query": { "wildcard": {"name":"*標準化*"} } }