ElasticSearch如何在逗号分隔的字段中搜索逗号分隔的字符串?

时间:2016-01-19 06:42:13

标签: php search indexing elasticsearch

在我应该用逗号分隔的字段搜索逗号分隔字符串的情况下,因此我在映射中执行以下操作但显示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,3,5,71,21,2,3以及1,3,5,6等等。对于搜索,我认为我应该展开搜索字词,例如如果搜索字词为1,2,我应该搜索两次,首先搜索1,然后搜索2。还有其他搜索解决方案吗?

1 个答案:

答案 0 :(得分:1)

您无法在settings电话中传递putMapping,它们将被忽略。 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);

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

        $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 / plugin或Sense或简单的curl
  3. 创建索引模板
  4. 只能在您的代码中致电index()
相关问题