我在Laravel项目中有搜索过滤器,可以很好地工作。现在,我正在尝试使用Elasticsearch 7.3.0使该搜索更高级。我在这方面还很陌生,需要一些有关如何更改控制器中当前查询以在Elasticsearch中工作的指南。我看了一些教程,但是无法在我的项目中解决。我下载并初始化了elasticsearch 7.3.0,然后在localhost:9200上运行它,它表明它可以工作,我也使用composer下载了软件包“ elasticsearch / elasticsearch”:“ ^ 7.2”。另外,我认为我需要让Elasticsearch在我的localhost项目上工作,而不是在localhost:9200上工作,因此,如果有人知道如何进行这项工作,那将是很棒的。感谢您提供任何帮助,或者仅提供一些类似的示例并提供指导,以使我能够正常工作。提前致谢。这是我的代码。
CategoryController.php
<?php
namespace App\Http\Controllers;
use App\Property;
use Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
class CategoryController extends Controller
{
public function search()
{
if (Auth::check()) {
$query = Property::query();
//PROPERTY TYPE!!!
if (Request::has('propertyType')) {
Request::get('propertyType');
}
$propertyType = Request::input('propertyType');
if (!empty($propertyType)) {
$query->whereHas('category', function ($query) use ($propertyType) {
$query->whereIn('category', $propertyType);
});
}
// CITY!!!
if (Request::has('city')) {
Request::get('city');
}
if (Request::input('city')) {
$query->where('city', Request::input('city'));
}
// PRICE
if (Request::has('min_price')) {
Request::get('min_price');
}
if (Request::has('max_price')) {
Request::get('max_price');
}
if (Request::input('min_price') && Request::input('max_price')) {
$query->whereBetween('price', [Request::input('min_price'), Request::input('max_price')]);
} elseif (Request::input('min_price')) {
$query->where('price', '>=', Request::input('min_price'));
} elseif (Request::input('max_price')) {
$query->where('price', '<=', Request::input('max_price'));
}
// SEARCHES
$results = $query->paginate(6, ['*'], 'searchResults');
}
return view('startpage', compact('results', 'request', 'propertyType'));
}
}
startpage.blade.php
<form id="searchForm" method="GET" action="/search">
<div class="row">
<div class="col-md-12 mb-3">
<h5 class="text-center">City</h5>
<input name="city" list="result" id="input" class="form-control" value="{{ isset($request->city) ? $request->city : old('city') }}">
<datalist id="result"></datalist>
</div>
</div>
<div class="row">
<div class="col-md-3 mb-6">
<label>Price</label>
<input type="number" id="min_price" name="min_price" class="form-control" placeholder="Min Price" value="{{ isset($request->min_price) ? $request->min_price : old('min_price') }}">
<input type="number" id="max_price" name="max_price" class="form-control" placeholder="Max Price" value="{{ isset($request->max_price) ? $request->max_price : old('max_price') }}">
</div>
<div class="col-md-2 mb-6">
<h5>Property type</h4>
<div class="d-block my-3 ">
<div class="custom-control custom-checkbox">
<input id="house" name="propertyType[]" value="house" type="checkbox" class="custom-control-input" @if (!empty($propertyType) && in_array('house', $propertyType)) checked="checked" @endif>
<label class="custom-control-label" for="house">house</label>
</div>
<div class="custom-control custom-checkbox">
<input id="flat" name="propertyType[]" value="flat" type="checkbox" class="custom-control-input" @if (!empty($propertyType) && in_array('flat', $propertyType)) checked="checked" @endif>
<label class="custom-control-label" for="flat">flat</label>
</div>
<div class="custom-control custom-checkbox">
<input id="room" name="propertyType[]" value="room" type="checkbox" class="custom-control-input" @if (!empty($propertyType) && in_array('room', $propertyType)) checked="checked" @endif>
<label class="custom-control-label" for="room">room</label>
</div>
</div>
</div>
</div>
<div class="row justify-content-md-center">
<div class="col-md-2 mb-6">
<button class="btn btn-primary btn-lg btn-block" type="submit">Search</button>
</div>
</div>
<hr class="mb-4">
</form>