关键点
布置一个html用来做展示画面
同项目下布置一个php文件用来做跳板
访问Elastic search 布置的数据接口
html代码(index.html)(1)
下上url为docker项目发布后的地址
提示:http://192.163.312.321:321/demo/elastic_search/elastic_search.php
其中总项目名称为:elastic_search
其中php项目名称为:elastic_search.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<!-- copy -->
<meta name="description" content="{$articles.desc}" />
<meta name="keywords" content="{$articles.keywords}" />
<!-- copy -->
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="./js/jquery-3.3.1.min.js"></script>
<script>
// encodeURIComponent为编译方法,用于处理参数中中文被乱码的问题,需要decodeURIComponent()解码。
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
function Search() {
$.ajax({
url: "http://192.163.312.321:321/demo/elastic_search/elastic_search.php",
type: "GET",
data: {
"key":encodeURIComponent($("#selectvalue").val())
},
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: "json",
success: function(response) {
console.log("Response:", response);
$("#content").html(response['hits']['hits'][0]['_source']['column2'])
console.log(response['hits']['hits'][0]['_source']['column2']);
}
});
}
</script>
</head>
<body>
<div>
<label>中文:</label> <input style="width: 100px;" type="text" id="selectvalue"></input> <button onclick="Search()">检索</button>
<br/>
日文:<div id="content"></div>
</div>
</body>
</html>
PHP代码(elastic_search.php)(2)
是通过Elastic search搭建到docker上得服务接口。(3)
http://本地ip:9200/test/_search
<?php
$url = "http://191.167.321.321:9200/test/_search?q=column4:".rawurlencode(urldecode($_GET['key']));
// 初期化curl
// urldecode方法解析url加密
if(urldecode($_GET['key'])){
// 发送请求服务 返回查询后的数据
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => "GET"
));
//実行curl
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}else {
echo "";
}
数据接口(3)
此处搭建至Dcoker中的参考路径
说明
http://191.167.321.321:9200/test/_search
test为在网段设置的数据组名称,
_search可理解为固定搭配
采用的是上传文件的方式,添加数据。上传后会自动生成索引
搭建完毕后可以通过(http://本地ip:5601/app/home#/)访问网页端
参数样板
csv内容
NO,日语,词性,中文
1,ちゅうごくじん(中国人),〔名〕,中国人
2,にほんじん(日本人),〔名〕,日本人
3,かんこくじん(韓国人),〔名〕,韩国人
4,アメリカじん(~人),〔名〕,美国人
5,フランスじん(~人),〔名〕,法国人
6,がくせい(学生),〔名〕,(大)学生
7,せんせい(先生),〔名〕,老师
8,りゅうがくせい(留学生),〔名〕,留学生
9,きょうじゅ(教授),〔名〕,教授
10,しゃいん(社員),〔名〕,职员
11,かいしゃいん(会社員),〔名〕,公司职员
12,てんいん(店員),〔名〕,店员
13,けんしゅうせい(研修生),〔名〕,进修生
14,きぎょう(企業),〔名〕,企业
15,だいがく(大学),〔名〕,大学
16,ちち(父),〔名〕,(我)父亲
17,かちょう(課長),〔名〕,科长
18,しゃちょう(社長),〔名〕,总经理,社
19,でむかえ(出迎え),〔名〕,迎
20,あのひと(あの人),〔名〕,那个人
21,をたし,〔代〕,我
22,あなた,〔代〕,你
23,どうも,〔副〕,非常,很
24,はい,〔叹〕,哎,是(应答);是的
25,いいえ,〔叹〕,不,不是
26,あっ,〔叹〕,哎,哎呀
27,り(李),〔专〕,李
28,おう(王),〔专〕,王
返回的json参数
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 61,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "cxu874sBAjx6jxA-kE1w",
"_score": 1.0,
"_source": {
"column1": "NO",
"column4": "中文",
"column3": "词性",
"column2": "日语"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "dBu874sBAjx6jxA-kE1w",
"_score": 1.0,
"_source": {
"column1": "1",
"column4": "中国人",
"column3": "〔名〕",
"column2": "ちゅうごくじん(中国人)"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "dRu874sBAjx6jxA-kE1w",
"_score": 1.0,
"_source": {
"column1": "2",
"column4": "日本人",
"column3": "〔名〕",
"column2": "にほんじん(日本人)"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "dhu874sBAjx6jxA-kE1w",
"_score": 1.0,
"_source": {
"column1": "3",
"column4": "韩国人",
"column3": "〔名〕",
"column2": "かんこくじん(韓国人)"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "dxu874sBAjx6jxA-kE1w",
"_score": 1.0,
"_source": {
"column1": "4",
"column4": "美国人",
"column3": "〔名〕",
"column2": "アメリカじん(~人)"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "eBu874sBAjx6jxA-kE1w",
"_score": 1.0,
"_source": {
"column1": "5",
"column4": "法国人",
"column3": "〔名〕",
"column2": "フランスじん(~人)"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "eRu874sBAjx6jxA-kE1w",
"_score": 1.0,
"_source": {
"column1": "6",
"column4": "(大)学生",
"column3": "〔名〕",
"column2": "がくせい(学生)"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "ehu874sBAjx6jxA-kE1w",
"_score": 1.0,
"_source": {
"column1": "7",
"column4": "老师",
"column3": "〔名〕",
"column2": "せんせい(先生)"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "exu874sBAjx6jxA-kE1w",
"_score": 1.0,
"_source": {
"column1": "8",
"column4": "留学生",
"column3": "〔名〕",
"column2": "りゅうがくせい(留学生)"
}
},
{
"_index": "test",
"_type": "_doc",
"_id": "fBu874sBAjx6jxA-kE1w",
"_score": 1.0,
"_source": {
"column1": "9",
"column4": "教授",
"column3": "〔名〕",
"column2": "きょうじゅ(教授)"
}
}
]
}
}
访问发布的画面在进行查询,搜索。
http://本地ip:8080/demo/elastic_search/