//本文不是完整的一个示例,写的不够详细完整,不适合还没入门的人看,
//需要添加nest.dll相关包
官网:https://www.elastic.co/cn/products
中文社区:https://elasticsearch.cn/
Chroem浏览器中有个可视化插件ElasticSearch Head
//模型
public class TestModel
{
public string TagCode { get; set; }
public string PN1 { get; set; }
public string PN2 { get; set; }
public string Tag { get; set; }
public string TagPYSX { get; set; }
public string TagPY { get; set; }
public int SNum { get; set; }
public int RNum { get; set; }
public int TNum { get; set; }
public string IsShow { get; set; }
public DateTime LastTime { get; set; }
public int OrderBy { get; set; }
}
//模型可加属性
[ElasticsearchType(Name= "CentaSearchTagMainModel")]
public class CentaSearchTagMainModel
{
[Keyword(Name="tagcode",DocValues =false)]
public string TagCode { get; set; }
//[Keyword(Name = "pn1", DocValues = false)]
public string PN1 { get; set; }
[Keyword(Name = "pn2", DocValues = false)]
public string PN2 { get; set; }
[Text(Name="tag",Analyzer ="ik_max_word")]
public string Tag { get; set; }
[Keyword(Name = "tagcommon", DocValues = false)]
public string TagCommon { get; set; }
[Keyword(Name = "tchinese", DocValues = false)]
public string TChinese { get; set; }
[Keyword(Name = "tagPYSX", DocValues = false)]
public string TagPYSX { get; set; }
[Keyword(Name = "tagPY", DocValues = false)]
public string TagPY { get; set; }
[Keyword(Name = "TagCategory", DocValues = false)]
public string TagCategory { get; set; }
[Number(NumberType.Integer,Name = "SNum")]
public int SNum { get; set; }
[Number(NumberType.Integer, Name = "RNum")]
public int RNum { get; set; }
// [Number(NumberType.Integer, Name = "TNum")]
public int TNum { get; set; }
[Keyword(Name = "IsShow", DocValues = false)]
public string IsShow { get; set; }
[Keyword(Name = "LastTime", DocValues = false)]
public string LastTime { get; set; }
[Number(NumberType.Integer,Name ="orderby")]
public int OrderBy { get; set; }
}
[WebMethod]
public static List<TestModel> SearchEsfAll(string searchName)
{
Uri uri = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(uri).DisableDirectStreaming();//.DefaultIndex("tagmain");
var client = new ElasticClient(settings);
//返回所有的文件,Fluent API表示查询
var searchResponse = client.Search <TestModel>(s => s.Query(q => q.MatchAll()).Index("testindex2").Type("testIndex2Type3"));
//searchResponse = client.Search <TestModel>(s => s .MatchAll()); //简写
var highlights = searchResponse.HitsMetaData.Hits.Select(h => h.Highlights);
//Object Initializer语法组合查询
//var searchRequest = new SearchRequest<TestModel>
//{ Query = new MatchAllQuery()};
//searchResponse = client.Search < TestModel >(searchRequest);
var searchResponse3 = client.Search<TestModel>(s => s.Query(q => q.DateRange(r => r.Field(f => f.LastTime)
.GreaterThanOrEquals(new DateTime(2017-01)).LessThan(new DateTime(2018-01))))
.Index("testindex2").Type("testIndex2Type3"));
//查找包含改字的文档,默认分词
var searchResponse4 = client.Search < TestModel >(s => s
.Query(q => q .Match(m => m .Field(f => f.Tag).Query("体馆"))).Index("testindex2").Type("testIndex2Type3"));
//匹配Tag包含南的文档,和PN2包含镇
var searchResponse5 = client.Search<TestModel>(s => s .Query(q => q .Bool(b => b.Must(mu => mu
.Match(m => m .Field(f => f.Tag).Query("南")), mu => mu.Match(m => m.Field(f => f.PN2)
.Query("镇") )))).Index("testindex2").Type("testIndex2Type3"));
//同上的简写,多个字默认分词器拆分后对比,如包含体馆or体育就匹配
var searchResponse6 = client.Search<TestModel>(s => s.Query(q => q.Match(m => m.Field(f => f.Tag)
.Query("馆育")) && q.Match(m => m.Field(f => f.Tag).Query("体")
)).Index("testindex2").Type("testIndex2Type3"));
//结构化搜索,是关于查询具有固有结构的数据
//二进制运算符
//包含东或包含体则匹配,不分词,只能输入一个字
var searchResults = client.Search<TestModel>(s => s.Query(q => q.Bool(b => b .Should
(bs => bs.Term(p => p.Tag, "东")
,bs => bs.Term(p => p.Tag, "体")
))).Index("testindex2").Type("testIndex2Type3"));
//同上。显示带和字or体字的文档
var firstSearchResponse = client.Search <TestModel>(s => s.Query(q =>
q.Term(p => p.Tag, "体")
|| q.Term(p => p.Tag, "和"))
.Index("testindex2").Type("testIndex2Type3"));
//包含体并且包含中心,只能输入一个字
var firstSearchResponse7 = client.Search<TestModel>(s => s.Query(q => q
.Term(p => p.Tag, "体") && q
.Term(p => p.Tag, "和"))
.Index("testindex2").Type("testIndex2Type3"));
//Object Initializer语法。同上
//一元运算符
//不包含和字
var firstSearchResponse8 = client.Search < TestModel >(s => s
.Query(q =>!q.Term(p => p.Tag, "和")).Index("testindex2").Type("testIndex2Type3"));
//指定返回单个字段
var searchResponse9= client.Search<TestModel>(s => s.StoredFields(sf => sf
.Fields(
f => f.Tag,
f => f.PN2 ))
.Query(q => q
.MatchAll()).Index("testindex2").Type("testIndex2Type3")
);
//包含或排除字段
var searchResponse10 = client.Search<TestModel>(s => s .Source(sf => sf .Includes(i => i.Fields(
f => f.Tag,
f => f.PN2
))
//.Excludes(e => e
//.Fields("PN*")
//)
)
.Query(q => q
.MatchAll()).Index("testindex2").Type("testIndex2Type3"));
return null;
//List<CentaSearchTagMainModel> tagList = new List<CentaSearchTagMainModel>();
//// Uri uri = new Uri("http://localhost:9200");
//// ConnectionSettings settings = new ConnectionSettings(uri);
////settings.DisableDirectStreaming();
//// ElasticClient client = new ElasticClient(settings);
////查询该索引该类的所有文档
//var searchResults11= client.Search<CentaSearchTagMainModel>(s => s.Index("tagmain").Size(10000));
////查询单个字
//var searchResults = client.Search<CentaSearchTagMainModel>(s => s.From(0).Query(q => q.Term(p => p.TagPYSX, "h")).Size(1000));
////查该类型的所有文档
//var searchResults1 = client.Search<CentaSearchTagMainModel>(s => s.AllIndices().From(0).Size(10000));
////查所有类型的文档
//var searchResults2 = client.Search<CentaSearchTagMainModel>(s => s.AllIndices().AllTypes().From(0).Size(10000));
//var result2 = client.Search<CentaSearchTagMainModel>(s => s.Query(q => q.MatchPhrase(m => m.Field(f => f.TagCode).Query("227"))).From(0).Size(15000));
////取得该type所有的文档
//var searchResults3 = client.Search<CentaSearchTagMainModel>(s => s.From(0).Size(10000));
////全文所有,匹配多个字段
//string keyword = String.Format("*{0}*", searchName);
//var tagList1 = client.Search<CentaSearchTagMainModel>(s => s
// .Index("tagmain")
// .Query(q => q.QueryString(qs => qs.Query(keyword).DefaultOperator(Operator.Or))).From(0).Size(1000));
//;
////搜拼音时需要全匹配,如万科城市花园=wkcshy
//var searchResults4 = client.Search<CentaSearchTagMainModel>
// (s =>s.Query(q => q.QueryString(m => m.Fields(fd => fd.Field(fdd => fdd.TagPYSX).Field(fdd => fdd.Tag)).Query(searchName))).From(0).Size(1500));//.Field(fdd => fdd.carGearBox).From(0).Size(15)
////模糊匹配,不分字。='%name%'
//var result3 = client.Search<CentaSearchTagMainModel>(s => s .Query(q => q.MatchPhrase(m => m.Field(f => f.Tag).Query("和"))).From(0) .Size(1500));
////分字匹配
//var searchResponse = client.Search<CentaSearchTagMainModel>
// (s => s.From(0).Size(100).Query(q => q.Match(m => m.Field(f => f.Tag).Query("*"+searchName+"*"))));
////根据id查询,精准匹配,不能分词包含等。
//var searchResults6 = client.Search<CentaSearchTagMainModel>(s => s.From(0) .Size(10).Query(q => q.Ids(r => r.Values("2270*"))));
////查询前10条,不能加数量
//var query = new Nest.SearchDescriptor<CentaSearchTagMainModel>();
//var result = client.Search<CentaSearchTagMainModel>(x => query);
////取得包含有科或和或平的,用分词器拆分的,单个字拆分,%科%,%和%,0%平%
//var v1 = client.Search <CentaSearchTagMainModel>
// (query.PostFilter(x => x.QueryString(t => t.Fields(f => f.Field(obj => obj.Tag)).Query("科和 平"))).From(0).Size(1000));
//tagList.AddRange(client.Search<CentaSearchTagMainModel>(s => s.Query(q => q.MatchPhrase(m => m.Field(f => f.Tag).Query("万"))).From(0).Size(11).Sort(d => d.Ascending(p => p.OrderBy).Descending(p => p.TNum))).Documents.ToList());
////tagList.AddRange(client.Search<CentaSearchTagMainModel>
//// (s => s.Query(q => q.QueryString(m => m.Fields(fd => fd.Field(fdd => fdd.Tag)).
//// Query("*" + searchNames + "*"))).From(0).Size(11).Sort(d => d.Ascending(p => p.OrderBy).Descending(p => p.TNum))).Documents.ToList());//.Field(fdd => fdd.carGearBox).From(0).Size(15)
// return null;//v1.Documents.ToList();
}
protected void Page_Load(object sender, EventArgs e)
{
Uri uri = new Uri("http://localhost:9200");
ConnectionSettings settings = new ConnectionSettings(uri);
settings.DisableDirectStreaming().DefaultIndex("customer");
ElasticClient client = new ElasticClient(settings);
//var node = new Uri("http://localhost:9200/");
//如果没有指定使用哪个index,ElasticSearch会直接使用我们在setting中的defaultIndex,如果没有,则会自动创建
//var settings = new ConnectionSettings(node).DefaultIndex("geopoint-tests11");
//var client = new ElasticClient(settings);
client.CreateIndex("geopoint-tests"); //添加索引
//dynamic response = client.GetIndex(indexName);
//client.CreateIndex("customer1");
//if (!response.IsValid)
//{
// response = client.CreateIndex("customer");
// Console.WriteLine(response);
//}
//创建文档并指定索引,和文档ID
// var company = new Company { CompanyID = 2, Name = "IBM" };
//response = client.Index(company, i => i.Index(indexName).Type("sss").Id(company.CompanyID));
//创建文档并指定索引,文档类型,未指定文档ID,ElasticSearch会帮你生成一个随机的ID
//var company1 = new Company { CompanyID = 2, Name = "IBM2" };
//response = client.Index(company, i => i.Index(indexName));
//这里没有显示指定索引,所以使用的客户端初始化默认索引,如果没有默认索引,就会发生错误
//var company2 = new Company { CompanyID = 1, Name = "IBM1" };
//response = client.Index(company);
// 修改文档,创建式修改比较合适
//company = new Company { CompanyID = 2, Name = "联想" };
//response = client.Index(company, i => i.Id(company.CompanyID));
//修改指定版本的文档
//company = new Company { CompanyID = 2, Name = "联想" };
//response = client.Index(company, i => i.Id(company.CompanyID).Version(1));
//根据特定的条件来删除文档
//response = client.Delete<Company>(company.CompanyID, d => d.Index(indexName));
//删除文档
//client.Delete<Company>(company.CompanyID);
//删除索引
//response = client.DeleteIndex(indexName);
//这里制定了要删除的索引名称,后一个表示删除的索引名
//var descriptor = new DeleteIndexDescriptor("geopoint-tests1").Index("geopoint-tests1");
//删除指定索引所在节点下的所有索引(,当前索引同级的所有索引)
//var descriptor = new DeleteIndexDescriptor("geopoint-tests1").AllIndices();
//这里是指定了相应的索引,如果没有显示指定索引,都是用的默认索引,
//批量创建文档,同时也创建了索引
BulkDescriptor descriptor = new BulkDescriptor();
// descriptor.Index<Company>(op => op.Document(new Company { CompanyID = 1, Name = "IBM" }));
//descriptor.Index<Company>(op => op.Document(new Company { CompanyID = 21221, Name = "IB1M" }).Index("b").Type("sss").Id("11"));
//descriptor.Index<Company>(op => op.Document(new Company { CompanyID = 3, Name = "IBM" }).Index("c"));
// response = client.DeleteIndex("c");
client.DeleteIndex("b");
client.Bulk(descriptor);
//response = client.DeleteIndex(indexName);
}
(NEST)c#中使用ElarticSearch的一些语句
最新推荐文章于 2022-11-03 14:23:23 发布