Elasticsearch: 사용 하기 쉬 운 전문 검색 라 이브 러 리
                                            
 17403 단어  #Elasticsearch
                    
Elasticsearch
클 러 스 터 는 색인, 색인 포함 유형, 형식 저장 문서, 문 서 는 속성 이 있 습 니 다.
능력.
확장 가능, 전문 검색, 분석, 저장
예 를 들다
기본 개념:
데이터 넣 기 및 검색
데 이 터 를 넣다
curl -X PUT "localhost:9200/megacorp/employee/1" -H 'Content-Type: application/json' -d'
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
'  데이터 읽 기
curl -X GET "localhost:9200/megacorp/employee/1"  단순 검색
curl -X GET "localhost:9200/megacorp/employee/_search"  매개 변수 단순 검색
curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith"  검색
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}  필터 검색
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}
'  전체 텍스트 에서 이 기능 을 검색 하면 관련 검색 을 실현 할 수 있 습 니 다.
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}
'  구문 검색 은 정확 한 검색 입 니 다. 이 검색 모드 를 사용 하면 정확 한 결 과 를 되 돌려 줍 니 다.
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}
'  하 이 라이트 검색 하 이 라 이 트 는 검색 결 과 를 표시 합 니 다. 검색 결과 의 일치 부분
 으로 패키지 합 니 다.curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}
'  분석 하 다.
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}
'  분석 과 동시에 검색 하 는 방식 은 match 필드 의 값 과 일치 하 며 분석 합 니 다.
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "last_name": "smith"
    }
  },
  "aggs": {
    "all_interests": {
      "terms": {
        "field": "interests"
      }
    }
  }
}
'  집합 등급 집합 은 정렬 에 따라 등급 별 분석 을 하고 집합 한다.
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
    "aggs" : {
        "all_interests" : {
            "terms" : { "field" : "interests" },
            "aggs" : {
                "avg_age" : {
                    "avg" : { "field" : "age" }
                }
            }
        }
    }
}
'  문서 제어
문서 에 넣 은 웹 사 이 트 는 색인 이 고 블 로 그 는 형식 이 며 123 은 id 입 니 다.이 기능 을 사용 하면 문 서 를 업데이트 할 수 있 습 니 다.
curl -X PUT "localhost:9200/website/blog/123" -H 'Content-Type: application/json' -d'
{
  "title": "My first blog entry",
  "text":  "Just trying this out...",
  "date":  "2014/01/01"
}
'  상술 한 방법 을 사용 하면 문서 에 업 데 이 트 를 가 져 올 수 있 기 때문에, 때때로 우 리 는 문 서 를 업데이트 하고 싶 지 않다.그래서 다음 과 같은 두 가지 방법 으로 만 들 수 있 습 니 다.
        op_type    -     :
PUT /website/blog/123?op_type=create
{ ... }
        URL      /_create :
PUT /website/blog/123/_create
{ ... }  이렇게 하면 성공 적 으로 생 성 되 었 을 때 201 을 되 돌려 줍 니 다. 그렇지 않 으 면 409 충돌 응답 코드 를 되 돌려 줍 니 다.자동 생 성 id
curl -X POST "localhost:9200/website/blog/" -H 'Content-Type: application/json' -d'
{
  "title": "My second blog entry",
  "text":  "Still trying this out...",
  "date":  "2014/01/01"
}
'  문서 되 찾기
curl -X GET "localhost:9200/website/blog/123?pretty"  문서 가 존재 하 는 지 확인 합 니 다.
curl -i -XHEAD http://localhost:9200/website/blog/123  문서 삭제
curl -X DELETE "localhost:9200/website/blog/123"  처리 병발
병발 에 대해 두 가지 방식 이 있다.
비관 적 인 병행 제어 라 는 방법 은 관계 형 데이터 베이스 에 의 해 광범 위 하 게 사용 되 고 있 으 며, 변경 충돌 이 발생 할 수 있다 고 가정 하기 때문에 충돌 을 방지 하기 위해 자원 방문 을 막는다.한 줄 의 데 이 터 를 읽 기 전에 잠 그 고 잠 긴 스 레 드 만 이 줄 의 데 이 터 를 수정 할 수 있 도록 하 는 전형 적 인 예 이다.
Elasticsearch 에서 사용 하 는 이러한 방법 은 충돌 이 발생 할 수 없다 고 가정 하고 시도 하고 있 는 작업 을 막 지 않 습 니 다.그러나 원본 데이터 가 읽 기와 쓰기 에서 수정 되면 업데이트 가 실 패 됩 니 다.응용 프로그램 은 다음 에 충돌 을 어떻게 해결 해 야 할 지 결정 할 것 이다.예 를 들 어 새로운 데 이 터 를 다시 시도 하거나 사용 하거나 관련 상황 을 사용자 에 게 보고 할 수 있다.
낙관적 인 병행 제어 지정 버 전 번 호 를 사용 하여 응용 을 수정 합 니 다.
curl -X PUT "localhost:9200/website/blog/1?version=1" -H 'Content-Type: application/json' -d'
{
  "title": "My first blog entry",
  "text":  "Starting to get the hang of this..."
}
'  외부 시스템 을 통한 버 전 관리
curl -X PUT "localhost:9200/website/blog/2?version=5&version_type=external" -H 'Content-Type: application/json' -d'
{
  "title": "My first external blog entry",
  "text":  "Starting to get the hang of this..."
}
'  문서 의 부분 업데이트
curl -X POST "localhost:9200/website/blog/1/_update" -H 'Content-Type: application/json' -d'
{
   "doc" : {
      "tags" : [ "testing" ],
      "views": 0
   }
}
'  문서 일괄 되 찾기
curl -X GET "localhost:9200/_mget" -H 'Content-Type: application/json' -d'
{
   "docs" : [
      {
         "_index" : "website",
         "_type" :  "blog",
         "_id" :    2
      },
      {
         "_index" : "website",
         "_type" :  "pageviews",
         "_id" :    1,
         "_source": "views"
      }
   ]
}
'  기본 색인 값 이나 형식 값 을 사용 하면 덮어 쓸 수 있 습 니 다.
curl -X GET "localhost:9200/website/blog/_mget" -H 'Content-Type: application/json' -d'
{
   "docs" : [
      { "_id" : 2 },
      { "_type" : "pageviews", "_id" :   1 }
   ]
}
'  결론.
Elasticsearch 의 사용 은 매우 간단 하고 편리 합 니 다. 응용 개발 자 에 게 Elasticsearch 를 바탕 으로 자신의 응용 프레임 워 크 를 통합 하면 Elasticsearch 를 쉽게 사용 할 수 있 습 니 다. 또한 Elasticsearch 는 NoSQL 의 저장 방식 으로 도 사용 할 수 있 습 니 다. 즉, Elasticsearch 를 사용 하여 저장 과 데이터 분석, 검색 등 기능 을 할 수 있 고 실현 도 매우 강력 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Rails Turbolinks를 페이지 단위로 비활성화하는 방법원래 Turobolinks란? Turbolinks는 링크를 생성하는 요소인 a 요소의 클릭을 후크로 하고, 이동한 페이지를 Ajax에서 가져옵니다. 그 후, 취득 페이지의 데이터가 천이 전의 페이지와 동일한 것이 있...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.