【发布时间】:2017-03-23 11:02:18
【问题描述】:
在土耳其,我们有土耳其语字母,例如“ğ”、“ü”、“ş”、“ı”、“ö”、“ç”。但是当我们一般搜索时,我们使用字母“g”、“u”、“s”、“i”、“o”、“c”。这不是一个规则,但我们通常会这样做,像一种习惯一样思考,我们曾经这样做过。例如,如果我写驼峰式“Ş”,则应搜索“ş”和“s”。请看这个链接,它是一样的。但是他们的解决方案太长而且不完美。我怎么能在下面?
我的目标是这样的:
ProductName 或 Category.CategoryName 可能包含土耳其语字母(“Eşarp”),或者有些可能输入错误并用英文字母书写(“Esarp”) 查询字符串可能包含土耳其语字母(“eşarp”)或不包含(“esarp”) 查询字符串可能有多个单词 应根据查询字符串搜索每个索引字符串字段(全文搜索)
indexing and full text searching in elasticsearch without dialitics using c# client Nest
我的代码是:
try
{
var node = new Uri(ConfigurationManager.AppSettings["elasticseachhost"]);
var settings = new ConnectionSettings(node);
settings.DefaultIndex("defaultindex").MapDefaultTypeIndices(m => m.Add(typeof(Customer), "myindex"));
var client = new ElasticClient(settings);
string command = Resource1.GetAllData;
using (var ctx = new SearchEntities())
{
Console.WriteLine("ORacle db is connected...");
var customers = ctx.Database.SqlQuery(command).ToList();
Console.WriteLine("Customer count : {0}", customers.Count);
if (customers.Count > 0)
{
var delete = client.DeleteIndex(new DeleteIndexRequest("myindex"));
foreach (var customer in customers)
{
client.Index(customer, idx => idx.Index("myindex"));
Console.WriteLine("Data is indexed in elasticSearch engine");
}
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
Console.WriteLine(ex.Message);
}
我的实体:
public class Customer
{
public string Name{ get; set; }
public string SurName { get; set; }
public string Address{ get; set; }
}
我想我想要的解决方案是:(Create index with multi field mapping syntax with NEST 2.x)
但我无法理解。
Check this out:
[Nest.ElasticsearchType]
public class MyType
{
// Index this & allow for retrieval.
[Nest.Number(Store=true)]
int Id { get; set; }
// Index this & allow for retrieval.
// **Also**, in my searching & sorting, I need to sort on this **entire** field, not just individual tokens.
[Nest.String(Store = true, Index=Nest.FieldIndexOption.Analyzed, TermVector=Nest.TermVectorOption.WithPositionsOffsets)]
string CompanyName { get; set; }
// Don't index this for searching, but do store for display.
[Nest.Date(Store=true, Index=Nest.NonStringIndexOption.No)]
DateTime CreatedDate { get; set; }
// Index this for searching BUT NOT for retrieval/displaying.
[Nest.String(Store=false, Index=Nest.FieldIndexOption.Analyzed)]
string CompanyDescription { get; set; }
[Nest.Nested(Store=true, IncludeInAll=true)]
// Nest this.
List Locations { get; set; }
}
[Nest.ElasticsearchType]
public class MyChildType
{
// Index this & allow for retrieval.
[Nest.String(Store=true, Index = Nest.FieldIndexOption.Analyzed)]
string LocationName { get; set; }
// etc. other properties.
}
After this declaration, to create this mapping in elasticsearch you need to make a call similar to:
var mappingResponse = elasticClient.Map(m => m.AutoMap());
我对上述挑战的第二次尝试: 错误:未检测到分析。大问题是版本差异。我发现很多样本都产生了如下错误: “CreateeIndexDescriptor”不包含“分析”的定义...
using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElasticSearchTest2
{
class Program
{
public static Uri EsNode;
public static ConnectionSettings EsConfig;
public static ElasticClient client;
static void Main(string[] args)
{
EsNode = new Uri("http://localhost:9200/");
EsConfig = new ConnectionSettings(EsNode);
client = new ElasticClient(EsConfig);
var partialName = new CustomAnalyzer
{
Filter = new List { "lowercase", "name_ngrams", "standard", "asciifolding" },
Tokenizer = "standard"
};
var fullName = new CustomAnalyzer
{
Filter = new List { "standard", "lowercase", "asciifolding" },
Tokenizer = "standard"
};
client.CreateIndex("employeeindex5", c => c
.Analysis(descriptor => descriptor
.TokenFilters(bases => bases.Add("name_ngrams", new EdgeNGramTokenFilter
{
MaxGram = 20,
MinGram = 2,
Side = "front"
}))
.Analyzers(bases => bases
.Add("partial_name", partialName)
.Add("full_name", fullName))
)
.AddMapping(m => m
.Properties(o => o
.String(i => i
.Name(x => x.Name)
.IndexAnalyzer("partial_name")
.SearchAnalyzer("full_name")
))));
Employee emp = new Employee() { Name = "yılmaz", SurName = "eşarp" };
client.Index(emp, idx => idx.Index("employeeindex5"));
Employee emp2 = new Employee() { Name = "ayşe", SurName = "eşarp" };
client.Index(emp2, idx => idx.Index("employeeindex5"));
Employee emp3 = new Employee() { Name = "ömer", SurName = "eşarp" };
client.Index(emp3, idx => idx.Index("employeeindex5"));
Employee emp4 = new Employee() { Name = "gazı", SurName = "emir" };
client.Index(emp4, idx => idx.Index("employeeindex5"));
}
}
public class Employee
{
public string Name { set; get; }
public string SurName { set; get; }
}
}
【问题讨论】:
标签: c# elasticsearch nest full-text-indexing