说明
我发现写的文章和代码太多了,有时候搜索起来很不方便。所以我打算给自己上一个全文搜素。初步搞的化功能尽量简单,时间尽量短。
以下部分2小时内就可以完成。
内容
1 安装
使用docker安装,很快。参考,里面有安装还有查询的常见格式。
# 安装
docker pull elasticsearch
# 运行
docker run -d --name ES01 \
-e ES_JAVA_OPTS="-Xms256m -Xmx256m"\
-p 9200:9200\
-p 9300:9300\
elasticsearch
使用浏览器输入对应的地址http://YOURIP:9200
{
"name" : "j_s_TwY",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "UDTgWZu4QVqolgsY2csYWQ",
"version" : {
"number" : "5.6.12",
"build_hash" : "cfe3d9f",
"build_date" : "2018-09-10T20:12:43.732Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}
2 连接
对我来说肯定是使用python进行操作,这部分参考这篇文章
!pip install elasticsearch -i https://mirrors.aliyun.com/pypi/simple/
无密码状态适合用于内网或者测试;有密码的可以用在外网。
3 写入
这部分参考这篇文章。
下面那个index函数好像就写入了。
# 基于上面的连接 es
body1={
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
#余下代码为写入三段数据
body2={
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
body3={
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
# 好像这里已经写入了
res1 = es.index(index="megacorp", doc_type='employee', id=1,body=body1)
res2 = es.index(index="megacorp", doc_type='employee', id=2,body=body2)
res3 = es.index(index="megacorp", doc_type='employee', id=3,body=body3)
res1
{'_index': 'megacorp',
'_type': 'employee',
'_id': '1',
'_version': 1,
'result': 'created',
'_shards': {'total': 2, 'successful': 1, 'failed': 0},
'created': True}
4 查询
4.1 精确匹配
# 查询
bb1={
"query" : {
"match" : {"last_name" : "Smith" }
}
}
rt1= es.search(index="megacorp", body=bb1)
{'took': 1,
'timed_out': False,
'_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0},
'hits': {'total': 2,
'max_score': 0.2876821,
'hits': [{'_index': 'megacorp',
'_type': 'employee',
'_id': '2',
'_score': 0.2876821,
'_source': {'first_name': 'Jane',
'last_name': 'Smith',
'age': 32,
'about': 'I like to collect rock albums',
'interests': ['music']}},
{'_index': 'megacorp',
'_type': 'employee',
'_id': '1',
'_score': 0.2876821,
'_source': {'first_name': 'John',
'last_name': 'Smith',
'age': 25,
'about': 'I love to go rock climbing',
'interests': ['sports', 'music']}}]}}
4.2 过滤查找
bb2={
"query" : {
"bool": {
"must": { "match" : { "last_name" : "smith" }
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
rt2= es.search(index="megacorp", body=bb2)
{'took': 19,
'timed_out': False,
'_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0},
'hits': {'total': 1,
'max_score': 0.2876821,
'hits': [{'_index': 'megacorp',
'_type': 'employee',
'_id': '2',
'_score': 0.2876821,
'_source': {'first_name': 'Jane',
'last_name': 'Smith',
'age': 32,
'about': 'I like to collect rock albums',
'interests': ['music']}}]}}
4.3 全文匹配
all_search={
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
rt3= es.search(index="megacorp", body=all_search)
rt3
{'took': 4,
'timed_out': False,
'_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0},
'hits': {'total': 2,
'max_score': 0.53484553,
'hits': [{'_index': 'megacorp',
'_type': 'employee',
'_id': '1',
'_score': 0.53484553,
'_source': {'first_name': 'John',
'last_name': 'Smith',
'age': 25,
'about': 'I love to go rock climbing',
'interests': ['sports', 'music']}},
{'_index': 'megacorp',
'_type': 'employee',
'_id': '2',
'_score': 0.26742277,
'_source': {'first_name': 'Jane',
'last_name': 'Smith',
'age': 32,
'about': 'I like to collect rock albums',
'interests': ['music']}}]}}
4.4 短语匹配
phrase={
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
rt4= es.search(index="megacorp", body=phrase)
rt4
{'took': 1,
'timed_out': False,
'_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0},
'hits': {'total': 1,
'max_score': 0.53484553,
'hits': [{'_index': 'megacorp',
'_type': 'employee',
'_id': '1',
'_score': 0.53484553,
'_source': {'first_name': 'John',
'last_name': 'Smith',
'age': 25,
'about': 'I love to go rock climbing',
'interests': ['sports', 'music']}}]}}
5 Next
- 1 构造实用的文章存储和检索
- 2 了解es的原理