Solr.NET使用示例
来源:互联网 发布:sql语句select语法 编辑:程序博客网 时间:2024/05/16 09:35
SolrPolicyEntity.cs实体类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using SolrNet.Attributes;namespace SolrDemo{ public class SolrPolicyEntity { [SolrUniqueKey("PolicyID")] public long PolicyID { get; set; } [SolrField("PolicyGroupID")] public long PolicyGroupID { get; set; } [SolrField("PolicyOperatorID")] public long PolicyOperatorID { get; set; } [SolrField("PolicyOperatorName")] public string PolicyOperatorName { get; set; } [SolrField("PolicyCode")] public string PolicyCode { get; set; } [SolrField("PolicyName")] public string PolicyName { get; set; } [SolrField("PolicyType")] public string PolicyType { get; set; } [SolrField("TicketType")] public int TicketType { get; set; } [SolrField("FlightType")] public int FlightType { get; set; } [SolrField("DepartureDate")] public DateTime DepartureDate { get; set; } [SolrField("ArrivalDate")] public DateTime ArrivalDate { get; set; } [SolrField("ReturnDepartureDate")] public DateTime ReturnDepartureDate { get; set; } [SolrField("ReturnArrivalDate")] public DateTime ReturnArrivalDate { get; set; } [SolrField("DepartureCityCodes")] public string DepartureCityCodes { get; set; } [SolrField("TransitCityCodes")] public string TransitCityCodes { get; set; } [SolrField("ArrivalCityCodes")] public string ArrivalCityCodes { get; set; } [SolrField("OutTicketType")] public int OutTicketType { get; set; } [SolrField("OutTicketStart")] public DateTime OutTicketStart { get; set; } [SolrField("OutTicketEnd")] public DateTime OutTicketEnd { get; set; } [SolrField("OutTicketPreDays")] public int OutTicketPreDays { get; set; } [SolrField("Remark")] public string Remark { get; set; } [SolrField("Status")] public int Status { get; set; } [SolrField("SolrUpdatedTime")] public DateTime SolrUpdatedTime { get; set; } }}
SolrNetApi.cs
using System;using System.Collections.Generic;using Microsoft.Practices.ServiceLocation;using SolrNet;using SolrNet.Commands.Parameters;using SolrNet.Impl;namespace SolrDemo{ internal class SolrNetApi { private static void Main(string[] args) { Startup.Init<SolrPolicyEntity>("http://localhost:8983/solr/PolicyCore"); Delete(); Add(); Query(); //FullDataImport(); //DeltaDataImport(); Console.WriteLine("操作已经完成,按任意键退出。"); Console.ReadKey(); } //删 public static void Delete() { var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrPolicyEntity>>(); solr.Delete(SolrQuery.All); solr.Commit(); } //增,改, 准实时数据导入 public static void Add() { Random random = new Random((int)DateTime.Now.Ticks & 0x0000FFFF); var policyID = random.Next(10); var p = new SolrPolicyEntity { PolicyID = policyID, PolicyGroupID = 14, PolicyOperatorID = 55, PolicyOperatorName = "研发", PolicyCode = "23", PolicyName = string.Format("{0}_b中国南通科技大促销政策a", policyID), PolicyType = "OW/RT", TicketType = 1, FlightType = 10, DepartureDate = new DateTime(2017, 6, 8), ArrivalDate = new DateTime(2017, 9, 8), ReturnDepartureDate = new DateTime(2017, 6, 8), ReturnArrivalDate = new DateTime(2017, 9, 8), DepartureCityCodes = "/CN/", //TransitCityCodes = "", ArrivalCityCodes = "/UG/CV/GM/CG/GN/GH/GA/KE/LY/MU/MR/EG/MG/ML/SD/SO/MA/ZA/NE/NG/SN/ET/ER/AO/DZ/CM/TD/GQ/TZ/TN/CF/GW/CD/RW/SL/KM/LR/RE/TG/SC/CI/MZ/BF/BI/MW/ZW/BJ/D", OutTicketType = 0, OutTicketStart = new DateTime(2017, 6, 8), OutTicketEnd = new DateTime(2017, 9, 8), OutTicketPreDays = 0, Remark = "中国南通科技始发政策内部备注", Status = 2, SolrUpdatedTime = DateTime.Now.AddHours(-1) }; var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrPolicyEntity>>(); solr.Add(p); solr.Commit(); } //查 public static void Query() { var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrPolicyEntity>>(); SolrQueryResults<SolrPolicyEntity> results = null; int queryType = 14; var queryOptions = new QueryOptions { StartOrCursor = new StartOrCursor.Start(0), Rows = 20 }; switch (queryType) { case 1: //简单查询(模糊查询) results = solr.Query(new SolrQuery("PolicyName:\"大促销\""), queryOptions); break; case 3: //范围查询 results = solr.Query( new SolrQueryByRange<DateTime>("DepartureDate", new DateTime(2017, 4, 8), new DateTime(2017, 4, 25)), queryOptions); break; case 4: //多值查询(模糊查询) results = solr.Query(new SolrQueryInList("PolicyName", "国内始发", "川航前1普发"), queryOptions); break; case 5: //任意值查询 results = solr.Query(new SolrHasValueQuery("Remark"), queryOptions); break; case 6: //组合查询 results = solr.Query(new SolrQuery("PolicyName:\"大促销\"") && new SolrQuery("FlightType:10"), queryOptions); break; case 7: //组合查询 results = solr.Query(new SolrQuery("PolicyName:\"普发\"") || new SolrQuery("FlightType:1"), queryOptions); break; case 8: //组合查询 results = solr.Query(new SolrQuery("PolicyName:\"大促销\"") + new SolrQuery("FlightType:1"), queryOptions); break; case 9: //组合查询 results = solr.Query(new SolrQuery("TicketType:1") - new SolrQuery("PolicyName:\"大促销\""), queryOptions); break; case 10: //组合查询 results = solr.Query(new SolrQuery("TicketType:1") + !new SolrQuery("PolicyName:\"大促销\""), queryOptions); break; case 11: //过滤查询 results = solr.Query(SolrQuery.All, new QueryOptions { FilterQueries = new ISolrQuery[] { new SolrQuery("FlightType:10"), new SolrQueryByRange<DateTime>("DepartureDate", new DateTime(2017, 6, 8), new DateTime(2017, 6, 8)) }, StartOrCursor = new StartOrCursor.Start(0), Rows = 20 }); break; case 2: //字段查询 SolrQueryByField case 12: //过滤查询 results = solr.Query(SolrQuery.All, new QueryOptions { FilterQueries = new QueryOptions().AddFilterQueries( new SolrQueryByField("PolicyName", "大促销"), //new SolrQueryByRange<DateTime>("DepartureDate", new DateTime(2017, 4, 8), new DateTime(2017, 6, 7)) new SolrQueryByRange<DateTime>("DepartureDate", new DateTime(2017, 4, 8), new DateTime(2017, 6, 8)) ).FilterQueries, StartOrCursor = new StartOrCursor.Start(0), Rows = 20 }); break; case 13: //返回字段 results = solr.Query(SolrQuery.All, new QueryOptions { Fields = new[] { "PolicyName", "PolicyGroupID", "PolicyType", "TicketType", "FlightType", "DepartureDate", "Remark", "SolrUpdatedTime" }, StartOrCursor = new StartOrCursor.Start(0), Rows = 20 }); break; case 14: //排序 results = solr.Query(SolrQuery.All, new QueryOptions { OrderBy = new[] { new SortOrder("PolicyID", Order.DESC), SortOrder.Parse("SolrUpdatedTime ASC") }, StartOrCursor = new StartOrCursor.Start(0), Rows = 20 }); break; case 15: //分页:查询第2页的结果 results = solr.Query(SolrQuery.All, new QueryOptions { OrderBy = new[] { new SortOrder("PolicyID", Order.DESC) }, StartOrCursor = new StartOrCursor.Start(1), Rows = 1 }); break; default: //综合示例 results = solr.Query(SolrQuery.All, new QueryOptions { FilterQueries = new QueryOptions().AddFilterQueries( new SolrQueryByField("PolicyName", "大促销"), new SolrQueryByRange<DateTime>("DepartureDate", new DateTime(2017, 4, 8), new DateTime(2017, 6, 8)) ).FilterQueries, Fields = new[] { "PolicyName", "PolicyGroupID", "PolicyType", "TicketType", "FlightType", "DepartureDate", "Remark", "SolrUpdatedTime" }, OrderBy = new[] { new SortOrder("PolicyID", Order.DESC), SortOrder.Parse("SolrUpdatedTime ASC") }, StartOrCursor = new StartOrCursor.Start(0), Rows = 20 }); break; } Console.WriteLine("查询结果:\n"); foreach (SolrPolicyEntity i in results) { Console.WriteLine("PolicyID:{0},PolicyName:{1},PolicyGroupID:{2},PolicyType:{3},TicketType:{4},FlightType:{5},DepartureDate:{6},Remark:{7},Solr更新时间:{8} \n ", i.PolicyID, i.PolicyName, i.PolicyGroupID, i.PolicyType, i.TicketType, i.FlightType, i.DepartureDate, i.Remark, i.SolrUpdatedTime); } } //全量数据导入 public static void FullDataImport() { var conn = new SolrConnection("http://localhost:8983/solr/PolicyCore"); string relativeUrl = "/dataimport"; var parameters = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("command", "full-import"), new KeyValuePair<string, string>("clean", "true"), new KeyValuePair<string, string>("commit", "true") }; string result = conn.Get(relativeUrl, parameters); Console.WriteLine("结果:{0}", result); } //增量数据导入 public static void DeltaDataImport() { var conn = new SolrConnection("http://localhost:8983/solr/PolicyCore"); string relativeUrl = "/dataimport"; var parameters = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("command", "delta-import"), new KeyValuePair<string, string>("clean", "false"), new KeyValuePair<string, string>("commit", "true") }; string result = conn.Get(relativeUrl, parameters); Console.WriteLine("结果:{0}", result); } }}
部分配置文件
D:\WorkSoftware\solr-6.4.2\server\solr\PolicyCore\conf\managed-schema
<fieldType name="tfloat" class="solr.TrieFloatField" positionIncrementGap="0" docValues="true" precisionStep="8"/> <fieldType name="tfloats" class="solr.TrieFloatField" positionIncrementGap="0" docValues="true" multiValued="true" precisionStep="8"/> <fieldType name="tint" class="solr.TrieIntField" positionIncrementGap="0" docValues="true" precisionStep="8"/> <fieldType name="tints" class="solr.TrieIntField" positionIncrementGap="0" docValues="true" multiValued="true" precisionStep="8"/> <fieldType name="tlong" class="solr.TrieLongField" positionIncrementGap="0" docValues="true" precisionStep="8"/> <fieldType name="tlongs" class="solr.TrieLongField" positionIncrementGap="0" docValues="true" multiValued="true" precisionStep="8"/> <field name="ArrivalCityCodes" type="strings" multiValued="false"/> <field name="ArrivalDate" type="tdates" multiValued="false"/> <field name="DepartureCityCodes" type="strings" multiValued="false"/> <field name="DepartureDate" type="tdates" multiValued="false"/> <field name="FlightType" type="tlongs" multiValued="false"/> <field name="OutTicketEnd" type="tdates" multiValued="false"/> <field name="OutTicketPreDays" type="tlongs" multiValued="false"/> <field name="OutTicketStart" type="tdates" multiValued="false"/> <field name="OutTicketType" type="tlongs" multiValued="false"/> <field name="PolicyCode" type="tlongs" multiValued="false"/> <field name="PolicyGroupID" type="tlongs" multiValued="false"/> <field name="PolicyID" indexed="true" type="tlongs" multiValued="false"/> <field name="PolicyName" type="strings" multiValued="false"/> <field name="PolicyOperatorID" type="tlongs" multiValued="false"/> <field name="PolicyOperatorName" type="strings" multiValued="false"/> <field name="PolicyType" type="strings" multiValued="false"/> <field name="Remark" type="strings" multiValued="false"/> <field name="ReturnArrivalDate" type="tdates" multiValued="false"/> <field name="ReturnDepartureDate" type="tdates" multiValued="false"/> <field name="SolrUpdatedTime" type="tdates" multiValued="false"/> <field name="Status" type="tlongs" multiValued="false"/> <field name="TicketType" type="tlongs" multiValued="false"/> <field name="_root_" type="string" docValues="false" indexed="true" stored="false"/> <field name="_text_" type="text_general" multiValued="true" indexed="true" stored="false"/> <field name="_version_" type="long" indexed="false" stored="false"/> <field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
运行结果如图:
阅读全文
0 0
- Solr.NET使用示例
- Solr安装及使用示例
- 使用.net调用Solr
- Solr示例
- FastReport.NET使用示例
- Solr 使用入门介绍,以搜索论坛帖子为示例
- Solr JAVA客户端SolrJ 4.9使用示例教程
- Solr JAVA客户端SolrJ 4.9使用示例教程
- solr示例程序
- Solr调用示例
- Solr查询语法示例
- Solr自动完成示例
- Solr突出显示示例
- Apache Solr复制示例
- Apache Solr同义词示例
- Solr过滤器查询示例
- Solr Schema.xml示例
- Solr Dataimporthandler示例
- 学习笔记TF055:TensorFlow神经网络简单实现一元二次函数
- 代码签名证书——辨别真假软件的天眼
- http协议-----待学
- HTTP协议与HTTP表单传输格式,再论get、post方法
- 深度学习Tensorflow视频推荐
- Solr.NET使用示例
- unix时间戳和普通时间戳转换
- 将map和泛型为map的list集合插入到指定的数据库表中
- TAG标签引用
- spring学习-spring jdbc 连接池的配置(一)
- leetcode题解-65. Valid Number
- XPath总结
- 33岁才接触博客是不是已经out了?
- vmware workstation14永久激活密钥分享