elasticsearch nested搜索

  • 在mapping中定义nested字段

定义一个学生信息的字段:

'properties' => [
    'student' => [
        'type' => 'nested',
        'properties' => [
            'id' => [
                'type' => 'integer'
            ],
            'name' => [
                'type' => 'text',
                // 下面两个可选
                'analyzer' => 'ik_smart',
                'search_analyzer' => 'ik_max_word'
            ],
            'gender' => [
                'type' => 'keyword'
            ]
        ]
    ]
]

nested也是可以嵌套的,比如再加上成绩:

'properties' => [
    'student' => [
        'type' => 'nested',
        'properties' => [
            'id' => [
                'type' => 'integer'
            ],
            'name' => [
                'type' => 'text',
                // 下面两个可选
                'analyzer' => 'ik_smart',
                'search_analyzer' => 'ik_max_word'
            ],
            'gender' => [
                'type' => 'keyword'
            ],
            'grade' => [
                'type' => 'nested',
                'properties' => [
                    'english' => [
                        'type' => 'float'
                    ],
                    'math' => [
                        'type' => 'float'
                    ]
                ]
            ]
        ]
    ]
]
  • 搜索nested字段

要找出名字中带字并且性别为的学生:

$this->client->search(
    [
        'index' => 'students',
        'body' => [
            'query' => [
                'bool' => [
                    'must' => [
                        [
                            'nested' => [
                                'path' => 'student',
                                'query' => [
                                    'bool' => [
                                        'must' => [
                                            [
                                                'match' => [
                                                    'student.name' => '月'
                                                ],
                                            ],
                                            [
                                                'term' => [
                                                    'student.gender' => 'm'
                                                ],
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
)
  • 遇到的错误:
[match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

这里是因为matchterm没有分开,解析出错了.

$this->client->search(
    [
        'index' => 'students',
        'body' => [
            'query' => [
                'bool' => [
                    'must' => [
                        [
                            'nested' => [
                                'path' => 'student',
                                'query' => [
                                    'bool' => [
                                        'must' => [
                                            [
                                                'match' => [
                                                    'student.name' => '月'
                                                ],
                                                'term' => [
                                                    'student.gender' => 'm'
                                                ],
                                            ],
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
)