question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

optional text search and filter

See original GitHub issue

@dj-nitehawk thank you for this great library.

I need little help.

I can apply a text search filter and field filter like this without no problem:

        [HttpGet("search-filter")]
        public async Task<IActionResult> SearchAndFilter(FilterProductDTO filter)
        {
            var products = await DB.Find<ProductEntity>()
                    .Match(Search.Full, filter.SearchTerm)
                    .ManyAsync(a => a.Category == filter.CategoryId);

            return Ok(products);
        }      
    public class FilterProductDTO
    {
        public string CategoryId { get; set; }
        public string SearchTerm { get; set; }
        public decimal? MinPrice { get; set; }
        public decimal? MaxPrice { get; set; }
        public bool? IsInStock { get; set; }
    }

But I want to apply these filters optionally, only if they are not null or empty I want to apply.

I guess this can be accomplished with linq IQueryable features. But is it possible to apply text search in linq?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
susahin80commented, Apr 11, 2021

Your last example worked perfectly, thank you very much.

1reaction
dj-nitehawkcommented, Apr 11, 2021

actually, this can also be done in an easier way with the find interface like so:

var query = DB.Find<Product, Product>();

if (!string.IsNullOrEmpty(dto.SearchTerm))
    query.Match(Search.Full, dto.SearchTerm);

if (dto.MinPrice.HasValue)
    query.Match(p => p.Price <= dto.MinPrice.Value);

if (!string.IsNullOrEmpty(dto.SearchTerm))
    query.SortByTextScore();

var result = await query.ExecuteAsync();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Find - $text search with $or + other optional values
I can't get to query multiple optional values on several fields. ... I already know that $text search can't be used with $or....
Read more >
Using optional filters
Learn how to use optional filters to modify your custom ranking. ... They don't just remove records that don't match the filter values....
Read more >
How can I build a Dashboard/Search to use optional...
I am trying to build a dashboard with multiple Text inputs that are ... By default the search filters have a logic AND...
Read more >
Optional 'free text' search in a primarily category based ...
Our search UI allows users to search across multiple categories: Day, Type, Area, etc. Each selected criteria is ANDed. We also allow a...
Read more >
How to make filters in queries optional
Take a look at the Orchard.Projections module. That's where you'll find many of the default query filters (including the date field filter you ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found