summaryrefslogtreecommitdiff
path: root/RhSolutions.Api/Controllers/SearchController.cs
blob: 92d93d0938f1a70b236740f3ca5a8b6768723d09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RhSolutions.Models;

namespace RhSolutions.Api.Controllers
{
    [Route("api/[controller]")]
    public class SearchController : ControllerBase
    {
        private RhSolutionsContext context;

        public SearchController(RhSolutionsContext context)
        {
            this.context = context;
        }

        [HttpGet]
        public IAsyncEnumerable<Product> SearchProducts([FromQuery] string query)
        {
            return context.Products
                .Where(p => EF.Functions.ToTsVector(
                    "russian", string.Join(' ', new[] { p.Name, string.Join(' ', p.ProductLines)}))
                    .Matches(EF.Functions.WebSearchToTsQuery("russian", query)))
                .AsAsyncEnumerable();
        }
    }
}