️ 该客户端已被弃用️ 从企业搜索版本 7.10.0 开始,我们将用户引导至新的企业搜索 Python 客户端并弃用此客户端。
此客户端将与所有 Enterprise Search 7.x 版本兼容,但不与 8.x 版本兼容。我们对该项目的开发工作将仅限于错误修复。所有未来的增强功能都将集中在企业搜索 Python 客户端上。
谢谢你! - 松紧带
第一方 Python 客户端,用于使用 Elastic App Search 构建出色的相关搜索体验。
要安装客户端,请使用 pip:
python - m pip install elastic - app - search
您还可以下载项目源并运行:
python setup . py install
该客户端与 App Search 一起进行版本控制和发布。
为了保证兼容性,请在相应 App Search 实现的主要版本中使用此库的最新版本。
例如,对于 App Search 7.3
,请使用此库的7.3
或更高版本,但不要使用8.0
。
如果您使用的是 swiftype.com 上提供的 App Search SaaS 版本,则应使用客户端的 7.5.x 版本。
使用此客户端假定您已经启动并运行了一个 Elastic App Search 实例。
可以使用base_endpoint
、 api_key
和use_https
参数实例化客户端:
>> > from elastic_app_search import Client
>> > client = Client (
base_endpoint = 'localhost:3002/api/as/v1' ,
api_key = 'private-mu75psc5egt9ppzuycnc2mc3' ,
use_https = False
)
笔记:
[api_key]
验证对 API 的请求。您可以在客户端中使用任何密钥类型,但每种密钥类型都有不同的范围。有关密钥的更多信息,请查看文档。
base_endpoint
必须排除协议并包含api/as/v1
前缀。这通常可以在应用程序搜索仪表板内的“凭据”选项卡中找到。
将use_https
设置为True
或False
具体取决于服务器的配置方式。通常,在localhost
上运行开发时该值为False
,而在生产环境中运行时为True
。
以下是 Elastic Cloud 的配置示例:
>> > from elastic_app_search import Client
>> > client = Client (
base_endpoint = '77bf13bc2e9948729af339a446b06ddcc.app-search.us-east-1.aws.found.io/api/as/v1' ,
api_key = 'private-mu75psc5egt9ppzuycnc2mc3' ,
use_https = True
)
使用 App Search 的 swiftype.com 上提供的 SaaS 版本时,您可以使用host_identifier
而不是base_endpoint
参数来配置客户端。 host_identifier
标识符可以在“凭据”菜单中找到。
>> > from elastic_app_search import Client
>> > host_identifier = 'host-c5s2mj'
>> > api_key = 'private-mu75psc5egt9ppzuycnc2mc3'
>> > client = Client ( host_identifier , api_key )
>> > engine_name = 'favorite-videos'
>> > document = {
'id' : 'INscMGmhmX4' ,
'url' : 'https://www.youtube.com/watch?v=INscMGmhmX4' ,
'title' : 'The Original Grumpy Cat' ,
'body' : 'A wonderful video of a magnificent cat.'
}
>> > client . index_document ( engine_name , document )
{ 'id' : 'INscMGmhmX4' }
>> > engine_name = 'favorite-videos'
>> > documents = [
{
'id' : 'INscMGmhmX4' ,
'url' : 'https://www.youtube.com/watch?v=INscMGmhmX4' ,
'title' : 'The Original Grumpy Cat' ,
'body' : 'A wonderful video of a magnificent cat.'
},
{
'id' : 'JNDFojsd02' ,
'url' : 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' ,
'title' : 'Another Grumpy Cat' ,
'body' : 'A great video of another cool cat.'
}
]
>> > client . index_documents ( engine_name , documents )
[{ 'id' : 'INscMGmhmX4' , 'errors' : []}, { 'id' : 'JNDFojsd02' , 'errors' : []}]
>> > engine_name = 'favorite-videos'
>> > documents = [
{
'id' : 'INscMGmhmX4' ,
'title' : 'Updated title'
}
]
>> > client . update_documents ( engine_name , documents )
>> > engine_name = 'favorite-videos'
>> > client . get_documents ( engine_name , [ 'INscMGmhmX4' ])
[{ 'id' : 'INscMGmhmX4' , 'url' : 'https://www.youtube.com/watch?v=INscMGmhmX4' , 'title' : 'The Original Grumpy Cat' , 'body' : 'A wonderful video of a magnificent cat.' }]
>> > engine_name = 'favorite-videos'
>> > client . list_documents ( engine_name , current = 1 , size = 20 )
{
'meta' : {
'page' : {
'current' : 1 ,
'total_pages' : 1 ,
'total_results' : 2 ,
'size' : 20
}
},
'results' : [{ 'id' : 'INscMGmhmX4' , 'url' : 'https://www.youtube.com/watch?v=INscMGmhmX4' , 'title' : 'The Original Grumpy Cat' , 'body' : 'A wonderful video of a magnificent cat.' }]
}
>> > engine_name = 'favorite-videos'
>> > client . destroy_documents ( engine_name , [ 'INscMGmhmX4' ])
[{ 'id' : 'INscMGmhmX4' , 'result' : True }]
>> > engine_name = 'favorite-videos'
>> > client . get_schema ( engine_name )
{ 'name' : 'text' , 'square_km' : 'number' , 'square_mi' : 'text' }
>> > engine_name = 'favorite-videos'
>> > client . update_schema ( engine_name , { 'square_km' : 'text' })
{ 'square_km' : 'text' }
>> > client . update_schema ( engine_name , { 'square_mi' : 'text' })
{ 'square_km' : 'text' , 'square_mi' : 'text' }
>> > client . update_schema ( engine_name , { 'square_km' : 'number' })
{ 'square_km' : 'number' , 'square_mi' : 'text' }
>> > client . list_engines ( current = 1 , size = 20 )
{
'meta' : {
'page' : {
'current' : 1 ,
'total_pages' : 1 ,
'total_results' : 2 ,
'size' : 20
}
},
'results' : [{ 'name' : 'favorite-videos' }, { 'name' : 'another-engine' }]
}
>> > client . get_engine ( 'favorite-videos' )
{ 'name' : 'favorite-videos' }
>> > client . create_engine ( 'favorite-videos' , 'en' )
{ 'name' : 'favorite-videos' , 'type' : 'default' , 'language' : 'en' }
>> > client . destroy_engine ( 'favorite-videos' )
{ 'deleted' : True }
>> > client . list_synonym_sets ( 'us-national-parks' )
{
'meta' : {
'page' : {
'current' : 1 ,
'total_pages' : 1 ,
'total_results' : 3 ,
'size' : 20
}
},
'results' : [
{
'id' : 'syn-5b11ac66c9f9292013220ad3' ,
'synonyms' : [
'park' ,
'trail'
]
},
{
'id' : 'syn-5b11ac72c9f9296b35220ac9' ,
'synonyms' : [
'protected' ,
'heritage'
]
},
{
'id' : 'syn-5b11ac66c9f9292013220ad3' ,
'synonyms' : [
'hectares' ,
'acres'
]
}
]
}
>> > client . list_synonym_sets ( 'us-national-parks' , size = 1 , current = 1 )
{
'meta' : {
'page' : {
'current' : 1 ,
'total_pages' : 3 ,
'total_results' : 3 ,
'size' : 1
}
},
'results' : [
{
'id' : 'syn-5b11ac66c9f9292013220ad3' ,
'synonyms' : [
'park' ,
'trail'
]
}
]
}
>> > client . get_synonym_set ( 'us-national-parks' , 'syn-5b11ac66c9f9292013220ad3' )
{
'id' : 'syn-5b11ac66c9f9292013220ad3' ,
'synonyms' : [
'park' ,
'trail'
]
}
>> > client . create_synonym_set ( 'us-national-parks' , [ 'park' , 'trail' ])
{
'id' : 'syn-5b11ac72c9f9296b35220ac9' ,
'synonyms' : [
'park' ,
'trail'
]
}
>> > client . update_synonym_set ( 'us-national-parks' , 'syn-5b11ac72c9f9296b35220ac9' , [ 'park' , 'trail' , 'ground' ])
{
'id' : 'syn-5b11ac72c9f9296b35220ac9' ,
'synonyms' : [
'park' ,
'trail' ,
'ground'
]
}
>> > client . destroy_synonym_set ( 'us-national-parks' , 'syn-5b11ac66c9f9292013220ad3' )
{
'deleted' : True
}
>> > client . search ( 'favorite-videos' , 'grumpy cat' , {})
{ 'meta' : { 'page' : { 'current' : 1 , 'total_pages' : 1 , 'total_results' : 2 , 'size' : 10 }, ...}, 'results' : [...]}
>> > client . multi_search ( 'favorite-videos' , [{
'query' : 'cat' ,
'options' : { 'search_fields' : { 'title' : {} }}
},{
'query' : 'dog' ,
'options' : { 'search_fields' : { 'body' : {} }}
}])
[{ 'meta' : {...}, 'results' : [...]}, { 'meta' : {...}, 'results' : [...]}]
>> > client . query_suggestion ( 'favorite-videos' , 'cat' , {
'size' : 10 ,
'types' : {
'documents' : {
'fields' : [ 'title' ]
}
}
})
{ 'results' : { 'documents' : [{ 'suggestion' : 'cat' }]}, 'meta' : { 'request_id' : '390be384ad5888353e1b32adcfaaf1c9' }}
>> > client . click ( engine_name , { 'query' : 'cat' , 'document_id' : 'INscMGmhmX4' })
创建一个仅搜索正文字段的搜索键。
>> > api_key = 'search-xxxxxxxxxxxxxxxxxxxxxxxx'
>> > api_key_name = 'search-key' # This name must match the name of the key above from your App Search dashboard
>> > signed_search_key = Client . create_signed_search_key ( api_key , api_key_name , { 'search_fields' : { 'body' : {}}})
>> > client = Client (
base_endpoint = 'localhost:3002/api/as/v1' ,
api_key = signed_search_key ,
use_https = False
)
>> > client . create_meta_engine (
engine_name = engine_name ,
source_engines = [
'source-engine-1' ,
'source-engine-2'
]
)
{ 'source_engines' : [ 'source-engine-1' , 'source-engine-2' ], 'type' : 'meta' , 'name' : 'my-meta-engine' }
>> > client . add_meta_engine_sources ( 'my-meta-engine' , [ 'source-engine-3' ])
{ 'source_engines' : [ 'source-engine-1' , 'source-engine-2' , 'source-engine-3' ], 'type' : 'meta' , 'name' : 'my-meta-engine' }
>> > client . delete_meta_engine_sources ( 'my-meta-engine' , [ 'source-engine-3' ])
{ 'source_engines' : [ 'source-engine-1' , 'source-engine-2' ], 'type' : 'meta' , 'name' : 'my-meta-engine' }
>> > client . get_api_logs ( 'my-meta-engine' , {
"filters" : {
"date" : {
"from" : "2020-03-30T00:00:00+00:00" ,
"to" : "2020-03-31T00:00:00+00:00"
},
"status" : "429"
}
})
{
'results' : [],
'meta' : {
'query' : '' ,
'filters' : {
'date' : {
'from' : '2020-03-27T00:00:00+00:00' ,
'to' : '2020-03-31T00:00:00+00:00'
},
'status' : '429'
},
'sort_direction' : 'asc' ,
'page' : {
'current' : 1 ,
'total_pages' : 0 ,
'total_results' : 0 ,
'size' : 10
}
}
}
>> > client . get_search_settings ( engine_name = 'us-national-parks' )
{
"search_fields" : {
"name" : {
"weight" : 1
},
"description" : {
"weight" : 1
}
},
"result_fields" : {
"name" : {
"raw" : {}
},
"description" : {
"raw" : {}
}
},
"boosts" : {}
}
>> > client . update_search_settings (
engine_name = 'us-national-parks' ,
search_settings = {
"search_fields" : {
"name" : {
"weight" : 2
},
"description" : {
"weight" : 1
}
},
"result_fields" : {
"name" : {
"raw" : {}
},
"description" : {
"raw" : {}
}
},
"boosts" : {}
}
)
{
"search_fields" : {
"name" : {
"weight" : 2
},
"description" : {
"weight" : 1
}
},
"result_fields" : {
"name" : {
"raw" : {}
},
"description" : {
"raw" : {}
}
},
"boosts" : {}
}
>> > client . reset_search_settings ( engine_name = 'us-national-parks' )
{
"search_fields" : {
"name" : {
"weight" : 1
},
"description" : {
"weight" : 1
}
},
"boosts" : {}
}
python setup . py test
如果某些内容未按预期工作,请提出问题。
最好的选择是阅读文档。
您可以查看 Elastic App Search 社区讨论论坛。
我们欢迎对该项目的贡献者。在开始之前,请注意以下几点:
Apache 2.0 © 弹性
感谢所有贡献者!