Lucene与Elasticsearch查询语法比较

7tofc5zh  于 2022-12-11  发布在  ElasticSearch
关注(0)|答案(1)|浏览(164)

我可以看到Elasticsearch支持Lucene语法和它自己查询语言。
您可以同时使用这两种方法,并获得相同的结果。
示例(可能做得不同,但为了说明我的意思):
这两个查询产生相同的结果,但使用Lucene或Elastic查询语法。

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

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

我想知道,在选择一种方法而不是另一种方法时,是否会有任何暗示(比如性能或某种优化)?或者,Elastic查询语法只是在某个地方转换为Lucene查询,因为Elastic将Lucene作为其底层搜索引擎运行?

628mspwn

628mspwn1#

我想知道,在选择一种方法而不是另一种方法时,是否会有任何影响(如性能或某种优化)?

Elasticsearch DSL将转换成Lucene查询,你可以在查询中设置"profile":true,看看它是如何工作的,以及转换需要多少时间。
我想说这对性能没有重要的影响,你应该总是使用DSL,因为在很多情况下Elasticsearch会为你做优化。而且,query_string会要求编写良好的Lucene查询,你可能会有语法错误(尝试将“丹麦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

相关问题