반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

[es] Nested Query vs Object Query 본문

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()
반응형

'ElasticStack > Elasticsearch' 카테고리의 다른 글

[es] scripted similarity  (0) 2022.06.24
[es] Similarity module  (0) 2022.06.24
[es] nested query test  (0) 2022.06.21
[es] 스코어 계산 확인 - explain  (0) 2022.06.20
[es] analyzer test  (0) 2022.06.19
Comments