solr <defaultSearchField>

 

solr <defaultSearchField>
在schema.xml中。

<defaultSearchField>text</defaultSearchField>

如果在localhost/solr/admin中直接搜索文字, 而不是title:xx,
就会搜索这个text域的索引内容。

所以如果我们想直接搜索某个域的索引, 只要修改这里就行了。


================
可以设置多个
比如
<defaultSearchField>msg_title</defaultSearchField>
<defaultSearchField>msg_content</defaultSearchField>

之前的做法是

 <defaultSearchField>msg_all</defaultSearchField>
<copyField source="msg_title" dest="msg_all"/>
	<copyField source="msg_content" dest="msg_all"/>



但是这样会影响到获取高亮。
比如

List<String> h_title = resp.getHighlighting().get(id)
							.get("msg_title");


上面的代码会返回控制。
因为resp里面只有msg_all, 这样就会混淆高亮的显示。

 

http://localhost:8983/solr/select/?q=TWINX2048-3200PRO&wt=json&version=2.2&start=0&rows=10&indent=on&df=name

 

如果在q中没有指定默认的查询字段,建议添加参数df来指定默认查询字段,如以下示例:

 

http://localhost:8983/solr/select/?q=TWINX2048-3200PRO&wt=json&version=2.2&start=0&rows=10&indent=on&df=name

 

 

我的solr 链接配置文件 为xml,内容如下。我有很多个.netcore,我想注入solr 链接来实现在Controller 中调用solr。xml内容:<?xml version="1.0" encoding="UTF-8"?> <castle> <facilities> <facility id='solr' type='Castle.Facilities.SolrNetIntegration.SolrNetFacility, Castle.Facilities.SolrNetIntegration'> <solrURL>http://127.0.0.1:8983/solr/</solrURL> <cores> <core id='MedTCMDisease'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMDisease</url> </core> <core id='MedTCM'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCM</url> </core> <core id='MedTCMZhongChengYao'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMZhongChengYao</url> </core> <core id='MedTCMFangJi'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMFangJi</url> </core> <core id='MedTCMZhenJiu'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMZhenJiu</url> </core> <core id='MedTCM_L'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCM_L</url> </core> <core id='MedTCMBianzhengLunzhi'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMBianzhengLunzhi</url> </core> <core id='MedTCMMulu'> <documentType>System.Collections.Generic.Dictionary`2[System.String,System.Object]</documentType> <url>http://127.0.0.1:8983/solr/MedTCMMulu</url> </core> </cores> </facility> </facilities> </castle>
07-05
在 ASP.NET Core 8.0 的 MVC 项目中使用 SolrNet,并通过 XML 配置文件注入 Solr 客户端连接信息,可以通过依赖注入机制实现。SolrNet 支持通过配置文件定义 Solr 服务地址,并在运行时动态加载这些配置[^1]。 ### 创建 XML 配置文件 首先,在项目根目录下创建一个 XML 文件(例如 `solr.config.xml`),用于存储 Solr 的连接信息: ```xml <?xml version="1.0" encoding="utf-8"?> <solr> <server> <url>http://localhost:8983/solr/your_core_name</url> </server> </solr> ``` ### 注册 SolrNet 并读取 XML 配置 在 `Program.cs` 中注册 SolrNet 服务,并从 XML 配置文件中加载 Solr 地址: ```csharp using Microsoft.Extensions.DependencyInjection; using SolrNet; using System.IO; using System.Xml.Linq; var builder = WebApplication.CreateBuilder(args); // 读取 XML 配置文件中的 Solr 地址 var solrUrl = XDocument.Load("solr.config.xml").Element("solr")?.Element("server")?.Element("url")?.Value; // 注册 SolrNet 服务 builder.Services.AddSolrNet<Product>(solrUrl); builder.Services.AddControllersWithViews(); var app = builder.Build(); if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run(); ``` ### 定义实体类并映射字段 定义一个实体类 `Product` 来表示 Solr 文档结构,并使用 `SolrField` 和 `SolrUniqueKey` 特性进行字段映射: ```csharp public class Product { [SolrUniqueKey("id")] public string Id { get; set; } [SolrField("name")] public string Name { get; set; } [SolrField("description")] public string Description { get; set; } [SolrField("price")] public decimal Price { get; set; } } ``` ### 在控制器中使用 SolrNet 客户端 在控制器中通过构造函数注入 `ISolrOperations<Product>` 接口,并执行查询、添加等操作: ```csharp using Microsoft.AspNetCore.Mvc; using SolrNet; using System.Threading.Tasks; public class ProductController : Controller { private readonly ISolrOperations<Product> _solr; public ProductController(ISolrOperations<Product> solr) { _solr = solr; } public async Task<IActionResult> Index() { // 查询所有文档 var results = await _solr.QueryAsync(SolrQuery.All); return View(results); } public async Task<IActionResult> Search(string query) { // 执行关键字搜索 var results = await _solr.QueryAsync(new SolrQuery(query)); return View("Index", results); } public async Task<IActionResult> Add() { var product = new Product { Id = "1", Name = "Sample Product", Description = "This is a sample product.", Price = 19.99m }; await _solr.AddAsync(product); await _solr.CommitAsync(); return RedirectToAction("Index"); } } ``` ### 使用视图展示 Solr 数据 在视图中绑定 `IEnumerable<Product>` 类型的数据,并展示 Solr 查询结果: ```html @model IEnumerable<SolrNet.Documentation.Product> <h2>Products</h2> <table class="table"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Description</th> <th>Price</th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td>@item.Id</td> <td>@item.Name</td> <td>@item.Description</td> <td>@item.Price</td> </tr> } </tbody> </table> ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值