-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from nhnacademy-be5-T3Team/feature/elastic
Feature/elastic
- Loading branch information
Showing
6 changed files
with
166 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/t3t/frontserver/elastic/controller/AutocompleteController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.t3t.frontserver.elastic.controller; | ||
|
||
import com.t3t.frontserver.elastic.client.ElasticClient; | ||
import com.t3t.frontserver.model.response.BaseResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class AutocompleteController { | ||
private final ElasticClient elasticAdaptor; | ||
/** | ||
* | ||
* elasticsearch 기반 실시간 자동완성 | ||
* | ||
* @param prefix text 검색어 | ||
* @return 페이지로 정보를 가지고 이동 | ||
*/ | ||
|
||
@GetMapping("/autocomplete") | ||
public ResponseEntity<BaseResponse<List<String>>> proxyAutocomplete(@RequestParam String prefix) { | ||
return elasticAdaptor.autocomplete(prefix); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
.autocomplete-container { | ||
position: relative; | ||
} | ||
|
||
.autocomplete-items { | ||
position: absolute; | ||
border: 1px solid #d4d4d4; | ||
border-bottom: none; | ||
border-top: none; | ||
z-index: 99; | ||
top: 100%; | ||
left: 0; | ||
right: 0; | ||
background-color: white; | ||
overflow: hidden; | ||
border-radius: 4px; | ||
box-shadow: 0 4px 6px rgba(0,0,0,0.1); | ||
} | ||
|
||
.autocomplete-items div { | ||
padding: 10px; | ||
cursor: pointer; | ||
background-color: #fff; | ||
border-bottom: 1px solid #d4d4d4; | ||
font-size: 12px; | ||
} | ||
|
||
.autocomplete-items div:hover { | ||
background-color: #e9e9e9; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
document.addEventListener('DOMContentLoaded', function() { | ||
updateSearchTypeDisplay(); | ||
updateSearchQueryDisplay(); | ||
updateCategoryIdFromURL(); | ||
|
||
var searchInput = document.getElementById('searchQueryInput'); | ||
var resultsContainer = document.getElementById('autocompleteResults'); | ||
|
||
searchInput.addEventListener('input', function() { | ||
var query = this.value; | ||
if (query.length > 1) { | ||
fetch('/autocomplete?prefix=' + encodeURIComponent(query)) | ||
.then(response => response.json()) | ||
.then(response => { | ||
if (response.data && response.data.length) { | ||
resultsContainer.innerHTML = ''; | ||
response.data.forEach(item => { | ||
var resultItem = document.createElement('div'); | ||
resultItem.textContent = item; | ||
resultItem.addEventListener('click', function() { | ||
searchInput.value = this.textContent; | ||
resultsContainer.innerHTML = ''; | ||
resultsContainer.style.display = 'none'; | ||
}); | ||
resultsContainer.appendChild(resultItem); | ||
}); | ||
resultsContainer.style.display = 'block'; | ||
} else { | ||
resultsContainer.innerHTML = ''; | ||
resultsContainer.style.display = 'none'; | ||
} | ||
}) | ||
.catch(error => console.error('Error fetching autocomplete suggestions:', error)); | ||
} else { | ||
resultsContainer.innerHTML = ''; | ||
resultsContainer.style.display = 'none'; | ||
} | ||
}); | ||
|
||
document.addEventListener('click', function(event) { | ||
if (!searchInput.contains(event.target) && !resultsContainer.contains(event.target)) { | ||
resultsContainer.style.display = 'none'; | ||
} | ||
}); | ||
|
||
searchInput.addEventListener('focus', function() { | ||
if (resultsContainer.innerHTML !== '') { | ||
resultsContainer.style.display = 'block'; | ||
} | ||
}); | ||
}); | ||
|
||
function updateCategoryIdFromURL() { | ||
const path = window.location.pathname; | ||
const categoryPattern = /\/category\/(\d+)/; // '/category/' 다음에 오는 숫자를 찾는 정규 표현식 | ||
const match = path.match(categoryPattern); | ||
|
||
if (match && match[1]) { | ||
document.getElementById('categoryIdInput').value = match[1]; | ||
} else { | ||
document.getElementById('categoryIdInput').parentNode.removeChild(document.getElementById('categoryIdInput')); | ||
} | ||
} | ||
function setSearchType(type) { | ||
var typeNames = { | ||
'all': '통합검색', | ||
'book_name': '책 이름', | ||
'publisher_name': '출판사', | ||
'participant_name': '참여자' | ||
}; | ||
document.getElementById('searchTypeInput').value = type; | ||
document.getElementById('dropdownMenuButton').textContent = typeNames[type]; | ||
} | ||
function updateSearchTypeDisplay() { | ||
var searchParams = new URLSearchParams(window.location.search); | ||
var currentType = searchParams.get('searchType') || 'all'; | ||
setSearchType(currentType); | ||
} | ||
function updateSearchQueryDisplay() { | ||
var searchParams = new URLSearchParams(window.location.search); | ||
var currentQuery = searchParams.get('query'); | ||
if (currentQuery) { | ||
document.getElementById('searchQueryInput').value = currentQuery; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters