1 Star 0 Fork 40

user_1411900/Masuit.LuceneEFCore.SearchEngine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test 2.84 KB
一键复制 编辑 原始数据 按行查看 历史
user_1411900 提交于 2021-05-27 17:32 . add test
using Masuit.LuceneEFCore.SearchEngine;
using Masuit.LuceneEFCore.SearchEngine.Extensions;
using Masuit.LuceneEFCore.SearchEngine.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
namespace Test
{
class Program
{
static ServiceProvider provide = null;
static void Main(string[] args)
{
Ini();
var _searchEngine = provide.GetService<ISearchEngine<DataContext>>();
// search
{
var options = new SearchOptions("思杰", 1, 10, typeof(Post));
var result = _searchEngine.ScoredSearch<Post>(options);
}
// create index
{
_searchEngine.CreateIndex(); // create full
_searchEngine.CreateIndex(new List<string> { nameof(Post) });
}
// add index
{
_searchEngine.Context.Post.Add(new Post { });
_searchEngine.SaveChanges();
}
// delete index
{
//从数据库删除并更新索引库
Post p = _searchEngine.Context.Post.Find(1);
_searchEngine.Context.Post.Remove(p);
_searchEngine.SaveChanges();
}
// update index
{
//从数据库删除并更新索引库
Post p = _searchEngine.Context.Post.Find(1);
_searchEngine.Context.Post.Update(p);
_searchEngine.SaveChanges();
}
Console.WriteLine("Hello World!");
}
static void Ini()
{
RegService();
var context = provide.GetService<DataContext>();
var searchEngine = provide.GetService<ISearchEngine<DataContext>>();
// 写入测试数据
context.Post.AddRange(
JsonConvert.DeserializeObject<List<Post>>(
File.ReadAllText(AppContext.BaseDirectory + "Posts.json")));
context.SaveChanges();
// 创建索引
searchEngine.CreateIndex(new List<string>()
{
nameof(Post)
});
}
static void RegService()
{
IServiceCollection services = new ServiceCollection();
LuceneIndexerOptions luceneIndexerOptions = new LuceneIndexerOptions()
{
Path = "lucene"
};
services.AddDbContext<DataContext>(db => db.UseInMemoryDatabase("test"));
services.AddSearchEngine<DataContext>(luceneIndexerOptions);
provide = services.BuildServiceProvider();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/magey/Masuit.LuceneEFCore.SearchEngine.git
[email protected]:magey/Masuit.LuceneEFCore.SearchEngine.git
magey
Masuit.LuceneEFCore.SearchEngine
Masuit.LuceneEFCore.SearchEngine
master

搜索帮助