簡體   English   中英

Lucene 與 Elasticsearch 查詢語法

[英]Lucene vs Elasticsearch query syntax

我可以看到 Elasticsearch 支持 Lucene 語法和它自己的查詢語言。

您可以同時使用兩者並獲得相同類型的結果。

示例(可能會有不同的做法,但為了說明我的意思):

這兩個查詢產生相同的結果,但使用 Lucene 或彈性查詢語法。

GET /index/_search 
{
  "query": { 
    "bool": { 
      "must": [ 
        {
            "query_string": {
            "query": "field101:Denmark"
          }
        }
      ]
    }
  }
}

GET /index/_search 
{
  "query": {
    "match": {
      "field101": {
       "query": "Denmark"
      }
    }
  }
}

我想知道在選擇一種方法而不是另一種方法時有什么影響(比如性能或某些優化)? 或者自從 Elastic 運行 Lucene 作為其底層搜索引擎以來,Elastic 查詢語法是否只是在某處翻譯為 Lucene 查詢?

我想知道在選擇一種方法而不是另一種方法時有什么影響(比如性能或某些優化)?

Elasticsearch DSL 將在后台轉換為 Lucene 查詢,您可以在查詢中設置"profile":true以查看其工作原理以及轉換所需的確切時間。

我會說沒有重要的性能影響,您應該始終使用 DSL,因為在許多情況下 Elasticsearch 會為您進行優化。 此外,query_string 將期望編寫良好的 Lucene 查詢,並且您可能會遇到語法錯誤(嘗試將“Denmark AND”作為 query_string。

或者自從 Elastic 運行 Lucene 作為其底層搜索引擎以來,Elastic 查詢語法是否只是在某處翻譯為 Lucene 查詢?

是的。 你可以自己試試:

GET test_lucene/_search
{
  "profile": true,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "field101:Denmark"
          }
        }
      ]
    }
  }
}

將產生:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "profile": {
    "shards": [
      {
        "id": "[KGaFbXIKTVOjPDR0GrI4Dw][test_lucene][0]",
        "searches": [
          {
            "query": [
              {
                "type": "TermQuery",
                "description": "field101:denmark",
                "time_in_nanos": 3143,
                "breakdown": {
                  "set_min_competitive_score_count": 0,
                  "match_count": 0,
                  "shallow_advance_count": 0,
                  "set_min_competitive_score": 0,
                  "next_doc": 0,
                  "match": 0,
                  "next_doc_count": 0,
                  "score_count": 0,
                  "compute_max_score_count": 0,
                  "compute_max_score": 0,
                  "advance": 0,
                  "advance_count": 0,
                  "score": 0,
                  "build_scorer_count": 0,
                  "create_weight": 3143,
                  "shallow_advance": 0,
                  "create_weight_count": 1,
                  "build_scorer": 0
                }
              }
            ],
            "rewrite_time": 2531,
            "collector": [
              {
                "name": "SimpleTopScoreDocCollector",
                "reason": "search_top_hits",
                "time_in_nanos": 1115
              }
            ]
          }
        ],
        "aggregations": []
      }
    ]
  }
}

GET /test_lucene/_search 
{
  "profile": true, 
  "query": {
    "match": {
      "field101": {
       "query": "Denmark"
      }
    }
  }
}

會產生相同的

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "profile": {
    "shards": [
      {
        "id": "[KGaFbXIKTVOjPDR0GrI4Dw][test_lucene][0]",
        "searches": [
          {
            "query": [
              {
                "type": "TermQuery",
                "description": "field101:denmark",
                "time_in_nanos": 3775,
                "breakdown": {
                  "set_min_competitive_score_count": 0,
                  "match_count": 0,
                  "shallow_advance_count": 0,
                  "set_min_competitive_score": 0,
                  "next_doc": 0,
                  "match": 0,
                  "next_doc_count": 0,
                  "score_count": 0,
                  "compute_max_score_count": 0,
                  "compute_max_score": 0,
                  "advance": 0,
                  "advance_count": 0,
                  "score": 0,
                  "build_scorer_count": 0,
                  "create_weight": 3775,
                  "shallow_advance": 0,
                  "create_weight_count": 1,
                  "build_scorer": 0
                }
              }
            ],
            "rewrite_time": 3483,
            "collector": [
              {
                "name": "SimpleTopScoreDocCollector",
                "reason": "search_top_hits",
                "time_in_nanos": 1780
              }
            ]
          }
        ],
        "aggregations": []
      }
    ]
  }
}

如您所見,時間以納秒為單位,甚至不是毫秒,這表示轉換很快。

您可以在此處閱讀更多信息:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-profile.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM