2017-02-24 76 views
0

一個多層次的邏輯查詢比方說,我們有這樣的查詢如何執行elasticsearch

(A或B或C)和(X或Y或Z)和(F或G或H)。 ..

我們如何使用Elasticsearch執行此類查詢? 我試過布爾查詢,但我仍然感到困惑必須的,應該......條款以及如何放置不同的條件,這些條款以獲得合理的結果

這裏是我的數據(場型)樣本:

[{ 
     "_index": "training-hub", 
     "_type": "courses", 
     "_id": "58ad8090604aff26df131971", 
     "_score": 1, 
     "_source": { 
      "title": "Getting Started with Vue.js", 
      "description": "In this course, you will learn the basics of Vue.js framework and how to create amazing web applications using this framework", 
      "slug": "hzrthtrjhyjyt--by--Jemli-Fathi", 
      "format": [ 
      "Intra-entreprise" 
      ], 
      "duration": 6, 
      "language": "EN", 
      "requirements": [ 
      "Basics of web development" 
      ], 
      "tags": [], 
      "audience": [ 
      "Web developers" 
      ], 
      "price": 600, 
      "cover": "http://res.cloudinary.com/dqihnnzaj/image/upload/v1487840598/training-hub/k1h0tzciyjflqvtlyr2l.png", 
      "scheduleUrl": "http://res.cloudinary.com/dqihnnzaj/image/upload/v1487765627/training-hub/sozofm68nrwxhta3ga3u.png", 
      "trainer": { 
      "firstname": "Jemli", 
      "lastname": "Fathi", 
      "photo": "https://ucarecdn.com/5923a1bb-3e77-47d0-bd5b-7d07b0f559fe/16487460_1269388943138870_3235853817844339449_o.jpg" 
      }, 
      "courseCategory": { 
      "name": "Game Development" 
      }, 
      "createdAt": "2017-02-22T12:14:08.186Z", 
      "updatedAt": "2017-02-24T10:39:22.896Z" 
     } 
     }, 
     { 
     "_index": "training-hub", 
     "_type": "courses", 
     "_id": "58ad81c0604aff26df131973", 
     "_score": 1, 
     "_source": { 
      "title": "Create your first Laravel application today!", 
      "description": "In this course, you're gonna learn how create fancy web applications using Laravel PHP Framework ...", 
      "slug": "sdvrehtrthrth--by--Jemli-Fathi", 
      "format": [ 
      "Intra-entreprise", 
      "Inter-entreprise" 
      ], 
      "duration": 6, 
      "language": "EN", 
      "requirements": [ 
      "Basics of Web development", 
      "Basics of PHP language" 
      ], 
      "tags": [], 
      "audience": [ 
      "Web developers", 
      "PHP developers" 
      ], 
      "price": 600, 
      "cover": "http://res.cloudinary.com/dqihnnzaj/image/upload/v1487841464/training-hub/dpgqbchualnfc78n69gs.png", 
      "scheduleUrl": "http://res.cloudinary.com/dqihnnzaj/image/upload/v1487765627/training-hub/sozofm68nrwxhta3ga3u.png", 
      "trainer": { 
      "firstname": "Jemli", 
      "lastname": "Fathi", 
      "photo": "https://ucarecdn.com/5923a1bb-3e77-47d0-bd5b-7d07b0f559fe/16487460_1269388943138870_3235853817844339449_o.jpg" 
      }, 
      "courseCategory": { 
      "name": "Web Development" 
      }, 
      "createdAt": "2017-02-22T12:19:12.376Z", 
      "updatedAt": "2017-02-23T09:39:23.414Z" 
     } 
     }, 
     { 
     "_index": "training-hub", 
     "_type": "courses", 
     "_id": "58aead4faecfc31e4559d49b", 
     "_score": 1, 
     "_source": { 
      "title": "Getting Started with Docker", 
      "description": "After taking this course, you will be able to use Docker in real life projects and you can bootstrap your projects into different containers", 
      "slug": "regrehgreh--by--Jemli-Fathi", 
      "format": [ 
      "Intra-entreprise" 
      ], 
      "duration": 5, 
      "language": "EN", 
      "requirements": [ 
      "Basic Linux Shell skills", 
      "Basic knowledge of Linux environment" 
      ], 
      "tags": [], 
      "audience": [ 
      "Dev-Ops", 
      "Web Developers" 
      ], 
      "price": 999, 
      "cover": "http://res.cloudinary.com/dqihnnzaj/image/upload/v1487939101/training-hub/vkqupkiqerq0kgjgd0km.png", 
      "scheduleUrl": "http://res.cloudinary.com/dqihnnzaj/image/upload/v1487842509/training-hub/r9y1qisyfelseeuzgtt3.png", 
      "trainer": { 
      "firstname": "Jemli", 
      "lastname": "Fathi", 
      "photo": "https://ucarecdn.com/5acb1b5f-b550-4560-b085-2d75384e5ec8/13567067_1064268623650904_3773193220506255422_n.jpg" 
      }, 
      "courseCategory": { 
      "name": "Web Development" 
      }, 
      "createdAt": "2017-02-23T09:37:19.758Z", 
      "updatedAt": "2017-02-24T12:31:32.078Z" 
     } 
     }] 

我需要的到底是篩選課程是這樣的:

(組別或類別2 ...)和(格式或FORMAT2 ...)和 (LANGUAGE1或LANGUAGE2)...

回答

1

您可以使用bool queriesterms query結合所期望的行爲

GET _search 
{ 
    "query" : { 
     "bool": { 
     "must": [ //AND 
      { 
      "terms": { 
       "category.name": [ // Category with value VALUE1 or VALUE2 
       "VALUE1", 
       "VALUE2" 
       ] 
      } 
      }, 
      { 
      "terms": { 
       "format": [// format with value FORMAT1 or FORMAT2 
       "FORMAT1", 
       "FORMAT2" 
       ] 
      } 
      }, 
      { 
      "terms": { 
       "language": [// language with value LANG1 or LANG2 
       "LANG1", 
       "LANG2" 
       ] 
      } 
      } 
     ] 
     } 

    } 
} 

此外,請確保該用於term匹配的領域,都not_analyzed。 請從這裏開始閱讀映像:http://www.elasticsearch.org/guide/reference/mapping/

1

Elasticsearch Query String Query的功能與您正在尋找的功能相同。

請看下面的代碼片段從官方的文檔here

GET /_search 
{ 
    "query": { 
     "query_string": { 
      "query": "(content:this OR name:this) AND (content:that OR name:that)" 
     } 
    } 
} 

希望這有助於提取!

+0

謝謝@avr,我試圖做這樣的事情'POST培訓中心/場/ _search { 「查詢」:{ 「QUERY_STRING」:{ 「查詢」:「標題:Web和( AND(格式:'Intra-entreprise')AND(language:EN)「 } } }'但我仍然無法弄清楚,它會返回空白匹配 – jemlifathi

+1

您可以使用示例文檔,查詢和預期結果編輯您的問題,以便我們可以快速找出它! – avr

+0

我使用數據樣本和更多詳細信息更新了我的問題 – jemlifathi

相關問題