ElasticSearch使用教程四(ElasticSearch查詢詳解)

一、簡介說明

注意:以下命令都是使用sense測試(ElasticSearch第二步-CRUD之Sense),且數據都已經使用過IK分詞。

以下測試數據來源於文檔(db_test/person)

需要注意的是下面的id是文檔的ID,不是elasticsearch生成的_id,刪除文檔需要用_id

複製代碼
{ "id": "0959ab1c-47bf-4417-904c-e5bc774ce730", "name": "王軍華", "age": 265, "sex": true, "birthday": "2015-04-07T18:11:35.2655732+08:00", "intro":"介紹" }
複製代碼

 

注意:must和should並列,should無效


二、各類查詢示例


查詢所有索引庫的數據

POST /_search

 

查詢索引庫db_test中的所有數據

說明:db_test代表所有庫(db)

POST /db_test/_search

 

查詢索引庫db_test表person中的所有的數據

說明:db_test代表所有庫(db),person代表文檔

POST /db_test/person/_search 

 

查詢索引庫db_test表person中age=3的一條數據

說明:db_test代表所有庫(db),person代表文檔



POST /db_test/person/_search?q=age:3 //或者這樣寫(DSL寫法) POST /db_test/person/_search { "query": { "query_string": { "fields": ["age"], "query": 3 } } }


多字段單分詞字段並且條件查詢

查詢索引庫db_test表person中age從500到800,intro包含"研究"的數據

複製代碼
POST /db_test/person/_search
{ "query":{ "bool":{ "must":[
                { "range":{ "age":{ "from":"500","to":"800" }
                    }                
                },
                { "term":{ "intro":"研究" }
                }]
        }
    }
}
複製代碼

 

多字段多分詞字段並且分詞或者條件

查詢索引庫db_test表person中age大於500,intro包含"研究"或者"方鴻漸"的數據

複製代碼
POST /db_test/person/_search
{ "query":{ "bool":{ "must":[
                { "range":{ "age":{ "gt":"500" }
                    }                
                },
                { "terms":{ "intro":["研究","方鴻漸"]
                    }
                }]
        }
    }
}
複製代碼

 

分頁/排序/高亮顯示

說明:size如果不寫默認是10,from如果不寫默認是0。取前20條數據,按照age升序,生日降序,並且北京高亮顯示。

複製代碼
POST /db_test/person/_search
{ "query": { "term":{"intro":"北京" }
    }, "from": 0, "size": 20, "sort":{ "age" : {"order" : "asc"}, "birthday": {"order" : "desc"}
        
    } , "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "intro" : {}
        }
    }
}
複製代碼

 還可以這樣寫

GET /_search?size=5 GET /_search?size=5&from=5 GET /_search?size=5&from=10

 

 

單字段多分詞或者關係查詢

說明:查詢intro包含"研究"或者"方鴻漸"。

(這在全文檢索時非常有用)

第一種寫法:



POST /db_test/person/_search { "query":{ "bool":{ "must":[ { "query_string":{ "default_field":"intro", "query":"研究方鴻漸" } } ] } }, "sort": [ { "birthday": { "order": "desc" } } ]}


第二種寫法:



POST /db_test/person/_search { "query": { "query_string": { "fields": ["intro"], "query": "研究方鴻漸" } }, "sort": [ { "birthday": { "order": "asc" } } ] }



第三種寫法:



POST /db_test/person/_search { "query":{ "bool":{ "should":[ { "term":{"intro":"研究"} }, { "term":{"intro":"方鴻漸"} } ] } }, "sort": [ { "birthday": { "order": "desc" } } ] }


單字段多分詞並且關係查詢

 intro包含"研究""方鴻漸"的寫法如下:



POST /db_test/person/_search { "query":{ "bool":{ "must":[ { "term":{"intro":"研究"} }, { "term":{"intro":"方鴻漸"} } ] } } }


 

多字段多分詞 字段或者分詞並且關係查詢

說明:查詢 title中包含"朋友"並且包含"喫飯" 或者 content中包含"朋友"並且包含"喫飯"  title和content中只要有一箇中包含"朋友"並且"喫飯"兩個關鍵字就行

複雜條件的範例-----------------------

插入測試數據



PUT /db_news/news/1 { "title":"我是中國人", "content":"我愛北京和天安門" } PUT /db_news/news/2 { "title":"我是中國人", "content":"朋友在一起就是親家" } PUT /db_news/news/3 { "title":"親家的朋友", "content":"我們明天去玩吧" } PUT /db_news/news/4 { "title":"朋友在一起喫飯", "content":"我們明天去玩吧" } PUT /db_news/news/5 { "title":"朋友在一起喫飯", "content":"親家的朋友在一起喫飯" } PUT /db_news/news/6 { "title":"在阿薩德", "content":"親家的在一起" } PUT /db_news/news/7 { "title":"在一起", "content":"按時到崗" } PUT /db_news/news/8 { "title":"在一起喫飯", "content":"朋友啊喫飯" } PUT /db_news/news/9 { "title":"在一起喫飯", "content":"朋友啊喫飯" }


第一種寫法


POST /db_news/news/_search { "query":{ "bool":{ "should":[ { "bool": { "must":[ { "term":{"title":"朋友"} }, { "term":{"title":"喫飯"} } ] } } ], "should":[ { "bool": { "must":[ { "term":{"content":"朋友"} }, { "term":{"content":"喫飯"} } ] } } ] } } }


 第二種寫法(參考http://www.elasticsearch.cn/guide/reference/query-dsl/query-string-query.html



POST /db_news/news/_search { "query":{ "bool":{ "should":[ { "query_string":{ "default_field":"title", "query":"朋友喫飯", "default_operator":"AND", "analyzer":"ik" } }, { "query_string":{ "default_field":"content", "query":"朋友喫飯", "default_operator":"AND", "analyzer":"ik" } } ] } } }



多字段多分詞  字段或者分詞或者關係查詢

 查詢 title中包含"朋友"或者包含"喫飯" 或者 content中包含"朋友"或者包含"喫飯"  也就是說只有title和content中包含"朋友"或者"喫飯"兩個關鍵字就行

 第一種寫法:



POST /db_news/news/_search { "query":{ "bool":{ "should":[ { "term":{"title":"朋友"} }, { "term":{"title":"喫飯"} }, { "term":{"content":"朋友"} }, { "term":{"content":"喫飯"} } ] } } }


第二種寫法:



POST /db_news/news/_search { "query":{ "bool":{ "should":[ { "terms":{"title":["朋友","喫飯"]} }, { "terms":{"content":["朋友","喫飯"]} } ] } } }


第三種寫法:(對於全文檢索比較簡單:比如招聘網站,搜索公司名或者職位名包含關鍵字asp.NET 或者mvc這兩個關鍵字任意一個)



POST /db_news/news/_search { "query":{ "bool":{ "should":[ { "query_string":{ "default_field":"title", "query":"朋友喫飯" } }, { "query_string":{ "default_field":"content", "query":"朋友喫飯" } } ] } } }


 

查詢部分字段數據 

說明:只查詢age和intro

POST /db_test/person/_search?_source=age,intro

同時查詢多個不同的文檔

 插入測試數據



PUT /db_news { "settings" : { "analysis" : { "analyzer" : { "stem" : { "tokenizer" : "standard", "filter" : ["standard", "lowercase", "stop", "porter_stem"] } } } }, "mappings" : { "news" : { "dynamic" : true, "properties" : { "title" : { "type" : "string", "indexAnalyzer" : "ik", "searchAnalyzer":"ik" }, "content" : { "type" : "string", "indexAnalyzer" : "ik", "searchAnalyzer":"ik" } } } } } PUT /db_news/news/1 { "title":"第一條新聞", "content":"我愛北京天安門" }


測試查詢

複製代碼
GET /_mget
{ "docs" : [
      { "_index" : "db_news", "_type" : "news", "_id" : 1 },
      { "_index" : "db_test", "_type" : "person", "_id" : "5bd94e88-10cb-4e9f-9ba6-df8ff8b59081" }
   ]
}
複製代碼

 

查詢包含一組id的文檔集合

GET /db_news/news/_mget
{ "ids" : [ "2", "1" ]
}

批量操作

說明:可以批量添加數據,詳情:http://es.xiaoleilu.com/030_Data/55_Bulk.html

POST /_bulk
{ "create": { "_index": "db_news", "_type": "news", "_id": "3" }}
{ "title" : "[email protected]", "content" : "John Smith" }
{ "create": { "_index": "db_news", "_type": "news", "_id": "4" }}
{ "title" : "[email protected]", "content" : "John Smidth" }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章