2017-06-17 102 views
0

我是elasticsearch的初學者,我不知道解決我的問題。 這裏是我的問題:ElasticSearch多個字段

{"email"=>"[email protected]", 
"full_name"=>"Lilla Fritz Ritchie", 
"location"=>"Connecticut", 
"postition"=>"Developer", 
"about"=> 
"Atque nihil dolorum et quod ea aut debitis. Aliquid commodi eos et architecto nostrum. Rerum aut sunt sed molestiae consequatur. Ut ut est id voluptatem maiores. Sed voluptate aut consequatur asperiores reprehenderit iure optio.", 
"skills"=> 
[{"name"=>"SQLServer", "year_experience"=>2}, 
    {"name"=>"MySQL", "year_experience"=>6}, 
    {"name"=>"Aps.net", "year_experience"=>7}, 
    {"name"=>"Ruby on Rails", "year_experience"=>8}, 
    {"name"=>"PHP", "year_experience"=>4}, 
    {"name"=>"Amazon EC2", "year_experience"=>4}], 
"id"=>3} 

這是ES用戶的文件,我需要找到技能和相應的年經驗的人。 示例:我的投入是Ruby on Rails 8年的經驗和具有5年以上經驗的亞馬遜EC2技能。

=>期望的結果:

  • 有Ruby on Rails的技巧和ORDER BY年的經驗
  • 有亞馬遜EC2技能和ORDER BY年的經驗。

任何人都可以幫助我如何爲這種情況編寫ES查詢。對不起,我的英語不好。

+0

我假設您正在尋找「我的輸入是Ruby on Rails 8年經驗和亞馬遜EC2技能以及5年經驗」的技能。 – user3775217

回答

0

我不確定我是否理解你的映射。我希望以下查詢能夠回答你的問題。

{ 
     "query": { 
      "filtered": { 
        "filter": 
         "bool": { 
          "term": { 
           "skills.name": "Ruby" 
          } 
         } 
        } 
      } 
     }, 
     "sort": { 
      "skills.year_experience": { 
        "order": "asc" 
      } 
     } 
    } 

您還可以更改ascdesc如果排序是不正確的順序。

0

我看了一下你的json文檔,我建議你先爲技能數據類型強制執行nested數據類型。

此外,在默認情況下,映射是標準分析器,它將打破你的輸入空白,即「Ruby on Rails」將導致「紅寶石」,「上」,「軌道」和standard分析儀不會保留倒序索引中的實際字段,除非您創建自定義分析器以將關鍵字小寫或在映射中使用multi-fields以支持搜索其他用例。

在這裏,我已經提供了最小的映射和您當前使用的用例的設置,您可以擴展它以支持其他用例。

我還建議你看看彈性的nested查詢。

映射

PUT index_playtime1 
{ 
    "settings": { 
     "analysis": { 
      "analyzer": { 
       "lowercase_keyword": { 
        "tokenizer": "keyword", 
        "filter": ["lowercase"] 
       } 
      } 
     } 
    }, 
    "mappings": { 
     "document_type": { 
      "properties": { 
       "skills": { 
        "type": "nested", 
        "properties": { 
         "name": { 
          "type": "text", 
          "analyzer": "lowercase_keyword" 
         } 
        } 
       } 
      } 
     } 
    } 
} 

查詢

POST index_playtime1/document_type/_search 


    { 
    "sort": { 
     "skills.year_experience": { 
      "order": "asc" 
     } 
    }, 
    "query": { 
     "bool": { 
      "must": [{ 
        "nested": { 
         "path": "skills", 
         "query": { 
          "bool": { 
           "must": [{ 
            "bool": { 
             "must": [{ 
               "term": { 
                "skills.name": { 
                 "value": "ruby on rails" 
                } 
               } 
              }, 
              { 
               "term": { 
                "skills.year_experience": { 
                 "value": 8 
                } 
               } 
              } 
             ] 
            } 
           }] 
          } 
         } 
        } 
       }, 
       { 
        "nested": { 
         "path": "skills", 
         "query": { 
          "bool": { 
           "must": [{ 
            "bool": { 
             "must": [{ 
               "term": { 
                "skills.name": { 
                 "value": "amazon ec2" 
                } 
               } 
              }, 
              { 
               "term": { 
                "skills.year_experience": { 
                 "value": 4 
                } 
               } 
              } 
             ] 
            } 
           }] 
          } 
         } 
        } 
       } 
      ] 
     } 
    } 
} 

希望這有助於。

讓我們知道如果這不包含您的用例。