ElasticSearch 기본 조회 1 (영어 분사)

12764 단어 ElasticSearch
쓸데없는 말은 하지 말고 우선 데이터를 준비하고, 우리는 먼저 몇 개의 문서를 추가합시다
PUT /lib3/user/1
{ 
  "name" : "zhaoliu",
  "address" :"hei long jiang sheng tie ling shi",
  "age" : 50,
  "birthday" : "1970-12-12",
  "interests": "xi buan hejiu, duanlian, lvyou"
}

PUT /lib3/user/2
{
  "name" :"zhaoming" ,
  "address" : "bei jing hai dian qu qing he zhen",
  "age" : 20,
  "birthday" : "1998-10-12" ,
  "interests": "xi huan hejiu, duanlian, changge"
}


PUT /lib3/user/3
{
  "name" : "lisi",
  "address" : "bei jing hai dian qu qing he zhen",
  "age" : 23,
  "birthday" : "1998-10-12" ,
  "interests": "xi huan hejiu,duanlian, changge"
}

PUT /lib3/user/4
{
  "name": "wangwu",
  "address" : "bei jing hai dian qu qing he zhen",
  "age": 26,
  "birthday" : "1995-10-12" ,
  "interests": "xi huan biancheng, tingyinyue, lvyou"
}

PUT /lib3/user/5
{
  "name" :"zhangsan",
  "address" : "bei jing chao yang qu",
  "age" : 29,
  "birthday" : "1988-10-12",
  "interests": "xi huan tingyinyue , changge , tiaowu"
}


간단히 name를 통해 lisi라는 사람의 기본 정보를 찾습니다 GET/lib3/user/_search?q=name:lisi
{
  "took" : 43,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "lisi",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 23,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu,duanlian, changge"
        }
      }
    ]
  }
}

이max_score: 관련 일치도 점수(ElasticSearch의 알고리즘에 따라 계산됨)
 
취미 조회 취미 interests changge(노래)를 좋아하는 사람, 그리고 나이 서열
GET /lib3/user/_search?q=interests:changge&sort=age:desc
{
  "took" : 124,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "name" : "zhangsan",
          "address" : "bei jing chao yang qu",
          "age" : 29,
          "birthday" : "1988-10-12",
          "interests" : "xi huan tingyinyue , changge , tiaowu"
        },
        "sort" : [
          29
        ]
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "lisi",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 23,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu,duanlian, changge"
        },
        "sort" : [
          23
        ]
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "zhaoming",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 20,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu, duanlian, changge"
        },
        "sort" : [
          20
        ]
      }
    ]
  }
}

term과terms 조회 (zhaoliu라는 사람의 정보를 찾습니다)


termquery는 역삭궁 | 에서 정확한 term을 찾습니다. 분사기의 존재를 알지 못합니다.이런 검색은 키워드,numeric에 적합하다.date. term: 특정 필드에 키워드가 포함된 문서 검색 GET/lib3/user/_search/{"query": {interests":'changge'}}terms: 필드에 여러 키워드가 포함된 문서 GET/ib3/user/_search {"query": {"terms": {"interests": ["hejiu","changge"]}}}
GET /lib3/user/_search
{
  "query": {
    "term": {
      "name": {
        "value": "zhaoliu"
      }
    }
  }
}


GET /lib3/user/_search
{
  "query": {
    "term": {
      "name": "zhaoliu"
    }
  }
}
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "zhaoliu",
          "address" : "hei long jiang sheng tie ling shi",
          "age" : 50,
          "birthday" : "1970-12-12",
          "interests" : "xi buan hejiu, duanlian, lvyou"
        }
      }
    ]
  }
}

 
취미인interests가hejiuchangge인 사람 정보 찾기
GET /lib3/user/_search
{
  "query": {
    "terms": {
      "interests": ["hejiu","changge"]
    }
  }
}
{
  "took" : 56,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "address" : "bei jing chao yang qu",
          "age" : 29,
          "birthday" : "1988-10-12",
          "interests" : "xi huan tingyinyue , changge , tiaowu"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhaoming",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 20,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu, duanlian, changge"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhaoliu",
          "address" : "hei long jiang sheng tie ling shi",
          "age" : 50,
          "birthday" : "1970-12-12",
          "interests" : "xi buan hejiu, duanlian, lvyou"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "lisi",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 23,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu,duanlian, changge"
        }
      }
    ]
  }
}

키워드 하나만 있으면 다 조회가 돼요. 총 4명이서 Hejiu changge, changge, Hejiu, 둘 다 있어요.
from:0 (첫 번째 문서부터) size:2 (두 개의 문서를 가져오는 것)
GET /lib3/user/_search
{
  "from":0,
  "size":2,
  "query": {
    "terms": {
      "interests": ["hejiu","changge"]
    }
  }
}
{
  "took" : 81,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "address" : "bei jing chao yang qu",
          "age" : 29,
          "birthday" : "1988-10-12",
          "interests" : "xi huan tingyinyue , changge , tiaowu"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhaoming",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 20,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu, duanlian, changge"
        }
      }
    ]
  }
}

이상의 조회는 버전 번호가 없습니다. 버전 번호를 얻으려면version:true 하나만 추가하십시오
GET /lib3/user/_search
{
  "version": true, 
  "query": {
    "terms": {
      "interests": ["hejiu","changge"]
    }
  }
}
{
  "took" : 33,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "5",
        "_version" : 1,
        "_score" : 1.0,
        "_source" : {
          "name" : "zhangsan",
          "address" : "bei jing chao yang qu",
          "age" : 29,
          "birthday" : "1988-10-12",
          "interests" : "xi huan tingyinyue , changge , tiaowu"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "2",
        "_version" : 1,
        "_score" : 1.0,
        "_source" : {
          "name" : "zhaoming",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 20,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu, duanlian, changge"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "1",
        "_version" : 1,
        "_score" : 1.0,
        "_source" : {
          "name" : "zhaoliu",
          "address" : "hei long jiang sheng tie ling shi",
          "age" : 50,
          "birthday" : "1970-12-12",
          "interests" : "xi buan hejiu, duanlian, lvyou"
        }
      },
      {
        "_index" : "lib3",
        "_type" : "user",
        "_id" : "3",
        "_version" : 1,
        "_score" : 1.0,
        "_source" : {
          "name" : "lisi",
          "address" : "bei jing hai dian qu qing he zhen",
          "age" : 23,
          "birthday" : "1998-10-12",
          "interests" : "xi huan hejiu,duanlian, changge"
        }
      }
    ]
  }
}

 
 
 
 

좋은 웹페이지 즐겨찾기