简体   繁体   中英

PHP Elasticsearch search on two fields

I want to search in two fields. One field is passed by PHP and the other is entered by the user in the input field.

Can someone help me here?

my code:

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

Unfortunately, it does not work - it always returns 0 results


so the search is fixed but i have now the problem to search correctly. file.name is calling "Anleitung_100_eng.Bedienung.pdf" - if i search for the "keyword" Anleitung or Bedienung they found nothing.. but when i search for example only for "t" then they found that... is this a problem on my query or from the indexing?

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

any ideas?

Index mapping:

$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
>                   }
>                 }
>               }
>             }
>           }
>         }
>       }
>     }

You need two match queries:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM