key | value |
---|---|
1 | 1번값 |
2 | 2번값 |
3 | 3번값 |
Document | Words |
---|---|
doc1 | apple, bottle, toy |
doc2 | grape, box |
doc3 | snack, toy, box |
Word | Documents |
---|---|
apple | doc1 |
box | doc2, doc3 |
toy | doc1, doc3 |
text
인 경우text = 'The Fast white dog jumped over the lazy cat!'
tokenized_text = tokenizer(text)
[fast, white, cat, jump, over, lazi, dog ]
keyword
or number
or date
인 경우Open Source Project인 Apache Lucene을 기반으로 만들어진 빅데이터를 거의 실시간(NRT, Near Real Time)으로 저장, 검색 및 분석할 수 있는 오픈소스 검색 엔진
import elasticsearch
from elasticsearch_dsl import Search, Q, Index
es = elasticsearch.Elasticsearch('localhost:9200')
s = Search(index='index_name', using=es)
s = s.query('match_all')
search_result = []
for hit in s.scan():
search_result.append(hit)
s = Search(index='index_name', using=es)
s = s.query('match', pk=1)
res = s.execute()
res.to_dict()
query_text = '브랜드명 상품명'
s = Search(index='item_storage', using=es)
s = s.query(
'multi_match',
query=query_text,
fuzziness='auto',
fields=['item_name', 'brand_name']
)
condition = {
'gte': 10,
'lt': 20
}
s = Search(index='index_name', using=es)
s = s.filter('range', pk=condition)
res = s.execute()
res.to_dict()
s = Search(index='users').using(es)
must = [
{'match': {'firstname.keyword': {'query': 'John'}}}
]
should = [
{'match': {'lastname.keyword': 'Lennon'}},
{'match': {'country': 'Korea'}}
]
s = s.query(Q('bool', must=must, should=should, minimum_should_match=1))
res = s.execute()
users index
에서firstname이 John이면서 lastename이 Lennon이거나 country가 Korea인 유저를 찾아온다.
field data type
name | description |
---|---|
text | full text를 의미. analyzer를 사용해서 inverted indexing(역색인) |
keyword | 정확한 값들을 의미. 들어온 값 그대로 inverted indexing(역색인) |
inverted Index type
| Field Name | Inverted Index |
|:----------|:----------:|
| full_text |[quick, foxes]|
| exact_value | [Quick Foxes!] |
s = Search(index='index_name', using=es)
s = s.query('terms', pks=[0, 3, 5, 9]).sort('pk')
오름차순 pk, 내림차순 -pk
s = Search(index='users', using=es)
s = s.query('terms', pks=[0, 3, 5, 9]).sort('pk').source([
'email',
'profile_image'
])