C# 弹性搜索,搜索以短语开头的单词
我正在尝试使用弹性搜索和嵌套为我的网站创建搜索功能。你可以在下面看到我的代码,如果我搜索完整(几乎完全)的单词,我会得到结果。 也就是说,如果我搜索“酪乳”或“酪乳”,我会找到包含“酪乳”一词的文档 然而,我试图实现的是,如果我搜索“Butter”,我将得到所有三个文档的结果,这些文档的单词都以“Butter”开头。我以为这是用FuzzyLikeThis解决的 有人能看出我做错了什么,并给我指出正确的方向吗 我创建了一个控制台应用程序,您可以在此处看到完整的代码:C# 弹性搜索,搜索以短语开头的单词,c#,
elasticsearch,nest,C#,
elasticsearch,Nest,我正在尝试使用弹性搜索和嵌套为我的网站创建搜索功能。你可以在下面看到我的代码,如果我搜索完整(几乎完全)的单词,我会得到结果。 也就是说,如果我搜索“酪乳”或“酪乳”,我会找到包含“酪乳”一词的文档 然而,我试图实现的是,如果我搜索“Butter”,我将得到所有三个文档的结果,这些文档的单词都以“Butter”开头。我以为这是用FuzzyLikeThis解决的 有人能看出我做错了什么,并给我指出正确的方向吗 我创建了一个控制台应用程序,您可以在此处看到完整的代码: using System; u
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nest;
using Newtonsoft.Json;
namespace ElasticSearchTest
{
class Program
{
static void Main(string[] args)
{
var indexSettings = new IndexSettings();
indexSettings.Analysis.Analyzers["text-en"] = new SnowballAnalyzer { Language = "English" };
ElasticClient.CreateIndex("elastictesting", indexSettings);
var testItem1 = new TestItem {
Id = 1,
Name = "Buttermilk"
};
ElasticClient.Index(testItem1, "elastictesting", "TestItem", testItem1.Id);
var testItem2 = new TestItem {
Id = 2,
Name = "Buttercream"
};
ElasticClient.Index(testItem2, "elastictesting", "TestItem", testItem2.Id);
var testItem3 = new TestItem {
Id = 3,
Name = "Butternut"
};
ElasticClient.Index(testItem3, "elastictesting", "TestItem", testItem3.Id);
Console.WriteLine("Write search phrase:");
var searchPhrase = Console.ReadLine();
var searchResults = Search(searchPhrase);
Console.WriteLine("Number of search results: " + searchResults.Count());
foreach (var item in searchResults) {
Console.WriteLine(item.Name);
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
private static List<TestItem> Search(string searchPhrase)
{
var query = BuildQuery(searchPhrase);
var result = ElasticClient
.Search(query)
.Documents
.Select(d => d)
.Distinct()
.ToList();
return result;
}
public static ElasticClient ElasticClient
{
get
{
var localhost = new Uri("http://localhost:9200");
var setting = new ConnectionSettings(localhost);
setting.SetDefaultIndex("elastictesting");
return new ElasticClient(setting);
}
}
private static SearchDescriptor<TestItem> BuildQuery(string searchPhrase)
{
var querifiedKeywords = string.Join(" AND ", searchPhrase.Split(' '));
var filters = new BaseFilter[1];
filters[0] = Filter<TestItem>.Bool(b => b.Should(m => m.Query(q =>
q.FuzzyLikeThis(flt =>
flt.OnFields(new[] {
"name"
}).LikeText(querifiedKeywords)
.PrefixLength(2)
.MaxQueryTerms(1)
.Boost(2))
)));
var searchDescriptor = new SearchDescriptor<TestItem>()
.Filter(f => f.Bool(b => b.Must(filters)))
.Index("elastictesting")
.Type("TestItem")
.Size(500);
var jsons = JsonConvert.SerializeObject(searchDescriptor, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
return searchDescriptor;
}
}
class TestItem {
public int Id { get; set; }
[ElasticProperty(Analyzer = "text-en", Index = FieldIndexOption.analyzed)]
public string Name { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
利用鸟巢;
使用Newtonsoft.Json;
名称空间弹性搜索测试
{
班级计划
{
静态void Main(字符串[]参数)
{
var indexSettings=新的indexSettings();
indexSettings.Analysis.Analyzers[“text en”]=新的雪球分析器{Language=“English”};
ElasticClient.CreateIndex(“elastictesting”,indexSettings);
var testItem1=新TestItem{
Id=1,
Name=“酪乳”
};
ElasticClient.Index(testItem1,“elastictesting”,“TestItem”,testItem1.Id);
var testItem2=新TestItem{
Id=2,
Name=“奶油”
};
ElasticClient.Index(testItem2,“elastictesting”,“TestItem”,testItem2.Id);
var testItem3=新TestItem{
Id=3,
Name=“巴特努特”
};
ElasticClient.Index(testItem3,“elastictesting”,“TestItem”,testItem3.Id);
控制台.WriteLine(“写搜索短语:”);
var searchPhrase=Console.ReadLine();
var searchResults=搜索(searchPhrase);
WriteLine(“搜索结果数:+searchResults.Count());
foreach(搜索结果中的var项){
Console.WriteLine(项目名称);
}
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
}
私有静态列表搜索(字符串搜索短语)
{
var query=BuildQuery(searchPhrase);
var结果=ElasticClient
.搜索(查询)
.文件
.选择(d=>d)
.Distinct()
.ToList();
返回结果;
}
公共静态ElasticClient ElasticClient
{
得到
{
var localhost=新Uri(“http://localhost:9200");
var设置=新连接设置(本地主机);
setting.SetDefaultIndex(“弹性测试”);
返回新的ElasticClient(设置);
}
}
私有静态搜索描述符BuildQuery(字符串搜索短语)
{
var queryfiedkeywords=string.Join(“AND”,searchPhrase.Split(“”));
var过滤器=新的基本过滤器[1];
filters[0]=Filter.Bool(b=>b.Should(m=>m.Query(q=>
q、 FuzzyLikeThis(flt=>
飞行场(新[]{
“姓名”
}).LikeText(查询关键字)
.预桥长度(2)
.MaxQueryTerms(1)
(二)
)));
var searchDescriptor=新的searchDescriptor()
.Filter(f=>f.Bool(b=>b.Must(filters)))
.指数(“弹性测试”)
.Type(“TestItem”)
.尺寸(500);
var jsons=JsonConvert.SerializeObject(searchDescriptor,新的JsonSerializerSettings{NullValueHandling=NullValueHandling.Ignore});
返回搜索描述符;
}
}
课堂测试{
公共int Id{get;set;}
[ElasticProperty(Analyzer=“text-en”,Index=FieldIndexOption.analyzed)]
公共字符串名称{get;set;}
}
}
编辑日期:2014-04-01 11:18 我最终使用了多重匹配和查询字符串,所以这就是我的代码现在的样子。希望它能帮助未来的任何人。另外,我在TestItem中添加了一个Description属性来说明多重匹配
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nest;
using Newtonsoft.Json;
namespace ElasticSearchTest
{
class Program
{
static void Main(string[] args)
{
var indexSettings = new IndexSettings();
ElasticClient.CreateIndex("elastictesting", indexSettings);
var testItem1 = new TestItem {
Id = 1,
Name = "Buttermilk",
Description = "butter with milk"
};
ElasticClient.Index(testItem1, "elastictesting", "TestItem", testItem1.Id);
var testItem2 = new TestItem {
Id = 2,
Name = "Buttercream",
Description = "Butter with cream"
};
ElasticClient.Index(testItem2, "elastictesting", "TestItem", testItem2.Id);
var testItem3 = new TestItem {
Id = 3,
Name = "Butternut",
Description = "Butter with nut"
};
ElasticClient.Index(testItem3, "elastictesting", "TestItem", testItem3.Id);
Console.WriteLine("Write search phrase:");
var searchPhrase = Console.ReadLine();
var searchResults = Search(searchPhrase);
Console.WriteLine("Number of search results: " + searchResults.Count());
foreach (var item in searchResults) {
Console.WriteLine(item.Name);
Console.WriteLine(item.Description);
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
private static List<TestItem> Search(string searchPhrase)
{
var query = BuildQuery(searchPhrase);
var result = ElasticClient
.Search(query)
.Documents
.Select(d => d)
.Distinct()
.ToList();
return result;
}
public static ElasticClient ElasticClient
{
get
{
var localhost = new Uri("http://localhost:9200");
var setting = new ConnectionSettings(localhost);
setting.SetDefaultIndex("elastictesting");
return new ElasticClient(setting);
}
}
private static SearchDescriptor<TestItem> BuildQuery(string searchPhrase)
{
var searchDescriptor = new SearchDescriptor<TestItem>()
.Query(q => q
.MultiMatch(m =>
m.OnFields(new[] {
"name",
"description"
}).QueryString(searchPhrase).Type(TextQueryType.PHRASE_PREFIX)
)
)
.Index("elastictesting")
.Type("TestItem")
.Size(500);
var jsons = JsonConvert.SerializeObject(searchDescriptor, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
return searchDescriptor;
}
}
class TestItem {
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
利用鸟巢;
使用Newtonsoft.Json;
名称空间弹性搜索测试
{
班级计划
{
静态void Main(字符串[]参数)
{
var indexSettings=新的indexSettings();
ElasticClient.CreateIndex(“elastictesting”,indexSettings);
var testItem1=新TestItem{
Id=1,
Name=“酪乳”,
Description=“黄油加牛奶”
};
ElasticClient.Index(testItem1,“elastictesting”,“TestItem”,testItem1.Id);
var testItem2=新TestItem{
Id=2,
Name=“奶油”,
Description=“奶油黄油”
};
ElasticClient.Index(testItem2,“elastictesting”,“TestItem”,testItem2.Id);
var testItem3=新TestItem{
Id=3,
Name=“Butternut”,
Description=“带坚果的黄油”
};
ElasticClient.Index(testItem3,“elastictesting”,“TestItem”,testItem3.Id);
控制台.WriteLine(“写搜索短语:”);
var searchPhrase=Console.ReadLine();
var searchResults=搜索(searchPhrase);
WriteLine(“搜索结果数:+searchResults.Count());
foreach(搜索结果中的var项){
Console.WriteLine(项目名称);
控制台写入线(项目说明);
}
Console.WriteLine(“Pr
curl -XPOST "http://localhost:9200/try/indextype/_search" -d'
{
"query": {
"prefix": {
"field": {
"value": "Butter"
}
}
}
}'