问题
I have downloaded the onet dataset which comprise of skills taxonomy and I have uploaded it into a elasticsearch. In skills taxonomy there are some skills like c++, .net, C#. I want to give c# and get only c# in skills. by checking some links, I have set the mapping and settings of my index as below.
{
"onnet_taxonomy": {
"mappings": {
"text": {
"properties": {
"Occupation": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Skill": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Skill Type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"keywords": {
"properties": {
"Occupation": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Skill": {
"type": "text",
"fields": {
"analyzed": {
"type": "text",
"analyzer": "analyzer_keyword",
"search_analyzer": "analyzer_shingle"
},
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Skill Type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
},
"settings": {
"index": {
"number_of_shards": "5",
"provided_name": "onnet_taxonomy",
"creation_date": "1583114276039",
"analysis": {
"filter": {
"my_shingle_filter": {
"max_shingle_size": "8",
"min_shingle_size": "2",
"output_unigrams": "true",
"type": "shingle"
}
},
"analyzer": {
"analyzer_keyword": {
"filter": [
"lowercase"
],
"char_filter": [
"code_mapping"
],
"type": "custom",
"tokenizer": "keyword"
},
"analyzer_shingle": {
"filter": [
"lowercase",
"my_shingle_filter"
],
"char_filter": [
"code_mapping"
],
"tokenizer": "standard"
}
},
"char_filter": {
"code_mapping": {
"type": "mapping",
"mappings": [
"++ => plusplus",
"c# => csharp",
"C# => csharp",
"F# => fsharp",
"f# => fsharp",
".net => dotnet",
".Net => dotnet",
".NET => dotnet",
"( => map_lp",
") => map_rp",
"& => and",
"# => hash",
"+ => plus"
]
}
}
},
"number_of_replicas": "1",
"uuid": "LNf2frW1S8WmHSOJWVrvLA",
"version": {
"created": "5030399"
}
}
}
}
}
when i use query as below
{
"query": {
"bool": {
"must": [
{
"match": {
"Skill": "c++"
}
}
]
}
},
"size": 10
i am getting all skills that have 'c'
when i use query as below assuming analyzer is applied
{
"query": {
"bool": {
"must": [
{
"match": {
"Skill.analyzed": "c++"
}
}
]
}
},
"size": 10
}
I get empty output. did i include the analyzer correctly or is my query wrong ?
回答1:
I just simplified your question and for simplicity, let's assume you just have 1 field called title
which contains different languages like c
, c++
, c#
f#
.
Index settings and mapping for this title
field.
{
"settings": {
"index": {
"analysis": {
"analyzer": {
"my_analyzer": {
"filter": [
"lowercase"
],
"char_filter": [
"code_mapping"
],
"tokenizer": "standard" --> notice `standard`
}
},
"char_filter": {
"code_mapping": {
"type": "mapping",
"mappings": [
"++ => plusplus",
"c# => csharp",
"C# => csharp",
"F# => fsharp",
"f# => fsharp",
".net => dotnet",
".Net => dotnet",
".NET => dotnet",
"( => map_lp",
") => map_rp",
"& => and",
"# => hash",
"+ => plus"
]
}
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "my_analyzer" --> using custom analyzer created in settings
}
}
}
}
Index some docs
POST /_doc/{doc-is}
{
"title": "c#"
}
{
"title": "c++"
}
{
"title": "c"
}
{
"title": "F#"
}
Search query, which is provided you in your question which fetches all records which contains c
.
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "c++"
}
}
]
}
},
"size": 10
}
For me now, it retuens only the documents which contains only c++
as shown in my search API result.
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.9808292,
"hits": [
{
"_index": "cplus",
"_type": "_doc",
"_id": "1",
"_score": 0.9808292,
"_source": {
"title": "c++"
}
}
]
}
来源:https://stackoverflow.com/questions/60487022/search-in-elasticsearch-errors-when-applying-analyzer-filter