【问题标题】:make use of Elastic search dsl python analyze api使用 Elastic search dsl python analyze api
【发布时间】:2018-08-10 01:01:04
【问题描述】:

如何在elasticsearch dsl python中使用默认的_analyze?

我的查询如下所示:

query = Q('regexp', field_name = "f04((?!z).)*")
search_obj = Search(using = conn, index = index_name, doc_type = type_name).query(query)
response = search_obj[0:count].execute()

我应该把analyze() method 放在哪里,这样我才能看到我的"f04((?!z).)*" 是如何被分解的?实际上,'!' 似乎不能用作正则表达式。如果默认分析器无法将'!' 用作正则表达式字符,我该如何更改分析器?

我是新手,很难准确地将分析方法放入我的代码中。请帮忙。

【问题讨论】:

    标签: python regex elasticsearch elasticsearch-dsl elasticsearch-dsl-py


    【解决方案1】:

    我不确定您到底想达到什么目标。如果您发布了一个 CURL 查询,它可以满足您的需求,则可以更轻松地将其转换为 Elasticsearch DSl 或 elasticsearch-py 界面。

    如果您正在寻找 _analyze 方法的替代方法,但在 Python 中,您可以使用 elasticsearch-py 来实现它,但我不确定您是否可以使用 Elasticsearch DSL 来实现。因此,假设我想查看使用名为morfologik 的分析器如何分析我的字符串jestem biały miś 的结果。使用 CURL 我会运行:

    $ curl -XGET "http://localhost:9200/morf_texts/_analyze" -H 'Content-Type: application/json' -d'
    {
      "analyzer": "morfologik",
      "text": "jestem biały miś"
    }'
    
    {
      "tokens": [
        {
          "token": "być",
          "start_offset": 0,
          "end_offset": 6,
          "type": "<ALPHANUM>",
          "position": 0
        },
        {
          "token": "biały",
          "start_offset": 7,
          "end_offset": 12,
          "type": "<ALPHANUM>",
          "position": 1
        },
        {
          "token": "miś",
          "start_offset": 13,
          "end_offset": 16,
          "type": "<ALPHANUM>",
          "position": 2
        },
        {
          "token": "misić",
          "start_offset": 13,
          "end_offset": 16,
          "type": "<ALPHANUM>",
          "position": 2
        }
      ]
    }
    

    为了使用 elasticsearch-py 达到相同的结果,您可以运行以下命令:

    from elasticsearch import Elasticsearch
    from elasticsearch.client import IndicesClient
    
    client = Elasticsearch()
    indices_client = IndicesClient(client)
    
    indices_client.analyze(
        body={
            "analyzer": "morfologik",
            "text": "jestem biały miś",
        }
    )
    

    analyze 方法的输出与上面的 CURL 请求相同:

    {'tokens': [{'token': 'być',
       'start_offset': 0,
       'end_offset': 6,
       'type': '<ALPHANUM>',
       'position': 0},
      {'token': 'biały',
       'start_offset': 7,
       'end_offset': 12,
       'type': '<ALPHANUM>',
       'position': 1},
      {'token': 'miś',
       'start_offset': 13,
       'end_offset': 16,
       'type': '<ALPHANUM>',
       'position': 2},
      {'token': 'misić',
       'start_offset': 13,
       'end_offset': 16,
       'type': '<ALPHANUM>',
       'position': 2}]}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多