php - 了解ElasticSearch路由

标签 php elasticsearch elastica

我正在尝试使用elasticsearch路由映射来加快一些查询的速度,但是我没有得到预期的结果集(现在还不担心查询性能)

我正在使用Elastic来设置映射:

    $index->create(array('number_of_shards' => 4,
            'number_of_replicas' => 1,
            'mappings'=>array("country"=>array("_routing"=>array("path"=>"countrycode"))),
            'analysis' => array(
                    'analyzer' => array(
                            'indexAnalyzer' => array(
                                    'type' => 'keyword',
                                    'tokenizer' => 'nGram',
                                    'filter' => array('shingle')
                            ),
                            'searchAnalyzer' => array(
                                    'type' => 'keyword',
                                    'tokenizer' => 'nGram',
                                    'filter' => array('shingle')
                            )
                    )
            )  ), true);

如果我理解正确,那么应该发生的是,每个结果现在都应该有一个名为“countrycode”的字段,其中包含“country”的值。

_mapping的结果如下所示:
{"postcode":
    {"postcode":
        {"properties":
               {
               "area1":{"type":"string"},
               "area2":{"type":"string"},
               "city":{"type":"string",
               "include_in_all":true},
               "country":{"type":"string"},
               "country_iso":{"type":"string"},
               "country_name":{"type":"string"},
               "id":{"type":"string"},
               "lat":{"type":"string"},
               "lng":{"type":"string"},
               "location":{"type":"geo_point"},
               "region1":{"type":"string"},
               "region2":{"type":"string"},
               "region3":{"type":"string"},
               "region4":{"type":"string"},
               "state_abr":{"type":"string"},
               "zip":{"type":"string","include_in_all":true}}},
               "country":{
                   "_routing":{"path":"countrycode"},
                   "properties":{}
                          }
                 }
           }

如果所有数据都在索引中(如果我运行此命令):
http://localhost:9200/postcode/_search?pretty=true&q=country:au

它总共回应了15740个项目

我所期望的是,如果我像这样运行查询:
http://localhost:9200/postcode/_search?routing=au&pretty=true

然后我期望它以15740个结果响应

而是返回120617个结果,其中包括国家/地区为== au的结果

我确实注意到结果中的分片数量从4个增加到1个,所以有些事情起作用了。

我期望结果集中会有一个名为“countrycode”的项目(来自漫游映射),而没有

因此,我认为此时我对路由的理解是错误的。也许所有路由所做的只是告诉它要查找的碎片,而不是要查找的碎片?换句话说,如果其他国家/地区代码也恰好也落入了该特定分片,那么这些查询的编写方式是否只会带回该分片中的所有记录?

因此,我再次尝试查询,这次添加了一些信息。
 http://localhost:9200/postcode/_search?routing=AU&pretty=true&q=country:AU 

我以为这样做会强制查询只给我澳大利亚的地名,但是这次只给了我3936个结果

因此,我不太确定自己做错了什么,我阅读的示例显示查询从需要过滤器变为仅使用match_all {},我认为这些查询只会返回与澳大利亚国家/地区代码匹配的查询。

感谢您的帮助,以使其正常工作。

几乎可以正常工作了,它现在可以在一个分片中为我提供正确数量的结果,但是create index不能正常工作,它会忽略我的number_of_shards设置,可能还会忽略其他设置
$index = $client->getIndex($indexname);
    $index->create(array('mappings'=>array("$indexname"=>array("_routing"=>array("required"=>true))),'number_of_shards' => 6,
            'number_of_replicas' => 1,
            'analysis' => array(
                    'analyzer' => array(
                            'indexAnalyzer' => array(
                                    'type' => 'keyword',
                                    'tokenizer' => 'nGram',
                                    'filter' => array('shingle')
                            ),
                            'searchAnalyzer' => array(
                                    'type' => 'keyword',
                                    'tokenizer' => 'nGram',
                                    'filter' => array('shingle')
                            )
                    )
            )  ), true); 

最佳答案

我至少可以为您提供有关查找位置的更多信息:

http://localhost:9200/postcode/_search?routing=au&pretty=true

该查询确实的确转化为“将要发送国家/地区的文件的分片上的所有文件交给我”。

路由就是路由,它不会为您过滤结果。

我也注意到您正在混合使用“au”和“AU” ..,这也可能会混淆。

您应该尝试将路由元素上的required设置为true,以确保在建立索引时文档实际上与路由信息一起存储。

实际上,为确保使用正确的路由对您的文档建立索引,在对文档建立索引时,请将路由显式设置为小写(国家代码)。看看是否有帮助。

有关更多信息,请尝试阅读此博客文章:

http://www.elasticsearch.org/blog/customizing-your-document-routing/

希望这可以帮助 :)

关于php - 了解ElasticSearch路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17986451/

相关文章:

php - 难以从两页注册中传递变量

php - PHP 中的数据库插入返回未定义的字符串并注意未知列

elasticsearch - 如何使用脚本聚合 'ip'字段

elasticsearch - Kibana中的脚本字段:添加值

php - 通过返回的id结果数组的elasticsearch查询未按传入的id数组排序

php - SQL 获取下一次给定日期的日期戳

php - 如何在PHP代码中从数据库的第一行检索数据

elasticsearch - 如何将Googlecloud中的弹性kubernetes集群暴露给外部ip?

elasticsearch - 结合过滤和 bool 查询

symfony - "mappings"下无法识别的选项 "fos_elastica.indexes.app.types.user"