索引分析器不适用于 Elasticsearch.net 和 Nest
Index analyzer not working with Elasticsearch.net And Nest
我的代码就是..使用nest,elasticsearch 2.3.0版本
我想要映射(+自定义分析器)并创建索引...
但映射不成功低级调用错误!
请检查我的代码并为我复习
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
var client = new ElasticClient(settings);
var request = new IndexExistsRequest("aa");
var result = client.IndexExists(request);
if (result.Exists == true)
{
client.DeleteIndex("aa", null);
}
var ilhee_Custom = new CustomAnalyzer
{
Filter = new List<string> { "lowercase", "stop", "standard", "snowball" },
Tokenizer = "standard"
};
List<Person> categList = new List<Person>();
var Person = new Person
{
id = 1,
Firstname = "an apples bananas boxes, the sun.",
Lastname = "a beautiful womens with a good guys in there"
};
categList.Add(Person);
var response = client.CreateIndex("aa");
var mappingResponse = client.Map<Person>(d => d
.Properties(props => props
.String(s => s
.Name(p => p.Firstname)
.Index(FieldIndexOption.Analyzed)
.Analyzer("ilhee_Custom")
)
.String(s1 => s1
.Name(p1 => p1.Lastname)
.NotAnalyzed()
)
)
.Index("aa")
.Type("person")
);
var b = client.IndexMany<Person>(categList, "aa", "person");
您创建了自定义分析器,但没有将其发送到 Elasticsearch,因此在映射中使用它时,Elasticsearch 对自定义分析器一无所知。
您可以在一个请求中创建一个包含分析和映射的新索引。下面是创建索引、添加自定义分析器和映射作为索引创建的一部分的示例
void Main()
{
var node = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(node)
// set "aa" as the default index; if no index
// is specified for a type or in the request,
// the default index will be used
.DefaultIndex("aa");
var client = new ElasticClient(settings);
var indexExistsResponse = client.IndexExists("aa");
if (indexExistsResponse.Exists)
{
client.DeleteIndex("aa", null);
}
var people = new List<Person>{
new Person
{
id = 1,
Firstname = "an apples bananas boxes, the sun.",
Lastname = "a beautiful womens with a good guys in there"
}
};
var createIndexResponse = client.CreateIndex("aa", c => c
.Settings(s => s
.Analysis(a => a
.Analyzers(ad => ad
// give the custom analyzer a name
.Custom("ilhee_Custom", ca => ca
.Tokenizer("standard")
.Filters("lowercase", "stop", "standard", "snowball")
)
)
)
)
.Mappings(m => m
.Map<Person>(d => d
.Properties(props => props
.String(s => s
.Name(p => p.Firstname)
.Analyzer("ilhee_Custom")
)
.String(s1 => s1
.Name(p1 => p1.Lastname)
.NotAnalyzed()
)
)
)
)
);
var indexManyResponse = client.IndexMany<Person>(people, "aa");
// refresh the index after indexing, so that newly indexed documents
// are available in search results.
client.Refresh("aa");
var searchResponse = client.Search<Person>(s => s
.Query(q => q
.Match(m => m
.Field(p => p.Firstname)
.Query("boxes")
)
)
);
}
public class Person
{
public int id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set;}
}
搜索 returns 按预期返回我们的索引文档
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.15342641,
"hits" : [ {
"_index" : "aa",
"_type" : "person",
"_id" : "1",
"_score" : 0.15342641,
"_source" : {
"id" : 1,
"firstname" : "an apples bananas boxes, the sun.",
"lastname" : "a beautiful womens with a good guys in there"
}
} ]
}
}
我的代码就是..使用nest,elasticsearch 2.3.0版本 我想要映射(+自定义分析器)并创建索引...
但映射不成功低级调用错误!
请检查我的代码并为我复习
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node);
var client = new ElasticClient(settings);
var request = new IndexExistsRequest("aa");
var result = client.IndexExists(request);
if (result.Exists == true)
{
client.DeleteIndex("aa", null);
}
var ilhee_Custom = new CustomAnalyzer
{
Filter = new List<string> { "lowercase", "stop", "standard", "snowball" },
Tokenizer = "standard"
};
List<Person> categList = new List<Person>();
var Person = new Person
{
id = 1,
Firstname = "an apples bananas boxes, the sun.",
Lastname = "a beautiful womens with a good guys in there"
};
categList.Add(Person);
var response = client.CreateIndex("aa");
var mappingResponse = client.Map<Person>(d => d
.Properties(props => props
.String(s => s
.Name(p => p.Firstname)
.Index(FieldIndexOption.Analyzed)
.Analyzer("ilhee_Custom")
)
.String(s1 => s1
.Name(p1 => p1.Lastname)
.NotAnalyzed()
)
)
.Index("aa")
.Type("person")
);
var b = client.IndexMany<Person>(categList, "aa", "person");
您创建了自定义分析器,但没有将其发送到 Elasticsearch,因此在映射中使用它时,Elasticsearch 对自定义分析器一无所知。
您可以在一个请求中创建一个包含分析和映射的新索引。下面是创建索引、添加自定义分析器和映射作为索引创建的一部分的示例
void Main()
{
var node = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(node)
// set "aa" as the default index; if no index
// is specified for a type or in the request,
// the default index will be used
.DefaultIndex("aa");
var client = new ElasticClient(settings);
var indexExistsResponse = client.IndexExists("aa");
if (indexExistsResponse.Exists)
{
client.DeleteIndex("aa", null);
}
var people = new List<Person>{
new Person
{
id = 1,
Firstname = "an apples bananas boxes, the sun.",
Lastname = "a beautiful womens with a good guys in there"
}
};
var createIndexResponse = client.CreateIndex("aa", c => c
.Settings(s => s
.Analysis(a => a
.Analyzers(ad => ad
// give the custom analyzer a name
.Custom("ilhee_Custom", ca => ca
.Tokenizer("standard")
.Filters("lowercase", "stop", "standard", "snowball")
)
)
)
)
.Mappings(m => m
.Map<Person>(d => d
.Properties(props => props
.String(s => s
.Name(p => p.Firstname)
.Analyzer("ilhee_Custom")
)
.String(s1 => s1
.Name(p1 => p1.Lastname)
.NotAnalyzed()
)
)
)
)
);
var indexManyResponse = client.IndexMany<Person>(people, "aa");
// refresh the index after indexing, so that newly indexed documents
// are available in search results.
client.Refresh("aa");
var searchResponse = client.Search<Person>(s => s
.Query(q => q
.Match(m => m
.Field(p => p.Firstname)
.Query("boxes")
)
)
);
}
public class Person
{
public int id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set;}
}
搜索 returns 按预期返回我们的索引文档
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.15342641,
"hits" : [ {
"_index" : "aa",
"_type" : "person",
"_id" : "1",
"_score" : 0.15342641,
"_source" : {
"id" : 1,
"firstname" : "an apples bananas boxes, the sun.",
"lastname" : "a beautiful womens with a good guys in there"
}
} ]
}
}