• 【转帖】Solr.Net的使用


    https://github.com/mausch/SolrNet/blob/master/Documentation/Basic-usage.md

    Basic usage

    First, we have to map the Solr document to a class. Let's use a subset of the default schema that comes with the Solr distribution:

    public class Product {
        [SolrUniqueKey("id")]
        public string Id { get; set; }
    
        [SolrField("manu_exact")]
        public string Manufacturer { get; set; }
    
        [SolrField("cat")]
        public ICollection<string> Categories { get; set; }
    
        [SolrField("price")]
        public decimal Price { get; set; }
    
        [SolrField("inStock")]
        public bool InStock { get; set; }
    }
    

    It's just a POCO with some attributes: SolrField maps the attribute to a Solr field and SolrUniqueKey (optional but recommended) maps an attribute to a Solr unique key field.

    Now we'll write some tests using this mapped class. Let's initialize the library:

    [TestFixtureSetUp]
    public void FixtureSetup() {
        Startup.Init<Product>("http://localhost:8983/solr");
    }
    

    Let's add a document (make sure you have a running Solr instance before running this test):

    [Test]
    public void Add() {
        var p = new Product {
            Id = "SP2514N",
            Manufacturer = "Samsung Electronics Co. Ltd.",
            Categories = new[] {
                "electronics",
                "hard drive",
            },
            Price = 92,
            InStock = true,
        };
    
        var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
        solr.Add(p);
        solr.Commit();
    }
    

    Let's see if the document is where we left it:

    [Test]
    public void Query() {
        var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Product>>();
        var results = solr.Query(new SolrQueryByField("id", "SP2514N"));
        Assert.AreEqual(1, results.Count);
        Console.WriteLine(results[0].Price);
    }
    
  • 相关阅读:
    .NET Core使用SignalR做登录、推送
    记录node-sass安装失败的解决方法
    .NET Core微服务二:Ocelot API网关
    .NET Core微服务一:Consul服务中心
    ASP.NET Core下Ocelot的简单使用
    ASP.Net Core 发布到IIS Http Error 502.5 官方解决办法
    Windows版Redis主从配置
    IIS 32位/64位 全局模式切换
    NOIP 2007 普及组 初赛 试卷+答案
    P1066 2^k进制数 NOIP 2006 提高组 第四题
  • 原文地址:https://www.cnblogs.com/yanyuge/p/3369602.html
Copyright © 2020-2023  润新知