关闭

Solr.NET使用示例

标签: netsolr
163人阅读 评论(0) 收藏 举报
分类:

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
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

指尖上的电商---(4).net开发solr

这一节我们看下怎样把查询数据放到服务器端存储,这里我们需要使用客户端工具来操作与服务端数据打交道,网上有好多基于.NET开发的SOLR客户端,我们这里选择easynet.solr,很方便的一个DLL,...
  • zx13525079024
  • zx13525079024
  • 2014-05-05 11:05
  • 6035

log4j 的简单示例

在项目的开发过程中,往往需要使用日志的功能,不仅便于调试,更是为了以后问题的排查。本文是一个简单的log4j的示例。
  • strongyoung88
  • strongyoung88
  • 2017-01-19 16:10
  • 607

Solr.NET快速入门(三)【Faceting】

分面搜索SolrNet支持分面搜索。基本上有三种方面查询: 按字段查询 日期分面 任意分面查询 分面查询通过QueryOptions的FacetQueries属性发出。 然后QueryOption...
  • WuLex
  • WuLex
  • 2017-03-18 21:37
  • 403

Solr.NET快速入门(二)【查询】

简单查询这是SolrNet中最简单的“查询对象”。 无论你给它什么都直接传递给Solr的q参数ISolrOperations solr = ... var products1 = solr.Query...
  • WuLex
  • WuLex
  • 2017-03-18 21:21
  • 1247

BCGSoft Demo示例展示之一般示例集合(1/2)

BCGSoft公司的BCGControlBar产品是全球最优秀的MFC界面类库,功能强大,显示丰富,在国际上享有盛誉,并屡次获奖,是VC界面设计的必备首选! 本文中的这些示例程序主要演示了如何创建先进...
  • AABBbaby
  • AABBbaby
  • 2016-04-25 10:08
  • 573

Solr.NET快速入门(六)【聚合统计,分组查询】

聚合统计 属性 说明 Min 最小值 Max 最大值 Sum 总和 Count 记录数,也就是多少行记录 Missing 结果集中,有多少条记录是空值 ...
  • WuLex
  • WuLex
  • 2017-03-18 22:22
  • 816

CKEditor的使用示例

CKEditor是一个专门使用在网页上属于开放源代码的所见即所得文字编辑器。以前叫FCKEditor。 1、到官网下载CKEditor:http://ckeditor.com/download  ,...
  • lin_yongrui
  • lin_yongrui
  • 2013-02-20 16:30
  • 2938

curl详解常用示例

curl 简介 命令参数 常用示例curl 简介curl是一个利用URL规则在命令行下工作的文件传输工具。它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称url为下载工具。命令参数 ...
  • chen8238065
  • chen8238065
  • 2016-11-23 13:50
  • 1739

C/C++ 开源库及示例代码

C/C++ 开源库及示例代码= == 说明 =本页面汇总俺收集的各种 C 和 C++ 的开源代码库,不定期更新。如果你发现本页面的开源库有错漏之处,非常欢迎给俺提供反馈——有 GitHub 帐号的同学...
  • x356982611
  • x356982611
  • 2016-11-16 16:15
  • 5018

线程池ThreadPoolExecutor使用简介

一、简介  线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为:  ThreadPoolExecutor(int corePoolSiz...
  • qq_26562641
  • qq_26562641
  • 2017-02-15 10:28
  • 213
    个人资料
    • 访问:1196494次
    • 积分:17412
    • 等级:
    • 排名:第643名
    • 原创:312篇
    • 转载:852篇
    • 译文:123篇
    • 评论:290条
    博客专栏
    文章分类
    打赏
    如果你觉得我的文章对您有用,请随意打赏。 微信 支付宝