按照这个链接这里。有一个名为“短语Suggestor”的概念,它使用一些N方法给出类似于自动完成的建议。我试图了解如何使用Python提供的这里文档所找到的api。但我找不到任何提到n克或短语暗示者的东西。
这个方法是否存在于中?我知道NLTK和那里的n-gram方法。
这是我所拥有的。
首先连接,这段代码可以正常工作。
from elasticsearch import Elasticsearch
CLOUD_ID = 'My_deployment:...'
ELASTIC_PASSWORD = 'password'
es = Elasticsearch(cloud_id=CLOUD_ID,
basic_auth=("elastic", ELASTIC_PASSWORD))
第二个块不工作。
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)
错误消息是
<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似乎也无法识别它。
更新
我终于能让它开始工作了,但我对输出结果感到困惑。
{'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': []}]}}
推荐信在哪里?也就是自动完成完成句子?
发布于 2022-04-08 04:44:25
您可以首先创建索引映射,因此在生成NGram后不需要依赖外部python和Elasticsearch字段。
指数映射
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,并提供与文档中提到的相同的正文。
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创建索引。
https://stackoverflow.com/questions/71788982
复制相似问题