ESのクエリーインタフェース

4375 ワード

1、query DSLとは
GET /_search
{
    "query": {
        "match_all": {}
    }
}

queryにはどのようなquery_がありますか?nameすべて検索match all
GET /web/info/_search   --       
{
    "query": {
        "match_all": {}
    }
}

照合クエリーmatch
GET /web/info/_search --   content    second   
{
  "query": {
    "match": {
      "content": "second"
    }
  }
}

複数のfiledに○○が含まれているドキュメントをクエリーmulti_match
GET /web/info/_search--   content author_id    xlucas   
{
  "query": {
   "multi_match": {
     "query": "xlucas",
     "fields": ["content","author_id"]
   }
  }
}

範囲クエリーrange
GET /web/info/_search  --  post_date    2018-11-02   
{
  "query": {
   "range": {
     "post_date": {
       "gte": "2018-11-02"
     }
   }
  }
}

正確なクエリーterm
GET /web/info/_search--  post_date    2018-11-02   
{
  "query": {
  "term": {
    "post_date": {
      "value": "2018-11-02"
    }
  }
  }
}

正確な検索term 3
GET /web/info/_search --  post_date   2018-11-02  2018-11-03   
{
  "query": {
  "terms": {
    "post_date": [
      "2018-11-02",
      "2018-11-03"
    ]
  }
  }
}

コンビネーションクエリbool boolの中でこれらのmustを持っていくことができて、must_not,should,filter各サブクエリは、documentの相関スコアを計算し、boolはすべてのスコアを統合して1つのスコアに結合します.filterはスコアを計算しない例です.author_をクエリーします.idにはxlucasが含まれている必要があります.contentにはthirdが含まれていません.titleにはinfoがあり、post_dateは2018-11-02より大きく、2018-11-03より小さいドキュメントです.
GET /web/info/_search 
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "author_id": "xlucas"
        }}
      ],
      "must_not": [
        {"match": {
          "content": "third"
        }}
      ],
      "should": [
        {"match": {
          "title": "info"
        }}
      ],
      "filter": {
       "bool": {
         "must":[{
           "range":{
             "post_date":{
               "gte":"2018-11-02"
             }
           }},{
            "range":{
             "post_date":{
               "lte":"2018-11-03"
             }
           }}
         ]
       }
      }
    }
  }
}

単純なクエリーでフィルタされたデータ
GET /web/info/_search 
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "post_date": {
            "gte": "2018-11-02"
          }
        }
      }
    }
  }
}

ESにおけるクエリーコードチェッカー
GET /web/info/_validate/query?explain  --   author_id   author_i
{
  "query": {
    "match": {
      "author_i": "xlucas"
    }
  }
}

エラーメッセージ
{
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "valid": true,
  "explanations": [
    {
      "index": "web",
      "valid": true,
      "explanation": """+MatchNoDocsQuery("unmapped field [author_i]") #*:*"""
    }
  ]
}

ESでのソートは、デフォルトでは_score降順ソートの場合_scoreは同じですが、ソート方法をカスタマイズしたいです.post_に従ってdateの降順ソート
GET /web/info/_search
{
  "query": {
    "match": {
      "author_id": "xlucas"
    }
  },
  "sort": [
    {
      "post_date": {
        "order": "desc"
      }
    }
  ]
}

filterとqueryの比較
filterは、検索条件に従って必要なデータをフィルタリングするだけで、相関度スコアは計算されず、相関度に何の影響もありませんqueryは、検索条件に対するdocumentの相関度を計算し、写真の関度でソートします一般的に、検索を行っている場合は、検索条件に最も一致するデータを先に返す必要があります.ではquery;いくつかの条件に基づいてデータの一部をフィルタリングし、ソートに関心を持たない場合は、filterを使用します.これらの検索条件でない限り、これらの検索条件に合致するdocumentが前に戻るほど、これらの検索条件はqueryに配置されます.検索条件がdocumentソートに影響を与えたくない場合は、filterに置いてください.
filterとquery性能filterは、相関度スコアを計算する必要がなく、写真の関度スコアでソートする必要がなく、また内蔵の自動cacheがfilterを最もよく使うデータqueryもあります.逆に、相関度スコアを計算するには、スコアでソートし、結果をcacheできません