ElasticSearch系列學習
ElasticSearch第五步-.net平臺下c#操作ElasticSearch詳解
注意:以下命令都是使用sense測試(ElasticSearch第二步-CRUD之Sense),且資料都已經使用過IK分詞。
以下測試資料來源於文件(db_test/person)
需要注意的是下面的id是文件的ID,不是elasticsearch生成的_id,刪除文件需要用_id
{
"id": "0959ab1c-47bf-4417-904c-e5bc774ce730",
"name": "王軍華",
"age": ,
"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":"","to":"" }
}
},
{
"term":{
"intro":"研究"
}
}]
}
}
}
多欄位多分詞欄位並且分詞或者條件
查詢索引庫db_test表person中age大於500,intro包含"研究"或者"方鴻漸"的資料
POST /db_test/person/_search
{
"query":{
"bool":{
"must":[
{
"range":{
"age":{
"gt":""
}
}
},
{
"terms":{
"intro":["研究","方鴻漸"]
}
}]
}
}
}
分頁/排序/高亮顯示
說明:size如果不寫預設是10,from如果不寫預設是0。取前20條資料,按照age升序,生日降序,並且北京高亮顯示。
POST /db_test/person/_search
{
"query": {
"term":{"intro":"北京"
}
},
"from": ,
"size": ,
"sort":{
"age" : {"order" : "asc"},
"birthday": {"order" : "desc"} } ,
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"intro" : {}
}
}
}
還可以這樣寫
GET /_search?size=
GET /_search?size=&from=
GET /_search?size=&from=
單欄位多分詞或者關係查詢
說明:查詢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/
{
"title":"我是中國人",
"content":"我愛北京和天安門"
}
PUT /db_news/news/
{
"title":"我是中國人",
"content":"朋友在一起就是親家"
}
PUT /db_news/news/
{
"title":"親家的朋友",
"content":"我們明天去玩吧"
}
PUT /db_news/news/
{
"title":"朋友在一起吃飯",
"content":"我們明天去玩吧"
}
PUT /db_news/news/
{
"title":"朋友在一起吃飯",
"content":"親家的朋友在一起吃飯"
}
PUT /db_news/news/
{
"title":"在阿薩德",
"content":"親家的在一起"
}
PUT /db_news/news/
{
"title":"在一起",
"content":"按時到崗"
}
PUT /db_news/news/
{
"title":"在一起吃飯",
"content":"朋友啊吃飯"
}
PUT /db_news/news/
{
"title":"在一起吃飯",
"content":"朋友啊吃飯"
}
POST /db_news/news/_search
{
"query":{
"bool":{
"should":[
{
"bool": {
"must":[
{ "term":{"title":"朋友"} },
{
"term":{"title":"吃飯"}
}
]
}
}
],
"should":[
{
"bool": {
"must":[
{ "term":{"content":"朋友"} },
{
"term":{"content":"吃飯"}
}
]
}
}
]
}
}
}
第二種寫法
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/
{
"title":"第一條新聞",
"content":"我愛北京天安門"
}
測試查詢
GET /_mget
{
"docs" : [
{
"_index" : "db_news",
"_type" : "news",
"_id" :
},
{
"_index" : "db_test",
"_type" : "person",
"_id" : "5bd94e88-10cb-4e9f-9ba6-df8ff8b59081"
}
]
}
查詢包含一組id的文件集合
GET /db_news/news/_mget
{
"ids" : [ "", "" ]
}
批量操作
說明:可以批量新增資料,詳情:http://es.xiaoleilu.com/030_Data/55_Bulk.html
POST /_bulk
{ "create": { "_index": "db_news", "_type": "news", "_id": "" }}
{ "title" : "[email protected]", "content" : "John Smith" }
{ "create": { "_index": "db_news", "_type": "news", "_id": "" }}
{ "title" : "[email protected]", "content" : "John Smidth" }
ElasticSearch系列學習
ElasticSearch第五步-.net平臺下c#操作ElasticSearch詳解