跳转到主要内容

Elastic Site Search API Python客户端

项目描述

Elastic Site Search Logo

CircleCI build GitHub release

是Elastic Site Search API的Elastic Site Search API的第一个Python客户端。

内容


入门 🐣

您可以使用pip安装Elastic Site Search客户端的最新版本

pip install elastic-site-search

要本地安装,克隆此存储库,然后cd进入目录并运行

python setup.py install

注意:此客户端仅针对Elastic Site Search API端点开发。您可以参考Elastic Site Search API文档以获取更多背景信息。

用法

  1. 创建Elastic Site Search账户并从您的账户设置获取API密钥。

  2. 配置您的客户端

from elastic_site_search import Client
client = Client(api_key='YOUR_API_KEY')
  1. 创建一个名为例如youtubeEngine
engine = client.create_engine('youtube')
  1. 创建您的DocumentType
client.create_document_type('youtube', 'videos');
client.create_document_type('youtube', 'channels');

索引

现在您需要创建您的 文档。考虑您创建的每个 文档 字段的类型非常重要。所属的 DocumentType 会记住每个字段的类型,且无法更改。类型指定了字段的特性,您应明智地选择。有关详细信息,请参阅我们的 字段类型文档

videos DocumentType 添加一个 文档

client.create_document('youtube', 'videos', {
  'external_id':  'external_id1',
  'fields': [
      {'name': 'title', 'value': 'Site Search Demo', 'type': 'string'},
      {'name': 'tags', 'value': ['Site Search', 'Search', 'Full text search'], 'type': 'string'},
      {'name': 'url', 'value': 'http://www.youtube.com/watch?v=pITuOcGgpBs', 'type': 'enum'},
      {'name': 'category', 'value': ['Tutorial', 'Product'], 'type': 'enum'},
      {'name': 'publication_date', 'value': '2012-05-08T12:07Z', 'type': 'date'},
      {'name': 'likes', 'value': 31, 'type': 'integer'},
      {'name': 'length', 'value': 1.50, 'type': 'float'}
  ]
})

channels DocumentType 添加一个 文档

client.create_document('youtube', 'channels', {
  'external_id': 'external_id1',
  'fields': [
    {'name': 'title', 'value': 'Elastic', 'type': 'string'},
    {'name': 'url', 'value': 'http://www.youtube.com/user/elasticsearch', 'type': 'enum'},
    {'name': 'video_views', 'value': 15678, 'type': 'integer'},
    {'name': 'video_counts', 'value': 6, 'type': 'integer'}
  ]
})

搜索

现在您的 引擎 准备接收查询。默认情况下,搜索查询将匹配类型为 stringtext 的任何字段。您可以单独搜索每个 DocumentType

video_results = client.search_document_type('youtube', 'videos', 'site search')
channel_results = client.search_document_type('youtube', 'channels', 'site search')

或一次性在您的 引擎 上搜索所有 DocumentType

results = client.search('youtube', 'site search')

自动完成

最后,与全文搜索一样,您还可以执行自动完成(前缀匹配)搜索

results = client.suggest('youtube', 'sit')

results = client.suggest_document_type('youtube', 'videos', 'sit')

API 文档

配置

在向 API 发送命令之前,请使用您的 API 密钥配置客户端

from elastic_site_search import Client
client = Client(api_key='YOUR_API_KEY')

您可以在您的 账户设置 中找到您的 API 密钥。

搜索

如果您想在您的 引擎 上搜索例如 站点搜索,您可以使用

results = client.search('youtube', 'site search')

将搜索限制为仅 videos DocumentType

results = client.search_document_type('youtube', 'videos', 'site search')

这两种搜索方法都允许您通过额外的参数指定选项,例如过滤或根据字段排序。有关这些选项的更多详细信息,请参阅 搜索选项。以下是一个仅显示属于 category Tutorialvideos 的示例

results = client.search_document_type('youtube', 'videos', 'site search', {'filters': {'videos': {'category': 'Tutorial'}}})

自动完成

自动完成具有与搜索相同的功能。您可以使用所有文档进行自动完成

results = client.suggest('youtube', 'sit')

或仅针对一个 DocumentType

results = client.suggest_document_type('youtube', 'videos', 'sit')

或添加选项以对结果有更多控制

results = client.suggest('youtube', 'sit', {'sort_field': {'videos': 'likes'}})

引擎

检索每个 引擎

engines = client.engines

创建一个新的名为 youtube引擎

engine = client.create_engine('youtube')

通过 slugid 检索一个 引擎

engine = client.engine('youtube')

要删除一个 引擎,您需要该 引擎slugid 字段

client.destroy_engine('youtube')

文档类型

检索具有 slug 字段 youtubeEngineDocumentTypes

document_types = client.document_types('youtube')

显示第二批文档

document_types = client.document_types('youtube', 2)

为具有 videos 名称的 Engine 创建一个新的 DocumentType

document_type = client.create_document_type('youtube', 'videos')

通过 slugid 检索一个 DocumentType

document_type = client.document_type('youtube', 'videos')

使用其 slugid 删除一个 DocumentType

client.destroy_document_type('youtube', 'videos')

文档

检索 youtube Enginevideos DocumentType 的所有 Document

documents = client.documents('youtube', 'videos')

使用其 idexternal_id 检索一个特定的 Document

document = client.document('youtube', 'videos', 'external_id1')

创建一个新的 Document,其中包含必填的 external_id 和用户定义的字段

document = client.create_document('youtube', 'videos', {
  'external_id': 'external_id1',
  'fields': [
    {'name': 'title', 'value': 'Site Search Demo', 'type': 'string'},
    {'name': 'tags', 'value': ['Site Search', 'Search', 'Full text search'], 'type': 'string'},
    {'name': 'url', 'value': 'http://www.youtube.com/watch?v=pITuOcGgpBs', 'type': 'enum'},
    {'name': 'category', 'value': ['Tutorial', 'Product'], 'type': 'enum'},
    {'name': 'publication_date', 'value': '2012-05-08T12:07Z', 'type': 'date'},
    {'name': 'likes', 'value': 31, 'type': 'integer'},
    {'name': 'length', 'value': 1.50, 'type': 'float'}
  ]
})

一次性创建多个 Document 并返回每个 Document 创建的状态

stati = client.create_documents('youtube', 'videos',
  {
    'external_id': 'external_id1',
    'fields': [
      {'name': 'title', 'value': 'Site Search Demo', 'type': 'string'},
      {'name': 'tags', 'value': ['Site Search', 'Search', 'Full text search'], 'type': 'string'},
      {'name': 'url', 'value': 'http://www.youtube.com/watch?v=pITuOcGgpBs', 'type': 'enum'},
      {'name': 'category', 'value': ['Tutorial', 'Product'], 'type': 'enum'},
      {'name': 'publication_date', 'value': '2012-05-08T12:07Z', 'type': 'date'},
      {'name': 'likes', 'value': 27, 'type': 'integer'},
      {'name': 'length', 'value': 1.50, 'type': 'float'}
    ]
  },  
  {
    'external_id': 'external_id2',
    'fields': [
      {'name': 'title', 'value': 'Site Search Search Wordpress Plugin Demo', 'type': 'string'},
      {'name': 'tags', 'value': ['Site Search', 'Search', 'Full text search', 'WordPress'], 'type': 'string'},
      {'name': 'url', 'value': 'http://www.youtube.com/watch?v=rukXYKEpvS4', 'type': 'enum'},
      {'name': 'category', 'value': ['Tutorial', 'Wordpress'], 'type': 'enum'},
      {'name': 'publication_date', 'value': '2012-08-15T09:07Z', 'type': 'date'},
      {'name': 'likes', 'value': 2, 'type': 'integer'},
      {'name': 'length', 'value': 2.16, 'type': 'float'}
    ]
  }
)

更新指定 idexternal_id 的现有 Document 的字段

client.update_document('youtube','videos','external_id1', {'likes': 28, 'category': ['Tutorial', 'Search']})

一次性更新多个 Document

stati = client.update_documents('youtube', 'videos', [
  {'external_id': '2', 'fields': {'likes': 29}},
  {'external_id': '3', 'fields': {'likes': 4}}
])

创建或更新一个 Document

document = client.create_or_update_document('youtube', 'videos', {
  'external_id': 'external_id3',
  'fields': [
    {'name': 'title', 'value': 'Site Search Install Type 1: Show results in an overlay', 'type': 'string'},
    {'name': 'tags', 'value': ['Site Search', 'Search', 'Full text search', 'Web'], 'type': 'string'},
    {'name': 'url', 'value': 'http://www.youtube.com/watch?v=mj2ApIx3frs', 'type': 'enum'}
  ]
})

一次性创建或更新多个 Document

stati = client.create_or_update_documents('youtube', 'videos',
  {
    'external_id': 'external_id4',
    'fields': [
      {'name': 'title', 'value': 'Site Search Install Type 2: Show results on the current page', 'type': 'string'},
      {'name': 'tags', 'value': ['Site Search', 'Search', 'Full text search', 'Web'], 'type': 'string'},
      {'name': 'url', 'value': 'http://www.youtube.com/watch?v=6uaZXYK2WOE', 'type': 'enum'}
    ]
  },
  {
    'external_id': 'external_id5',
    'fields': [
      {'name': 'title', 'value': 'Site Search Install Type 3: Show results on a new page', 'type': 'string'},
      {'name': 'tags', 'value': ['Site Search', 'Search', 'Full text search', 'Web'], 'type': 'string'},
      {'name': 'url', 'value': 'http://www.youtube.com/watch?v=ebSWAscBPtc', 'type': 'enum'}
    ]
  }
)

销毁一个 Document

client.destroy_document('youtube','videos','external_id5')

一次性销毁多个 Document

stati = client.destroy_documents('youtube','videos',['external_id2','external_id3','external_id6'])

域名

检索 websites Engine 的所有 Domain

domains = client.domains('websites')

通过 id 检索一个特定的 Domain

domain = client.domain('websites', 'generated_id')

创建一个新的具有 URL https://elastic.ac.cnDomain 并开始抓取

domain = client.create_domain('websites', 'https://elastic.ac.cn')

使用其 id 删除一个 Domain

client.destroy_domain('websites', 'generated_id')

使用其 id 启动特定 Domain 的重新抓取

client.recrawl_domain('websites', 'generated_id')

Domain 添加或更新一个 URL

client.crawl_url('websites', 'generated_id', 'https://elastic.ac.cn/new/path/about.html')

分析

要获取您 Engine 上过去 14 天内的搜索量,请使用

searches = client.analytics_searches('youtube')

您还可以使用特定的开始日期和/或结束日期

searches = client.analytics_searches('youtube', '2013-01-01', '2013-02-01')

要获取自动选择的数量(自动完成结果的点击次数),请使用

autoselects = client.analytics_autoselects('youtube')

与搜索一样,您还可以通过开始日期和/或结束日期进行限制

autoselects = client.analytics_autoselects('youtube', 2, 10)

如果您想了解您引擎的顶级查询,可以使用

top_queries = client.analytics_top_queries('youtube')

要查看更多顶级查询,您可以使用分页功能来浏览它们

top_queries = client.analytics_top_queries('youtube', page=2)

或者,您可以获取特定日期范围内的顶级查询

top_queries = client.analytics_top_queries_in_range('youtube', '2013-01-01', '2013-02-01')

如果您想提高搜索结果,您应该始终查看没有返回结果的搜索查询,并可能添加一些与该查询匹配的文档,或者使用我们的标记功能为该查询添加文档

top_no_result_queries = client.analytics_top_no_result_queries('youtube')

您还可以为没有结果查询指定日期范围

top_no_result_queries = client.analytics_top_no_result_queries('youtube', '2013-01-01', '2013-02-01')

运行测试

pip install -r test_requirements.txt
python tests/test_client.py

常见问题解答 🔮

在哪里报告客户端的问题?

如果某个功能不符合预期,请打开一个问题

我在哪里可以了解更多关于 Site Search 的信息?

您最好的选择是阅读文档

我还可以去哪里寻求帮助?

您可以查看Elastic Site Search 社区讨论论坛

贡献 🚀

我们欢迎对项目做出贡献。在您开始之前,请注意以下几点...

  • 在提交拉取请求之前,请创建一个问题讨论您提案的范围
  • 请适当编写简单代码和简洁的文档。

许可 📗

Apache 2.0 © Elastic

感谢所有贡献者

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

elastic-site-search-2.1.1.tar.gz (12.1 kB 查看散列)

上传时间

构建分布

elastic_site_search-2.1.1-py2.py3-none-any.whl (12.5 kB 查看散列)

上传时间 Python 2 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面