도서 요약 / / 2022. 12. 22. 20:24

10. Controlling Query Results

complete guide to elasticsearch를 정리한 자료입니다.
https://www.udemy.com/course/elasticsearch-complete-guide/

111. Result 포맷 형식

GET recipe/_search?format=yaml
{
  "query": {
    "match": {
      "title": "pasta"
    }
  }
}

112. Source 필터링

_source필드를 표시하지 않으려면

GET recipe/_search
{
  "_source": false, 
  "query": {
    "match": {
      "title": "pasta"
    }
  }
}

특정 필드만 표시하려면

GET recipe/_search
{
  "_source": "ingredients.name", 
  "query": {
    "match": {
      "title": "pasta"
    }
  }
}

GET recipe/_search
{
  "_source": "ingredients.*", 
  "query": {
    "match": {
      "title": "pasta"
    }
  }
}

GET recipe/_search
{
  "_source": ["ingredients.*", "servings"], 
  "query": {
    "match": {
      "title": "pasta"
    }
  }
}

GET recipe/_search
{
  "_source": {
      "includes": "ingredients.*",
      "excludes": "ingredients.name"
  } 
  "query": {
    "match": {
      "title": "pasta"
    }
  }
}

113. result size

GET recipe/_search?size=2
{
  "_source": false, 
  "query": {
    "match": {
      "title": "pasta"
    }
  }
}

GET recipe/_search
{
  "_source": false, 
  "query": {
    "match": {
      "title": "pasta"
    }
  },
  "size": 2
}

114. offset

GET recipe/_search?size=2
{
  "_source": false, 
  "from": 0,
  "size": 2,
  "query": {
    "match": {
      "title": "pasta"
    }
  }
}

115. pagination

total_pages = ceil(total_hits / page_size)

14 = ceil(137 /10)

from = (page_size * (page_number - 1)

50 = (10 * (6 - 1))

116. result 정렬

GET recipe/_search
{
  "_source": "created", 
  "query": {
    "match": {
      "title": "pasta"
    }
  },
  "sort": [
    { "created": "desc" }
  ]
}
GET recipe/_search
{
  "_source": ["preparation_time_minutes", "created"], 
  "query": {
    "match": {
      "title": "pasta"
    }
  },
  "sort": [
    { "preparation_time_minutes": "asc" },
    { "created": "desc" }
  ]
}

117. Multi-value 필드 정렬

GET recipe/_search
{
  "_source": "ratings", 
  "query": {
    "match_all": {}
  },
  "sort": [
    { 
      "rating":  {
        "order": "desc",
        "mode": "avg"
      }
    }
  ]
}

118. filters

GET recipe/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "pasta"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "preparation_time_minutes": {
              "lte": 15
            }
          }
        }
      ]
    }
  }
}
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유