PHP Elasticsearch在两个字段上搜索

时间:2018-09-24 07:34:13

标签: php elasticsearch

我想在两个字段中搜索。一个字段由PHP传递,另一个字段由用户在输入字段中输入。

有人可以在这里帮助我吗?

我的代码:

        $params = [
        'index' => 'search_documentation',
        'type' => 'document',
        'body' => [
            'size' => 10,
            'query' => [
                'multi_match' => [
                    'match' => [
                        'file.name' => $searchTerm,
                        'relation.machine' => $machineNumber,
                    ],
                ],
            ],
        ],
   ];

不幸的是,它不起作用-它总是返回0个结果


因此,搜索已修复,但我现在有正确搜索的问题。 file.name调用“ Anleitung_100_eng.Bedienung.pdf”-如果我搜索“关键字” Anleitung或Bedienung,他们一无所获..但是当我仅搜索“ t”时,他们发现...这是一个问题是我的查询还是来自索引?

$params = [
            'index' => 'search_' . $machineNumber . '_documentation',
            'type' => 'document',
            'size' => 500,
            'body' => [
                'query' => [
                    'prefix' => [
                        'file.name' => $searchTerm,
                        ],
                    ],
            ],
        ];

有什么想法吗?

索引映射:

$params = [
        'index' => $this->getIndex(),
        'body' => [

            'mappings' => [
                'document' => [

                    '_source' => [
                        'excludes' => [
                            'file_base64'
                        ]
                    ],

                    '_all' => [
                        'enabled' => false
                    ],

                    'properties' => [

                        'file' => [
                            'properties' => [
                                'name' => [
                                    'type' => 'text',
                                    'fields' => [
                                        'keyword' => [
                                            'type' => 'keyword',
                                            'ignore_above' => 256
                                        ]
                                    ]
                                ],

                                'extension' => [
                                    'type' => 'keyword'
                                ],

                                'created' => [
                                    'type' => 'date',
                                    'format' => 'yyy-MM-dd HH:mm:ss'
                                ],
                                'last_modified' => [
                                    'type' => 'date',
                                    'format' => 'yyy-MM-dd HH:mm:ss'
                                ],
                                'last_accessed' => [
                                    'type' => 'date',
                                    'format' => 'yyy-MM-dd HH:mm:ss'
                                ],

                                'path_folder' => [
                                    'type' => 'keyword'
                                ],
                                'path_file' => [
                                    'type' => 'keyword'
                                ],
                                'link_file' => [
                                    'type' => 'keyword'
                                ]
                            ]
                        ],

                        'article' => [
                            'properties' => [
                                'number' => [
                                    'type' => 'keyword'
                                ]
                            ]
                        ]
                    ]
                ],

                'meta' => [

                    '_all' => [
                        'enabled' => false
                    ],

                    'properties' => [

                        'last_modified' => [
                            'type' => 'date',
                            'format' => 'yyy-MM-dd HH:mm:ss'
                        ]
                    ]
                ]
            ],

            'settings' => [
                'number_of_shards' => 1,
                'number_of_replicas' => 0
            ]
        ]
    ];

ES:

> "mappings": {
>       "meta": {
>         "_all": {
>           "enabled": false
>         },
>         "properties": {
>           "last_modified": {
>             "type": "date",
>             "format": "yyy-MM-dd HH:mm:ss"
>           },
>           "update_date": {
>             "type": "text",
>             "fields": {
>               "keyword": {
>                 "type": "keyword",
>                 "ignore_above": 256
>               }
>             }
>           }
>         }
>       },
>       "document": {
>         "_all": {
>           "enabled": false
>         },
>         "_source": {
>           "excludes": [
>             "file.content_base64"
>           ]
>         },
>         "properties": {
>           "article": {
>             "properties": {
>               "number": {
>                 "type": "keyword"
>               }
>             }
>           },
>           "file": {
>             "properties": {
>               "content_base64": {
>                 "type": "text"
>               },
>               "create_date": {
>                 "type": "text",
>                 "fields": {
>                   "keyword": {
>                     "type": "keyword",
>                     "ignore_above": 256
>                   }
>                 }
>               },
>               "created": {
>                 "type": "date",
>                 "format": "yyy-MM-dd HH:mm:ss"
>               },
>               "extension": {
>                 "type": "keyword"
>               },
>               "last_accessed": {
>                 "type": "date",
>                 "format": "yyy-MM-dd HH:mm:ss"
>               },
>               "last_modified": {
>                 "type": "date",
>                 "format": "yyy-MM-dd HH:mm:ss"
>               },
>               "link_file": {
>                 "type": "keyword"
>               },
>               "link_folder": {
>                 "type": "text",
>                 "fields": {
>                   "keyword": {
>                     "type": "keyword",
>                     "ignore_above": 256
>                   }
>                 }
>               },
>               "name": {
>                 "type": "text",
>                 "fields": {
>                   "decompound": {
>                     "type": "text",
>                     "analyzer": "my_decompound"
>                   },
>                   "keyword": {
>                     "type": "keyword",
>                     "ignore_above": 256
>                   },
>                   "simple": {
>                     "type": "text",
>                     "analyzer": "simple"
>                   }
>                 }
>               },
>               "path_file": {
>                 "type": "keyword"
>               },
>               "path_folder": {
>                 "type": "keyword"
>               },
>               "path_folder_short": {
>                 "type": "keyword"
>               },
>               "permissions": {
>                 "type": "long"
>               },
>               "size": {
>                 "type": "long"
>               },
>               "version": {
>                 "type": "text",
>                 "fields": {
>                   "keyword": {
>                     "type": "keyword",
>                     "ignore_above": 256
>                   }
>                 }
>               }
>             }
>           },
>           "relation": {
>             "properties": {
>               "machine": {
>                 "type": "text",
>                 "fields": {
>                   "keyword": {
>                     "type": "keyword",
>                     "ignore_above": 256
>                   }
>                 }
>               },
>               "plant": {
>                 "type": "text",
>                 "fields": {
>                   "keyword": {
>                     "type": "keyword",
>                     "ignore_above": 256
>                   }
>                 }
>               },
>               "type": {
>                 "type": "text",
>                 "fields": {
>                   "keyword": {
>                     "type": "keyword",
>                     "ignore_above": 256
>                   }
>                 }
>               }
>             }
>           }
>         }
>       }
>     }

1 个答案:

答案 0 :(得分:2)

您需要两个match查询:

$params = [
    'index' => 'search_documentation',
    'type' => 'document',
    'body' => [
        'size' => 10,
        'query' => [
            'bool' => [
                'must' => [
                  [ 'match' => [
                    'file.name' => $searchTerm
                  ]],
                  [ 'match' => [
                    'relation.machine' => $machineNumber
                  ]]
                ]
            ],
        ],
    ],
];