naming-Tutorials & Cookbooks
FROM : https://docs.blockstack.org/core/naming/search.html
Blockstack Core的搜索子系统为与名称空间中的注册名称关联的数据创建索引,并使该数据可搜索。
搜索子系统目前用于索引.id名称空间,但是可以很容易地扩展到包含其他名称空间。
目前有两种索引类型来处理搜索查询:
- 用户名、全名、twitter_handle子字符串搜索(由MongoDB提供支持)
- 处理搜索扩展数据的原始Lucene索引,如bio。
搜索目前最多返回20个结果(根据查询的不同可以少一些),数据遵循blockstack id结构:
2017年初,搜索子系统被移植到新的API系统,端点提供了对搜索的支持:
http://localhost:5000/search?query=<SEARCH_PATTERN>
本文档描述如何设置搜索子系统以在该端点响应。
安装
Step 1: 首先,确保安装了virtualenv。然后,设置API和搜索子系统:
sudo apt-get install -y mongodb memcached python-dev libmemcached-dev zlib1g-dev nginx
sudo pip install uwsgi
git clone https://github.com/blockstack/blockstack-core.git --branch api
cd blockstack-core/
sudo pip install .
sudo pip install -r api/requirements.txt
sudo mkdir /var/blockstack-search && sudo chownUSER /var/blockstack-search
Step 2:确保Blockstack核心在本地运行(参见说明)。我们强烈建议使用本地节点,因为搜索子系统会向blockstack核心发出数千次调用,以便重新索引,而远程节点会降低性能。
**Step 3: **获取.id名称空间和各自概要文件的数据。注意,您可能希望将stderr重定向到一个文件,因为有很多调试输出。
cd api/
python -m search.fetch_data --fetch_namespace
python -m search.fetch_data --fetch_profiles
**Step 4: **创建搜索索引:
python -m search.basic_index --refresh
Step 5:启用搜索API端点:
sed -i 's/SEARCH_API_ENDPOINT_ENABLED = False/SEARCH_API_ENDPOINT_ENABLED = True/' config.py
使用
您可以从命令行快速测试搜索索引:
python -m search.substring_search --search_name "Fred Wil"
python -m search.substring_search --search_twitter fredwil
你也可以使用搜索API的端点:
curl -G {machine_ip}:port/search/name -d “query=muneeb”
例子返回响应:
{
"people": [
{
"profile": {
"website": [
{
"url": "http://muneebali.com",
"@type": "WebSite"
}
],
"name": "Muneeb Ali",
"address": {
"addressLocality": "New York, NY",
"@type": "PostalAddress"
},
"image": [
{
"contentUrl": "https://s3.amazonaws.com/dx3/muneeb",
"@type": "ImageObject",
"name": "cover"
},
{
"contentUrl": "https://s3.amazonaws.com/kd4/muneeb",
"@type": "ImageObject",
"name": "avatar"
}
],
"@type": "Person",
"description": "Co-founder of Blockstack. Interested in distributed systems and blockchains. Previously, PhD at Princeton."
},
"username": "muneeb"
},
{
"profile": {
"message": "This blockchain ID is reserved for Muneeb Ali. If this is you, please email support@onename.com to claim it for free.",
"status": "reserved"
},
"username": "muneebali"
},
{
"profile": {
"cover": {
"url": "https://s3.amazonaws.com/97p/HHE.jpg"
},
"v": "0.2"
},
"username": "muneebali1"
}
]
}
使用Elastic搜索
要求:
sudo apt-get install mongodb
sudo apt-get install memcached libmemcached-dev
sudo apt-get install python2.7-dev
pip install -r requirements.txt
Elastic搜索
Elastic搜索库不在github中,而是驻留在unix/lib/ Elastic中
我们使用的当前版本是0.90.2。下载:
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.2.zip
在安装pylimbmc之前,请确保安装了memcached。
确保mongodb和elastic search正在运行,并启动elastic search:
$elasticsearch (on mac)
bin/elasticsearch -d (on linux)
测试弹性搜索是否运行:
curl -X GET http://localhost:9200/
返回值
{
"ok" : true,
"status" : 200,
"name" : "Angler",
"version" : {
"number" : "0.90.2",
"snapshot_build" : false,
"lucene_version" : "4.3.1"
},
创建Index:
python create_search_index.py –create_index