Solr.NET使用示例

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值