제목(title) 뿐만 아니라 내용(content)을 포함해서 검색한다. 
엘라스틱서치 적용 후기)가 포함된 데이터를 조회하고 싶을 때 어떻게 쿼리를 작성하면 되는 지 살펴보자. DELETE /boards PUT /boards { "mappings": { "properties": { "title": { "type": "text", "analyzer": "nori" }, "content": { "type": "text", "analyzer": "nori" } } } }
// title, content 둘 다에 키워드 포함 POST /boards/_doc { "title": "엘라스틱서치 적용 후기", "content": "회사 프로젝트에 엘라스틱서치를 적용한 후기를 공유합니다." } // title에만 키워드 포함 POST /boards/_doc { "title": "엘라스틱서치를 사용해보니", "content": "검색 엔진 도입 후 성능이 향상되었습니다." } // content에만 키워드 포함 POST /boards/_doc { "title": "검색엔진 도입 사례", "content": "이번 프로젝트에 엘라스틱서치를 적용한 후 많은 개선 효과가 있었습니다." } // title, content 둘 다 포함 안 됨 POST /boards/_doc { "title": "레디스 캐시 사용기", "content": "서비스 속도 개선을 위해 캐시 시스템을 사용했습니다." }
GET /boards/_search { "query": { "multi_match": { "query": "엘라스틱서치 적용 후기", "fields": ["title", "content"] } } }
{ "took": 33, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 3.79424, "hits": [ // title, content 둘 다에 키워드 포함 { "_index": "boards", "_id": "2k-SmJYBWhYNXJPWbr6u", "_score": 3.79424, "_source": { "title": "엘라스틱서치 적용 후기", "content": "회사 프로젝트에 엘라스틱서치를 적용한 후기를 공유합니다." } }, // content에만 키워드 포함 { "_index": "boards", "_id": "3E-SmJYBWhYNXJPWd740", "_score": 1.7749425, "_source": { "title": "검색엔진 도입 사례", "content": "이번 프로젝트에 엘라스틱서치를 적용한 후 많은 개선 효과가 있었습니다." } }, // title에만 키워드 포함 { "_index": "boards", "_id": "20-SmJYBWhYNXJPWc75z", "_score": 1.3862942, "_source": { "title": "엘라스틱서치를 사용해보니", "content": "검색 엔진 도입 후 성능이 향상되었습니다." } } ] } }
title 또는 content 필드에 검색 키워드가 포함된 데이터를 조회했다. title이랑 content 둘 다에 키워드가 포함되지 않은 데이터는 제외됐다. 내용(content)에만 키워드가 포함된 글보다 제목(title)에만 키워드가 포함된 글을 더 상위노출 시키고 싶을 수 있다. 즉, 제목(title)에 검색 키워드가 등장한다면 더 관련성 높은 데이터라고 판단하고 싶을 수 있다. 그럴 때 가중치를 활용한다. GET /boards/_search { "query": { "multi_match": { "query": "엘라스틱서치 적용 후기", "fields": ["title^2", "content"] // title에 2배 더 높은 score를 부여 } } }
{ "took": 10, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 7.58848, "hits": [ // title, content 둘 다에 키워드 포함 { "_index": "boards", "_id": "2k-SmJYBWhYNXJPWbr6u", "_score": 7.58848, "_source": { "title": "엘라스틱서치 적용 후기", "content": "회사 프로젝트에 엘라스틱서치를 적용한 후기를 공유합니다." } }, // title에만 키워드 포함 { "_index": "boards", "_id": "20-SmJYBWhYNXJPWc75z", "_score": 2.7725885, "_source": { "title": "엘라스틱서치를 사용해보니", "content": "검색 엔진 도입 후 성능이 향상되었습니다." } }, // content에만 키워드 포함 { "_index": "boards", "_id": "3E-SmJYBWhYNXJPWd740", "_score": 1.7749425, "_source": { "title": "검색엔진 도입 사례", "content": "이번 프로젝트에 엘라스틱서치를 적용한 후 많은 개선 효과가 있었습니다." } } ] } }
multi_match 키워드를 사용하면 된다. 다음 강의에서는 다른 검색 기능을 배워보자.