Boolean Queries (AND, OR, NOT)

donghwikim00·2025년 1월 24일

19_elastic_search

목록 보기
10/11
  • 여러 조건을 결합해 복잡한 검색을 수행

기본 사용법

must

  • AND 조건(필수 조건)

should

  • OR 조건(하나 이상 일치)

must_not

  • NOT 조건(제외 조건)

예제 코드

const axios = require("axios");

const url = "http://localhost:9200/test_index/_search";

const query = {
  query: {
    bool: {
      //   must: [
      //     // AND 조건
      //     {
      //       match: {
      //         name: "D",
      //       },
      //     },
      //   ],
      //   must_not: [
      //     // NOT 조건
      //     {
      //       match: {
      //         name: "A",
      //       },
      //     },
      //   ],
      should: [
        // OR 조건
        {
          //   match: {
          //     name: "B",
          //   },

          //   match: {
          //     price: 300,
          //   },
          range: { price: { gte: 200, lte: 400 } },
        },
      ],
    },
  },
};

axios
  .post(url, query)
  .then((res) => {
    // console.log(res.data);
    const jsonRes = JSON.stringify(res.data, null, 2);
    console.log(jsonRes);
  })
  .catch((err) => {
    console.error(err);
  });
  • 각각 실행해보고 싶으면 주석 해제해서 사용
  • must: nameD가 들어간 모든 걸 조회

  • should: price200보다 크거나 또는 400보다 작은 데이터
    를 조회

  • must_not: name에 A가 들어가지 않은 모든 데이터를 조회한다.

profile
기술 블로그입니다. 여러 언어에 대한 정리, 프로젝트 설명 등을 기록합니다.

0개의 댓글