我一直试图解决这个问题超过 20 天,但无法使其正常工作。我也是 Elasticsearch 的新手,因为这是我们实施的第一个项目。
第 1 步:我已经在 Ubuntu 14.04 中安装了 Elasticsearch 2.0。我能够使用以下代码创建新索引
$hosts = array('our ip address:9200');
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$index = "IndexName";
$params['index'] = $index;
$params['type'] = 'xyz';
$params['body']["id"] = "1";
$params['body']["title"] = "C++ Developer - C# Developer";
$client->index($params);
Run Code Online (Sandbox Code Playgroud)
一旦上面的代码运行成功创建索引。
第 2 步:能够使用以下链接查看创建的索引
http://our ip address:9200/IndexName/_search?q=C%23&pretty
{
"took" : 30,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 9788,
"max_score" : 0.8968174,
"hits" : [ {
"_index" : "IndexName",
"_type" : "xyz",
"_id" : "1545680",
"_score" : 0.8968174,
"_source":{"id":"1545680","title":"C\\+\\+ and C\\# \\- Software Engineer"}
}, {
"_index" : "IndexName",
"_type" : "xyz",
"_id" : "1539778",
"_score" : 0.853807,
"_source":{"id":"1539778","title":"Rebaca Technologies Hiring in C\\+\\+"}
}
....
Run Code Online (Sandbox Code Playgroud)
如果您注意到上述搜索结果,我将得到第二个没有 c# 的结果。即使我只在搜索“C”时得到相同的结果
根据包含特殊字符(如 +、# 或 .
我按照以下指南保留特殊字符
转义特殊字符
Lucene 支持转义作为查询语法一部分的特殊字符。当前列表特殊字符是
+ - && || ! ( ) { } [ ] ^ " ~ * ? : \
Run Code Online (Sandbox Code Playgroud)
要转义这些字符,请在字符前使用 \。例如要搜索 (1+1):2 使用查询:
\(1\+1\)\:2
Run Code Online (Sandbox Code Playgroud)
我在转义字符组中添加了#。
第 3 步:
在 php 中,同时将特殊字符传递给 Elasticsearch 搜索功能,我像下面这样转义
$keyword = str_replace(""",'"',$keyword);
$keyword = str_replace("+","\+",$keyword);
$keyword = str_replace(".","\.",$keyword);
$keyword = str_replace("#","\#",$keyword);
$keyword = str_replace("/","\/",$keyword);
$keyword = trim($keyword);
$params['body']['query']['query_string'] = array("query" => $keyword,"default_operator" => "AND" ,"fields" => array("title"));
Run Code Online (Sandbox Code Playgroud)
$client->search($params);
请帮助我如何使特殊字符在 Elasticsearch 中工作
| 归档时间: |
|
| 查看次数: |
475 次 |
| 最近记录: |