简体   繁体   English

Lucene 与 Elasticsearch 查询语法

[英]Lucene vs Elasticsearch query syntax

I can see that Elasticsearch support both Lucene syntax and it's own query language.我可以看到 Elasticsearch 支持 Lucene 语法和它自己的查询语言。

You can use both and get same kinds of results.您可以同时使用两者并获得相同类型的结果。

Example (might be done differently maybe but to show what I mean):示例(可能会有不同的做法,但为了说明我的意思):

Both of these queries produce the same result but use Lucene or Elastic query syntax.这两个查询产生相同的结果,但使用 Lucene 或弹性查询语法。

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

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

I was wondering are there any kind of implications when choosing one approach over the other (like performance or some kinds of optimizations)?我想知道在选择一种方法而不是另一种方法时有什么影响(比如性能或某些优化)? Or is Elastic query syntax just translated to Lucene query somewhere since Elastic runs Lucene as its underlying search engine?或者自从 Elastic 运行 Lucene 作为其底层搜索引擎以来,Elastic 查询语法是否只是在某处翻译为 Lucene 查询?

I was wondering are there any kind of implications when choosing one approach over the other (like performance or some kinds of optimizations)?我想知道在选择一种方法而不是另一种方法时有什么影响(比如性能或某些优化)?

Elasticsearch DSL will convert into Lucene query under the hood, you can set "profile":true in the query to see how that works and exactly how much time it takes to convert. Elasticsearch DSL 将在后台转换为 Lucene 查询,您可以在查询中设置"profile":true以查看其工作原理以及转换所需的确切时间。

I would say there are no important performance implications and you should always use the DSL, because in many cases Elasticsearch will do optimizations for you.我会说没有重要的性能影响,您应该始终使用 DSL,因为在许多情况下 Elasticsearch 会为您进行优化。 Also, query_string will expect well written Lucene queries, and you can have syntax errors (try doing "Denmark AND" as query_string.此外,query_string 将期望编写良好的 Lucene 查询,并且您可能会遇到语法错误(尝试将“Denmark AND”作为 query_string。

Or is Elastic query syntax just translated to Lucene query somewhere since Elastic runs Lucene as its underlying search engine?或者自从 Elastic 运行 Lucene 作为其底层搜索引擎以来,Elastic 查询语法是否只是在某处翻译为 Lucene 查询?

Yes.是的。 You can try it yourself:你可以自己试试:

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

will produce:将产生:

{
  "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": []
      }
    ]
  }
}

And

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

Will produce the same会产生相同的

{
  "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": []
      }
    ]
  }
}

As you see, times are in nanoseconds, not even miliseconds, that says conversion is fast.如您所见,时间以纳秒为单位,甚至不是毫秒,这表示转换很快。

You can read more about here:您可以在此处阅读更多信息:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-profile.html 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