首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python中的Elasticsearch,有没有暗示性的短语?

Python中的Elasticsearch,有没有暗示性的短语?
EN

Stack Overflow用户
提问于 2022-04-07 21:12:38
回答 1查看 351关注 0票数 0

按照这个链接这里。有一个名为“短语Suggestor”的概念,它使用一些N方法给出类似于自动完成的建议。我试图了解如何使用Python提供的这里文档所找到的api。但我找不到任何提到n克或短语暗示者的东西。

这个方法是否存在于中?我知道NLTK和那里的n-gram方法。

这是我所拥有的。

首先连接,这段代码可以正常工作。

代码语言:javascript
运行
复制
from elasticsearch import Elasticsearch
CLOUD_ID = 'My_deployment:...'
ELASTIC_PASSWORD = 'password'

es = Elasticsearch(cloud_id=CLOUD_ID,
    basic_auth=("elastic", ELASTIC_PASSWORD))

第二个块不工作。

代码语言:javascript
运行
复制
text = 'noble prize'
suggest_dictionary = {"simple_phrase" : {
                      'text' : text,
                      "phrase" : {
                          "field" : "title.trigram"
                      }
                    }
                  }

query_dictionary = {'suggest' : suggest_dictionary}

res = es.search(
    index='test',
    body=query_dictionary)
print(res)

错误消息是

代码语言:javascript
运行
复制
<ipython-input-29-05c434577314>:12: DeprecationWarning: The 'body' parameter is deprecated and will be removed in a future version. Instead use individual parameters.
  res = es.search(

---------------------------------------------------------------------------
NotFoundError                             Traceback (most recent call last)
<ipython-input-29-05c434577314> in <module>
     10 query_dictionary = {'suggest' : suggest_dictionary}
     11 
---> 12 res = es.search(
     13     index='test',
     14     body=query_dictionary)

~/anaconda3/lib/python3.8/site-packages/elasticsearch/_sync/client/utils.py in wrapped(*args, **kwargs)
    402                         pass
    403 
--> 404             return api(*args, **kwargs)
    405 
    406         return wrapped  # type: ignore[return-value]

~/anaconda3/lib/python3.8/site-packages/elasticsearch/_sync/client/__init__.py in search(self, index, aggregations, aggs, allow_no_indices, allow_partial_search_results, analyze_wildcard, analyzer, batched_reduce_size, ccs_minimize_roundtrips, collapse, default_operator, df, docvalue_fields, error_trace, expand_wildcards, explain, fields, filter_path, from_, highlight, human, ignore_throttled, ignore_unavailable, indices_boost, lenient, max_concurrent_shard_requests, min_compatible_shard_node, min_score, pit, post_filter, pre_filter_shard_size, preference, pretty, profile, q, query, request_cache, rescore, rest_total_hits_as_int, routing, runtime_mappings, script_fields, scroll, search_after, search_type, seq_no_primary_term, size, slice, sort, source, source_excludes, source_includes, stats, stored_fields, suggest, suggest_field, suggest_mode, suggest_size, suggest_text, terminate_after, timeout, track_scores, track_total_hits, typed_keys, version)
   3697         if __body is not None:
   3698             __headers["content-type"] = "application/json"
-> 3699         return self.perform_request(  # type: ignore[return-value]
   3700             "POST", __path, params=__query, headers=__headers, body=__body
   3701         )

~/anaconda3/lib/python3.8/site-packages/elasticsearch/_sync/client/_base.py in perform_request(self, method, path, params, headers, body)
    319                     pass
    320 
--> 321             raise HTTP_EXCEPTIONS.get(meta.status, ApiError)(
    322                 message=message, meta=meta, body=resp_body
    323             )

NotFoundError: NotFoundError(404, 'index_not_found_exception', 'no such index [test]', test, index_or_alias)

答案提供了使用PUT test设置索引的状态。哪里?不知道..。多么?不知道..。我不熟悉这种语法,Python似乎也无法识别它。

更新

我终于能让它开始工作了,但我对输出结果感到困惑。

代码语言:javascript
运行
复制
{'took': 1, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 0, 'relation': 'eq'}, 'max_score': None, 'hits': []}, 'suggest': {'simple_phrase': [{'text': 'Hi, I need help', 'offset': 0, 'length': 15, 'options': []}]}}

推荐信在哪里?也就是自动完成完成句子?

EN

回答 1

Stack Overflow用户

发布于 2022-04-08 04:44:25

您可以首先创建索引映射,因此在生成NGram后不需要依赖外部python和Elasticsearch字段。

指数映射

代码语言:javascript
运行
复制
PUT test
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "analysis": {
        "analyzer": {
          "trigram": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": ["lowercase","shingle"]
          }
        },
        "filter": {
          "shingle": {
            "type": "shingle",
            "min_shingle_size": 2,
            "max_shingle_size": 3
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "fields": {
          "trigram": {
            "type": "text",
            "analyzer": "trigram"
          }
        }
      }
    }
  }
}

您可以使用下面的python代码作为短语Suggestor,并提供与文档中提到的相同的正文。

代码语言:javascript
运行
复制
from elasticsearch import Elasticsearch
es = Elasticsearch()

text = 'noble prize'
suggest_dictionary = {"simple_phrase" : {
                      'text' : text,
                      "phrase" : {
                          "field" : "title.trigram"
                      }
                    }
                  }

query_dictionary = {'suggest' : suggest_dictionary}

res = es.search(
    index='test',
    body=query_dictionary)
print(res)

更新1:

答案提供了使用PUT测试来设置索引的状态。哪里?不知道..。多么?不知道..。我不熟悉这种语法,Python似乎也无法识别它。

put test用于在Elasticsearch中创建索引。因此,如果您安装了kibana,那么您可以转到dev console并执行它。否则,您也可以在curl命令中使用相同的命令。如果您有exsitig索引,那么您可以给出您的索引名以及test中的索引名。

将展示如何使用curl命令创建索引。

将展示如何使用python创建索引。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71788982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档