【问题标题】:ElasticSearch how to search comma separated string inside comma separated fields?ElasticSearch如何在逗号分隔的字段中搜索逗号分隔的字符串?
【发布时间】:2016-04-24 12:45:59
【问题描述】:

如果我应该根据逗号分隔的字段搜索逗号分隔的字符串,因此我在映射中执行以下操作,但显示 MapperParsingException[Analyzer [comma] not found for field [conduct_days]] 错误。

            $course = new Course();
            $course->no = '1231321';
            .......
            .......
            $course->save();

            // Now index the new created course

            $client = \Elasticsearch\ClientBuilder::create()->build();




            $params = [
                'index' => 'my_index',
                'type' => 'my_resources',
                'body' => [
                    'my_resources' => [
                        '_source' => [
                            'enabled' => true
                        ],
                        'settings' => [
                            "analysis" => [
                                "tokenizer" => [
                                    "comma" => [
                                        "type" => "pattern",
                                        "pattern" => ","
                                    ]
                                ],
                                "analyzer" => [
                                    "comma" => [
                                        "type" => "custom",
                                        "tokenizer" => "comma"
                                    ]
                                ]
                            ]
                        ],
                        'properties' => [
                            'conduct_days' => array(
                                'type' => 'string',
                                'analyzer' => 'comma'
                            ),
                            'no' => array(
                                'type' => 'string',
                                'analyzer' => 'standard'
                            ),
                            'created_at' => array(
                                'type' => 'date_time',
                                "format"=>"YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
                            ),
                            'updated_at' => array(
                                'type' => 'date_time',
                                "format" => "YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
                            ),
                            'deleted_at' => array(
                                'type' => 'date_time',
                                "format" => "YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
                            ),
                            'created_by' => array(
                                'type' => 'string',
                                'analyzer' => 'standard'
                            ),
                            'updated_by' => array(
                                'type' => 'string',
                                'analyzer' => 'standard'
                            ),
                            'deleted_by' => array(
                                'type' => 'string',
                                'analyzer' => 'standard'
                            )
                        ]
                    ]
                ]
            ];

            // Update the index mapping
            $client->indices()->putMapping($params);

            $params = [
                'index' => 'promote_kmp',
                'type' => 'courses',
                'id' => uniqid(),
                'body' => [
                    'id'                      => $course->id,
                    'conduct_days'            => $course->conduct_days,
                    'no'                      => $course->no,
                    'created_at'              => $course->created_at,
                    'created_by'              => $loggedInUser,
                ]
            ];
            $client->index($params);

假设我必须在具有1,21,2,31,3,5,6 等的行为日字段中搜索1,3,5,7。对于搜索,我想我应该爆炸搜索词,例如如果搜索词是1,2,我应该搜索两次,第一次搜索1,然后搜索2。还有其他搜索解决方案吗?

【问题讨论】:

    标签: php search indexing elasticsearch


    【解决方案1】:

    您不能在putMapping 调用中传递settings,它们将被忽略。 settings 旨在传递给 create 调用以创建索引

        $params = [
            'index' => 'my_index',
            'body' => [
                        'settings' => [
                            "analysis" => [
                                "tokenizer" => [
                                    "comma" => [
                                        "type" => "pattern",
                                        "pattern" => ","
                                    ]
                                ],
                                "analyzer" => [
                                    "comma" => [
                                        "type" => "custom",
                                        "tokenizer" => "comma"
                                    ]
                                ]
                            ]
                        ]
            ]
        ];
    
        $response = $client->indices()->create($params);
    

    然后您可以使用映射类型定义但不使用settings 调用putMapping

            $params = [
                'index' => 'my_index',
                'type' => 'my_resources',
                'body' => [
                    'my_resources' => [
                        '_source' => [
                            'enabled' => true
                        ],
                        'properties' => [
                            'conduct_days' => array(
                                'type' => 'string',
                                'analyzer' => 'comma'
                            ),
                            'no' => array(
                                'type' => 'string',
                                'analyzer' => 'standard'
                            ),
                            'created_at' => array(
                                'type' => 'date_time',
                                "format"=>"YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
                            ),
                            'updated_at' => array(
                                'type' => 'date_time',
                                "format" => "YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
                            ),
                            'deleted_at' => array(
                                'type' => 'date_time',
                                "format" => "YYYY-MM-dd HH:mm:ss||MM/dd/yyyy||yyyy/MM/dd"
                            ),
                            'created_by' => array(
                                'type' => 'string',
                                'analyzer' => 'standard'
                            ),
                            'updated_by' => array(
                                'type' => 'string',
                                'analyzer' => 'standard'
                            ),
                            'deleted_by' => array(
                                'type' => 'string',
                                'analyzer' => 'standard'
                            )
                        ]
                    ]
                ]
            ];
    
            // Update the index mapping
            $client->indices()->putMapping($params);
    

    更新

    但是,在您的情况下,我认为最好的办法是创建一个包含设置(即分析器)和映射的 index template。然后,您的应用所需要关心的只是调用index() 来索引新的课程文档。 ES 将在正确的时间创建索引和映射,即第一次索引你的第一个课程文档。

    请注意,为了做到这一点,您需要

    1. 删除当前索引以及代码中的 indices->create()indices->putMapping() 调用
    2. 使用 /head/ 插件或 Sense 或简单的 curl 创建索引模板
    3. 仅在您的代码中调用 index()

    【讨论】:

    • 但是因为我应该多次创建映射,例如在创建操作、更新和删除中,我应该如何创建应该创建一次的索引。现在我改变我的代码,就像你说的那样显示这个错误IndexAlreadyExistsException[[promote_kmp] already exists]
    • 通常,您创建一个索引和一个映射类型,然后您就不必再这样做了。您可以使用$response = $client->indices()->delete(['index' => 'promote_kmp']); 在重新创建索引之前删除它。然后你可以再次拨打create
    • 但是通过删除索引,数据安全吗?我想我会丢失索引文档。
    • 那我不明白你为什么要多次重新应用映射。如果你的映射是稳定的,你可以创建一个索引,放置一个映射,然后你就可以进行查询了。
    • 由于存储所有用户操作相关数据的原因(不更新文档,每次插入新文档),我使用 ElasticSearch-PHP 和 laravel,但我不想使用 from动态映射,我应该创建自己的映射,所以每次索引新文档之前,我应该做映射,如果我想创建一次映射,我应该在哪里做?我应该在哪里创建映射?
    猜你喜欢
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多