关闭

Solr.NET使用示例

标签: netsolr
216人阅读 评论(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
查看评论

Solr.NET快速入门(九)【多核多实例,映射验证】

多核/多实例本页介绍如何配置SolrNet访问(读/写)多个Solr内核或实例。 它假定您知道Solr内核是什么,如何在SolrNet外部配置和使用它们。 此页面不涵盖CoreAdminHandler命令。如何配置SolrNet for multicore取决于它如何集成到您的应用程序,如果您的内核...
  • WuLex
  • WuLex
  • 2017-03-18 23:06
  • 668

使用.net调用Solr

1. using System; using System.Collections.Generic; using System.Linq; using System.Web; using SolrNet; using SampleSolrApp.Models; using SolrNe...
  • tianyitianyi1
  • tianyitianyi1
  • 2013-04-25 23:06
  • 2189

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

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

solrnet 查询

public ShowSelectVo SearchVos(string value, Guid? siteId, string areaName)         {             v...
  • ksmldd
  • ksmldd
  • 2017-09-12 17:25
  • 109

Solrnet教程

Solrnet基本用法http://code.google.com/p/solrnet/ 一、开始我们必须生成一个类来映射Solr的Field,下面是Solr默认schema的映射:public class Product {    [SolrUniqueKey(&qu...
  • superyoungchaos
  • superyoungchaos
  • 2010-01-04 14:38
  • 8098

SolrNet Source

  • 2012-10-20 01:44
  • 24.56MB
  • 下载

solrNet使用

  • 2014-06-26 20:07
  • 44.23MB
  • 下载

Solr学习总结(五)SolrNet的基本用法及CURD

上一篇已经讲到了Solr 查询的相关的参数。这里在讲讲C#是如何通过客户端请求和接受solr服务器的数据, 这里推荐使用SolrNet,主要是:SolrNet使用非常方便,而且用户众多,一直都在更新,感兴趣的可以加入他们的邮件群组,方便迅速了解 SolrNet的最新动态。     Sor...
  • gangzhucoll
  • gangzhucoll
  • 2016-08-10 16:31
  • 293

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

聚合统计 属性 说明 Min 最小值 Max 最大值 Sum 总和 Count 记录数,也就是多少行记录 Missing 结果集中,有多少条记录是空值 SumOfSquares 平方和(x1^2 + x2^2+xn^2) Mean 平均数...
  • WuLex
  • WuLex
  • 2017-03-18 22:22
  • 898

Solr.NET快速入门(一)【基本使用,映射字段】

字典映射和动态字段Solr dynamicFields可以根据用例不同地映射。 它们可以被“静态地”映射,例如,给定:<dynamicField name="price_*" type="integer" indexed="true&quo...
  • WuLex
  • WuLex
  • 2017-03-18 17:33
  • 913
    个人资料
    • 访问:1297999次
    • 积分:18625
    • 等级:
    • 排名:第591名
    • 原创:312篇
    • 转载:932篇
    • 译文:123篇
    • 评论:297条
    博客专栏
    文章分类
    打赏
    如果你觉得我的文章对您有用,请随意打赏。 微信 支付宝