如何使用ElasticSearch以我键入的方式建议(结果)城市
我是Elasticsearch的新手,我花了几个小时试图解决这个问题,所以,如果您愿意帮助我,请提前感谢 :不太简短的解释:我目前拥有的以及我努力实现的目标: 我创建了一个CouchDB数据库,其中包含8000多个西班牙城市和省份的文档。另一方面,我有一个带有jQuery自动完成功能的HTML表单,在键入时显示结果。我从我创建的PHP Laravel服务提供商连接到ElasticSearch,并返回jQuery自动完成的结果。我想这可以通过从客户端直接连接到ElasticSearch来实现,但出于安全原因,我现在更喜欢这样 :问题是: 我从ElasticSearch得到的结果并不完全是我所期望的,我不知道如何修复我所拥有的,或者它是否是正确的方法。我不知道bool查询是为了满足我的需要,还是应该使用其他类型的查询 只有当我键入与数据库中的单词完全相同的单词时,我才会得到结果: 如果我输入Álava,我会得到结果,但对于阿拉瓦来说,Á重音并不重要 在键入完整的单词之前,我不会获得结果: 如果我输入Albacet,我会得到结果,但不会得到Albacet的结果 我使用用于ElasticSearch的CouchDB River插件将CouchDB与ElasticSearch>>同步,并使用以下命令:如何使用ElasticSearch以我键入的方式建议(结果)城市,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,我是Elasticsearch的新手,我花了几个小时试图解决这个问题,所以,如果您愿意帮助我,请提前感谢 :不太简短的解释:我目前拥有的以及我努力实现的目标: 我创建了一个CouchDB数据库,其中包含8000多个西班牙城市和省份的文档。另一方面,我有一个带有jQuery自动完成功能的HTML表单,在键入时显示结果。我从我创建的PHP Laravel服务提供商连接到ElasticSearch,并返回jQuery自动完成的结果。我想这可以通过从客户端直接连接到ElasticSearch来实现,但出
curl -XPUT 'localhost:9200/_river/spain_locales/_meta' -d '{
"type" : "couchdb",
"couchdb" : {
"host" : "localhost",
"port" : 5984,
"db" : "spain_locales",
"filter" : null
},
"index" : {
"index" : "spain_locales",
"type" : "spain_locales",
"bulk_size" : "100",
"bulk_timeout" : "10ms"
}
}'
curl -XPUT 'localhost:9200/spain_locales/' -d '
{
"settings": {
"analysis": {
"analyzer": {
"folding": {
"tokenizer": "standard",
"filter": [ "lowercase", "asciifolding" ]
}
}
}
},
"uuid":"KwKrBc3uQoG5Ld1nOdc5rQ"
}'
我还尝试了:
curl -XPUT 'localhost:9200/_river/spain_locales/_meta' -d '{
"type" : "couchdb",
"couchdb" : {
"host" : "localhost",
"port" : 5984,
"db" : "spain_locales",
"filter" : null
},
"index" : {
"number_of_shards" : 2,
"refresh_interval" : "1s",
"analysis": {
"analyzer": {
"folding": {
"tokenizer": "standard",
"filter": [ "lowercase", "asciifolding" ]
}
}
},
"index" : "spain_locales",
"type" : "spain_locales",
"bulk_size" : "100",
"bulk_timeout" : "10ms"
}
}'
以上所有操作均未返回任何错误并成功创建了_river同步,但仍存在重音和整词问题
我还尝试通过终端使用以下命令以某种方式应用所需的过滤器:
curl -XPUT 'localhost:9200/_river/spain_locales/_meta' -d '{
"type" : "couchdb",
"couchdb" : {
"host" : "localhost",
"port" : 5984,
"db" : "spain_locales",
"filter" : null
},
"index" : {
"index" : "spain_locales",
"type" : "spain_locales",
"bulk_size" : "100",
"bulk_timeout" : "10ms"
}
}'
curl -XPUT 'localhost:9200/spain_locales/' -d '
{
"settings": {
"analysis": {
"analyzer": {
"folding": {
"tokenizer": "standard",
"filter": [ "lowercase", "asciifolding" ]
}
}
}
},
"uuid":"KwKrBc3uQoG5Ld1nOdc5rQ"
}'
但我得到了以下错误:
{"error":"IndexAlreadyExistsException[[spain_locales] already exists]","status":400}
CouchDB文档示例:
{
"_id": "1",
"_rev": "1-087ddbe8593f68f1d7d37a9c3f6de787",
"Provincia": "Álava",
"Poblacion": "Alegría-Dulantzi",
"helper": ""
}
{
"_id": "10",
"_rev": "1-ce38dcdabeb3b34d34d2296c6e2fdf24",
"Provincia": "Álava",
"Poblacion": "Ayala/Aiara",
"helper": ""
}
{
"_id": "100",
"_rev": "1-72e66601e378ee48519aa93601dc0717",
"Provincia": "Albacete",
"Poblacion": "Herrera (La)",
"helper": "La Herrera"
}
PHP服务提供商/控制器:
public function searchzones(){
$q = (Input::has('term')) ? Input::get('term') : 'null';
$params['index'] = 'spain_locales';
$params['type'] = 'spain_locales';
$params['body']['query']['bool']['should'] = array(
array('match' => array('Poblacion' => $q)),
array('match' => array('Provincia' => $q))
);
$query = $this->elasticsearch->search($params);
if ($query['hits']['total'] >= 1){
$results = $query['hits']['hits'];
foreach ($results as $zone) {
$databag[] = array( "value" => $zone['_source']['Poblacion'].', '.$zone['_source']['Provincia'],
"state" => $zone['_source']['Provincia'],
"city" => $zone['_source']['Poblacion'],
);
}
} else {
$results = ['res' => null];
$databag[] = array();
}
return $databag;
} // End Search Zones
jQuery JavaScript:
// Sugest locations when user type in zones
$(document).ready(function() {
$('#zones').autocomplete({
source : applink + 'ajax/searchzones',
select : function(event, ui){
console.log(ui);
}
}); // End autocomplete
}); // End Document ready
HTML表单部分Twitter引导:
<div class="form-group">
<div class="input-group input-append dropdown">
<input type="text" class="form-control typeahead" placeholder="City name" name="zones" id="zones">
<div class="input-group-btn" >
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
<ul class="dropdown-menu dropdown-menu-right" id="dropZonesAjax">
</ul>
</div>
</div>
<div id="zonesAjax"></div>
</div>
我找到了以下资源:但我不知道如何实现
非常感谢您的时间和帮助!
对不起我的英语 尝试在索引之前创建映射。然后,您可以定义前面提到的分析器并将其分配给字段:
{
"settings": {
"analysis": {
"analyzer": {
"folding": {
"tokenizer": "standard",
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
},
"mappings": {
"locales": {
"properties": {
"Provincia": {
"type": "string",
"analyzer": "folding"
},
"Poblacion": {
"type": "string",
"analyzer": "folding"
},
"helper": {
"type": "string"
}
}
}
}
}
您可以发布您用于此索引的映射吗?还有,你签出了关于完成建议的文档了吗?嗨!谢谢你的回答!我不知道你说的映射是什么意思。我没有用elasticsearc做更多的东西。。。只是我解释的东西。