利用弹性搜索dsl python analyze api

问题描述 投票:1回答:1

如何在弹性搜索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).)*"是如何被打破的?实际上,似乎'!'不能作为正则表达式。如果默认分析器无法将'!'作为正则表达式字符,我如何更改anaylzer?

我很擅长使用,很难在我的代码中准确地使用analyze方法。请帮忙。

python regex elasticsearch elasticsearch-dsl elasticsearch-dsl-py
1个回答
0
投票

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

如果您正在寻找_analyze方法的替代方法,但在Python中,您可以使用elasticsearch-py来实现它,我不确定您是否可以使用Elasticsearch DSL来实现它。所以,假设我想看看我的字符串jestem biały miś如何使用名为morfologik的分析器进行分析的结果。使用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}]}
© www.soinside.com 2019 - 2024. All rights reserved.