ElasticSearch分析了各个领域 [英] ElasticSearch analyzed fields
问题描述
curl -X PUThttp:// localhost:9200 / $ IndexName /-d{
settings:{
analysis:{
analyzer:{
analyzer1:{
type :custom,
tokenizer:keyword,
filter:[standard,smallcase,stop,snowball,my_synonyms]
}
}
},
过滤器:{
my_synonyms:{
type:synonym,
peers_path 同义词txt
}
}
}
},
mappings:{
product:{
:{
title:{
type:string,
search_analyzer:analyzer1,
index_analyzer:analyzer1
}
}
}
}
}';
在标题字段中搜索单个单词时出现问题。如果它的帽子里装满了猫,它将它存储为猫在帽子,但如果我搜索猫,我什么也没有返回。
这是否甚至可能完成或需要有两个单独的字段,并分析一个与关键字,另一个与雪球?
我正在使用嵌套的vb代码来索引数据,如果重要。
感谢
Robert
您可以使用字段
属性(以前称为多个字段 )
我的VB.NET有点生锈,所以我希望你不介意C#的例子。如果您正在使用开发分支中的最新代码,则只需将 Fields
添加到每个核心映射描述符,以便现在可以执行此操作:
client.Map< Foo>(m => m
.Properties(props =>道具
.String(s => ; s
.Name(o => o.Bar)
.Analyzer(keyword)
.Fields(fs => fs
.String(f => ; f
.Name(o => o.Bar.Suffix(stemmed))
.Analyzer(snowball)
)
)
)
)
);
否则,如果您使用NEST 1.0.2或更早版本(您可能是),您必须通过旧的多字段
类型方式完成此操作:
客户端.Map&Foo>(m => m
.Properties(props => props
.MultiField(mf => mf
.Name(o => o.Bar)
.Fields(fs => fs
.String(s => s
.Name(o => o.Bar)
.Analyzer(keyword )
.String(s => s
.Name(o => o.Bar.Suffix(stemmed))
.Analyzer(snowball))
)
)
)
);
Elasticsearch支持两种方式,并将完全相同的事情。将关键字
分析器应用于主要栏
字段和 snowball
分析器到 bar.stemmed
字段。 stemmed
当然只是我在这些例子中选择的后缀,你可以使用任何你想要的后缀名。实际上,您不需要添加一个后缀,可以将多个字段命名为完全不同于主字段。
I'm building my search but need to analyze 1 field with different analyzers. My problem is for a field I need to have an analyzer on it for stemming (snowball) and then also one to keep the full word as one token (keyword). I can get this to work by the following index settings:
curl -X PUT "http://localhost:9200/$IndexName/" -d '{
"settings":{
"analysis":{
"analyzer":{
"analyzer1":{
"type":"custom",
"tokenizer":"keyword",
"filter":[ "standard", "lowercase", "stop", "snowball", "my_synonyms" ]
}
}
},
"filter": {
"my_synonyms": {
"type": "synonym",
"synonyms_path ": "synonyms.txt"
}
}
}
},
"mappings": {
"product": {
"properties": {
"title": {
"type": "string",
"search_analyzer" : "analyzer1",
"index_analyzer" : "analyzer1"
}
}
}
}
}';
The problem comes when searching on a single word in the title field. If it's populated with The Cat in the Hat it will store it as "The Cat in the Hat" but if I search for cats I get nothing returned.
Is this even possible to accomplish or do I need to have 2 separate fields and analyze one with keyword and the other with snowball?
I'm using nest in vb code to index the data if that matters.
Thanks Robert
You can apply two different analyzers to the same using the fields
property (previously known as multi fields).
My VB.NET is a bit rusty, so I hope you don't mind the C# examples. If you're using the latest code from the dev branch, Fields
was just added to each core mapping descriptor so you can now do this:
client.Map<Foo>(m => m
.Properties(props => props
.String(s => s
.Name(o => o.Bar)
.Analyzer("keyword")
.Fields(fs => fs
.String(f => f
.Name(o => o.Bar.Suffix("stemmed"))
.Analyzer("snowball")
)
)
)
)
);
Otherwise, if you're using NEST 1.0.2 or earlier (which you likely are), you have to accomplish this via the older multi field
type way:
client.Map<Foo>(m => m
.Properties(props => props
.MultiField(mf => mf
.Name(o => o.Bar)
.Fields(fs => fs
.String(s => s
.Name(o => o.Bar)
.Analyzer("keyword"))
.String(s => s
.Name(o => o.Bar.Suffix("stemmed"))
.Analyzer("snowball"))
)
)
)
);
Both ways are supported by Elasticsearch and will do the exact same thing. Applying the keyword
analyzer to the primary bar
field, and the snowball
analyzer to the bar.stemmed
field. stemmed
of course was just the suffix I chose in these examples, you can use whatever suffix name you desire. In fact, you don't need to add a suffix, you can name the multi field something completely different than the primary field.
这篇关于ElasticSearch分析了各个领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!