- 更新
2013-09-14
- バージョン
0.0.3
- 作者
@voluntas
- URL
検索というのがよくわからないのでどんなことが出来るのか調べてみることにしました。 Solr より elasticsearch がシンプルで良いというのがどこかに書いてあったので、elasticsearch を試してみた。
なにやらバージョンが二つあったので、両方とも試してみましたが特に問題なく動きました。 ダウンロードから動かすまでとても簡単でした。あとはこれをどう組み込むかが難しいんだろうなと。
0.90.3:
$ curl -O https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.3.tar.gz
解凍して ./bin/elasticsearch を起動すれば終わりです
Java のバージョン:
$ java -version
java version "1.6.0_51"
Java(TM) SE Runtime Environment (build 1.6.0_51-b11-457-11M4509)
Java HotSpot(TM) 64-Bit Server VM (build 20.51-b01-457, mixed mode)
0.90.3:
$ tar xfz elasticsearch-0.90.3.tar.gz
$ cd elasticsearch-0.90.3/
$ ./bin/elasticsearch -f
[2013-09-14 10:49:01,012][INFO ][node ] [Mentor] version[0.90.3], pid[71021], build[5c38d60/2013-08-06T13:18:31Z]
[2013-09-14 10:49:01,012][INFO ][node ] [Mentor] initializing ...
[2013-09-14 10:49:01,020][INFO ][plugins ] [Mentor] loaded [], sites []
[2013-09-14 10:49:02,846][INFO ][node ] [Mentor] initialized
[2013-09-14 10:49:02,846][INFO ][node ] [Mentor] starting ...
[2013-09-14 10:49:02,942][INFO ][transport ] [Mentor] bound_address {inet[/0:0:0:0:0:0:0:0%0:9300]}, publish_address {inet[/192.168.20.27:9300]}
[2013-09-14 10:49:05,998][INFO ][cluster.service ] [Mentor] new_master [Mentor][agDm1QDdQ0KsjPq0fi568g][inet[/192.168.20.27:9300]], reason: zen-disco-join (elected_as_master)
[2013-09-14 10:49:06,029][INFO ][discovery ] [Mentor] elasticsearch/agDm1QDdQ0KsjPq0fi568g
[2013-09-14 10:49:06,040][INFO ][http ] [Mentor] bound_address {inet[/0:0:0:0:0:0:0:0%0:9200]}, publish_address {inet[/192.168.20.27:9200]}
[2013-09-14 10:49:06,041][INFO ][node ] [Mentor] started
[2013-09-14 10:49:06,053][INFO ][gateway ] [Mentor] recovered [0] indices into cluster_state
Python の elasticsearch クライアント
https://github.com/rhec/pyelasticsearch
http://pyelasticsearch.readthedocs.org/en/latest/
インストール:
$ pip install pyelasticsearch
とりあえずドキュメントデータを突っ込んでみる:
$ python
Python 2.7.5 (default, Aug 1 2013, 01:01:17)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
>>> from pyelasticsearch import ElasticSearch
>>> es = ElasticSearch('http://localhost:9200/')
>>> es.index("contacts", "person", {"name":"Joe Tester", "age": 25, "title": "QA Master"}, id=1)
{u'_type': u'person', u'_id': u'1', u'ok': True, u'_version': 2, u'_index': u'contacts'}
>>> es.index("contacts", "person", {"name":"Jessica Coder", "age": 32, "title": "Programmer"}, id=2)
{u'_type': u'person', u'_id': u'2', u'ok': True, u'_version': 1, u'_index': u'contacts'}
>>> es.index("contacts", "person", {"name":"Freddy Tester", "age": 29, "title": "Office Assistant"}, id=3)
{u'_type': u'person', u'_id': u'3', u'ok': True, u'_version': 1, u'_index': u'contacts'}
>>> es.refresh('contacts')
{u'ok': True, u'_shards': {u'successful': 5, u'failed': 0, u'total': 10}}
>>> es.get('contacts', 'person', 2)
{u'_type': u'person', u'exists': True, u'_source': {u'age': 32, u'name': u'Jessica Coder', u'title': u'Programmer'}, u'_index': u'contacts', u'_version': 1, u'_id': u'2'}
>>> es.search('name:joe OR name:freddy', index='contacts')
{u'hits': {u'hits': [{u'_score': 0.028130025, u'_type': u'person', u'_id': u'3', u'_source': {u'age': 29, u'name': u'Freddy Tester', u'title': u'Office Assistant'}, u'_index': u'contacts'}, {u'_score': 0.028130025, u'_type': u'person', u'_id': u'1', u'_source': {u'age': 25, u'name': u'Joe Tester', u'title': u'QA Master'}, u'_index': u'contacts'}], u'total': 2, u'max_score': 0.028130025}, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}, u'took': 50, u'timed_out': False}
>>> query = {'query': {
... 'filtered': {
... 'query': {
... 'query_string': {'query': 'name:tester'}
... },
... 'filter': {
... 'range': {
... 'age': {
... 'from': 27,
... 'to': 37,
... },
... },
... },
... },
... },
... }
>>>
>>> es.search(query, index='contacts')
{u'hits': {u'hits': [{u'_score': 0.19178301, u'_type': u'person', u'_id': u'3', u'_source': {u'age': 29, u'name': u'Freddy Tester', u'title': u'Office Assistant'}, u'_index': u'contacts'}], u'total': 1, u'max_score': 0.19178301}, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}, u'took': 13, u'timed_out': False}
>>> es.delete_index('contacts')
{u'acknowledged': True, u'ok': True}
日付を範囲指定して検索できるか確認します。
>>> from pyelasticsearch import ElasticSearch
>>> es = ElasticSearch('http://localhost:9200/')
>>> es.index("contacts", "person", {"name":"Joe Tester", "age": 25, "title": "QA Master", "created_at": "2000-04-16T02:09:37.000032"}, id=1)
{u'_type': u'person', u'_id': u'1', u'ok': True, u'_version': 1, u'_index': u'contacts'}
>>> es.index("contacts", "person", {"name":"Jessica Coder", "age": 32, "title": "Programmer", "created_at": "2013-04-12T01:09:37.100000"}, id=2)
{u'_type': u'person', u'_id': u'2', u'ok': True, u'_version': 1, u'_index': u'contacts'}
>>> es.index("contacts", "person", {"name":"Freddy Tester", "age": 29, "title": "Office Assistant", "created_at": "2013-04-22T02:09:37.999999"}, id=3)
{u'_type': u'person', u'_id': u'3', u'ok': True, u'_version': 1, u'_index': u'contacts'}
# created_at が date 型になっているかどうかを確認します
>>> es.get_mapping("contacts")
{u'contacts': {u'person': {u'properties': {u'age': {u'type': u'long'}, u'title': {u'type': u'string'}, u'created_at': {u'type': u'date', u'format': u'dateOptionalTime'}, u'name': {u'type': u'string'}}}}}
>>> query = {'query': {
... 'range': {
... 'created_at': {
... 'gte': '2013-04-01',
... 'lte': '2013-04-30',
... },
... },
... },
... }
# 2013-04-01 から 2013-04-30 の間に追加されたデータのみを引っ張ってきています
>>> es.search(query, index='contacts')
{u'hits': {u'hits': [{u'_score': 1.0, u'_type': u'person', u'_id': u'2', u'_source': {u'created_at': u'2013-04-12T01:09:37.100000', u'title': u'Programmer', u'age': 32, u'name': u'Jessica Coder'}, u'_index': u'contacts'}, {u'_score': 1.0, u'_type': u'person', u'_id': u'3', u'_source': {u'created_at': u'2013-04-22T02:09:37.999999', u'title': u'Office Assistant', u'age': 29, u'name': u'Freddy Tester'}, u'_index': u'contacts'}], u'total': 2, u'max_score': 1.0}, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}, u'took': 4, u'timed_out': False}
Erlang の elasticsearch クライアント
https://github.com/tsloughter/erlastic_search
Django で Elasticsearch を使う場合は haystack が高機能そう
- url
- doc
- github
インストール:
$ pip install django-haystack
settings.py:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'haystack',
},
}
INSTALLED_APPS = [
# 追加
'haystack',
]
# TODO(nakai): 後で調べる
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
- Django + haystack + elasticsearch simple example project - See more at:
http://nanvel.name/weblog/django-haystack-elasticsearch-example-project/#sthash.CDIf8sDq.dpuf