简体   繁体   中英

How do you do fulltext search with Entity Framework Core?

I have the following query:

SELECT Animals.name 
FROM Animals 
WHERE CONTAINS(*, 'feline AND black');

I am having trouble converting it to an Entity Framework Core query. I have a SQL Server with a catalog that has a few indexes.

I want to be able to use FREETEXT and CONTAINS to do a fulltext query on the tables. I cannot find the method in Entity Framework Core for fulltext search with CONTAINS .

This is possible as of EF Core 2.1. You have to add a using statement for Microsoft.EntityFrameworkCore but after that you can use it as shown below

var results = db.Widgets
    .Where(x => EF.Functions.FreeText(x.ColumnName, "search text"));

Yes it is posible using EF 6, just use EF.Functions.Contains

var results = db.Widgets
    .Where(x => EF.Functions.Contains(x.ColumnName, "search text"));

Important "search text" do not allow blank spaces you have to separate each word using logic operators like AND OR, for example if you want to search records with "green" and "blue", you have to put "green AND blue", not "green blue"

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