ElasticStack/Elasticsearch
[es] Nested Query vs Object Query
닉의네임
2022. 6. 21. 16:17
반응형
nested 와 object 를 비교해보자.
같은 데이터를 색인하였으나
nested 구조 안의 문서를 개별로 인식해서 그런가 docs 의 차이가 .. 근데 또 size 는 작네 거의 두배차이
from elasticsearch import Elasticsearch
import pprint as ppr
import json
nested_index_name ="nested-index"
object_index_name ="object-index"
query = {
"sort": [
"_score",
{
"store.price": {
"mode": "max",
"order": "asc",
"nested": {
"path": "store",
"filter": {
"term": {
"store.code": "0002"
}
}
}
}
}
],
"query": {
"bool": {
"must": [
{
"match": {
"name": {
"query" : "소고기 1등급",
"operator": "and"
}
}
},
{
"nested": {
"path": "store",
"query": {
"bool": {
"must": [
{
"match": {
"store.code": "0002"
}
}
]
}
}
}
}
]
}
},
"aggs": {
"price_aggs_filter": {
"nested": {
"path": "store"
},
"aggs": {
"price_filter": {
"filter": {
"term": {
"store.code": "0002"
}
},
"aggs": {
"price_avg": {
"avg": {
"field": "store.price"
}
}
}
}
}
}
}
}
class EsAPI:
es = Elasticsearch(hosts="localhost", port=9200, http_auth=('elastic', 'elastic1!')) # 객체 생성
@classmethod
def srvHealthCheck(cls):
health = cls.es.cluster.health()
print (health)
@classmethod
def allIndex(cls): # Elasticsearch에 있는 모든 Index 조회
print (cls.es.cat.indices())
@classmethod
def dataInsert(cls):
# ===============
# 데이터 삽입
# ===============
with open("/Users/doo/doo_py/homeplus/data/nested/data.json", "r", encoding="utf-8") as fjson:
data = json.loads(fjson.read())
for n, i in enumerate(data):
doc = {
"name": i['name'],
"store": i["store"]
}
res_n = cls.es.index(index=nested_index_name, doc_type="_doc", id=n + 1, body=doc)
res_o = cls.es.index(index=object_index_name, doc_type="_doc", id=n + 1, body=doc)
print (res_n)
print (res_o)
@classmethod
def searchAll(cls, indx=None): # =============== # 데이터 조회 [전체] # ===============
res_n = cls.es.search(
index=nested_index_name,
doc_type="_doc",
body={"query": {"match_all": {}}})
print (json.dumps(res_n, ensure_ascii=False, indent=4))
res_o = cls.es.search(
index=object_index_name,
doc_type="_doc",
body={"query": {"match_all": {}}})
print (json.dumps(res_o, ensure_ascii=False, indent=4))
@classmethod
def searchFilter(cls):
# =============== # 데이터 조회 [] # ===============
res_n = cls.es.search(
index=nested_index_name,
doc_type="_doc",
body= query
)
ppr.pprint(res_n)
res_o = cls.es.search(
index=object_index_name,
doc_type="_doc",
body= query
)
ppr.pprint(res_o)
@classmethod
def createIndex(cls):
# =============== # 인덱스 생성 # ===============
cls.es.indices.create(
index=nested_index_name,
body={
"settings": {
"index.number_of_shards": 1,
"index.number_of_replicas": 0
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"store": {
"type": "nested"
}
}
}
}
)
cls.es.indices.create(
index=object_index_name,
body={
"settings": {
"index.number_of_shards": 1,
"index.number_of_replicas": 0
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"store": {
"type": "object"
}
}
}
}
)
# EsAPI.allIndex()
# EsAPI.srvHealthCheck()
# EsAPI.createIndex()
# EsAPI.dataInsert()
# EsAPI.searchAll()
EsAPI.searchFilter()
반응형