Elastic App Search 透過前端連接來執行搜尋的動作,本文僅示範JavaScript連接
使用CDN的方式引入js檔
<script src="https://cdn.jsdelivr.net/npm/@elastic/app-search-javascript@7.8.0/dist/elastic_app_search.umd.js"></script>
js用法範例
var client = ElasticAppSearch.createClient({
searchKey: "your search key",
endpointBase: "your endpoint",
engineName: "your engineName"
});
var options = {
search_fields: { name_zh: {} },
result_fields: { id: { raw: {} },
name_zh: { raw: {} },
selling_price:{raw:{}} }
};
client
.search("貓砂", options)
.then(resultList => {
resultList.results.forEach(result => {
console.log(`id: ${result.getRaw("id")}
raw: ${result.getRaw("name_zh")} :
${result.getRaw("selling_price")}`);
});
})
.catch(error => {
console.log(`error: ${error}`);
});
完整檔案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://cdn.jsdelivr.net/npm/@elastic/app-search-javascript@7.8.0/dist/elastic_app_search.umd.js"></script>
<title>Document</title>
</head>
<body>
</body>
<script>
var client = ElasticAppSearch.createClient({
searchKey: "your search key",
endpointBase: "your endpoint",
engineName: "your engineName"
});
var options = {
search_fields: { name_zh: {} },
result_fields: { id: { raw: {} }, name_zh: { raw: {} }, selling_price:{raw:{}} }
};
client
.search("貓砂", options)
.then(resultList => {
resultList.results.forEach(result => {
console.log(`id: ${result.getRaw("id")} raw: ${result.getRaw("name_zh")} : ${result.getRaw("selling_price")}`);
});
})
.catch(error => {
console.log(`error: ${error}`);
});
</script>
</html>
搜尋結果
Learn More →
回傳結果第一條是在Elastic App Search 設定 Curation 的結果
參考資料的CDN是錯誤的
需要使用
https://cdn.jsdelivr.net/npm/@elastic/app-search-javascript@7.8.0/dist/elastic_app_search.umd.js
或者到 jsDelivr
查詢最新版本
Elastic App Search 後端連接有兩種方式,一種是透過ELK的Elastic 去取得 Elastic App 的資料,第二種是直接用Java 打API的方式,去取得 Elastic App Search 的資料 一、透過ELK查詢 Elastic App Search 取得Elastic App Search 的 index,可以從Kibana取得所有的Elastic 列表,GET _cat/indices?v&index=*engine* 回傳的結果
Apr 8, 2021Elastic cloud 透過 Spring 實做遠端連接功能 一、依賴項 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
Apr 8, 2021Elasticsearch docker 透過 Spring 實做本地連接功能 一、依賴項 <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.12.0</version> </dependency> 二、寫入資料
Apr 8, 2021安裝 IKAnalyzer 一、安裝步驟 如果是使用官方ELK Docker yml 安裝的話,會有三個 cluster,這裡示範在es01安裝中文分詞 進入 elasticsearch bash docker exec -it ex01 bash 在 Elasticsearch bash 中,下載並安裝 IKAnalyzer
Apr 8, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up