映射包含不应分析的嵌套字段(不确定“not_analyzed”值是否准确)。是否可以对嵌套字段进行精确匹配?在下面的查询中,“metadata.value”:“2014.NWJSD.47”仍然被分析。 Elasticsearch 将字符串分解为多个术语(“2014”、“NWJSD”、“47”)。我尝试使用“term”而不是“match”,但这没有返回任何结果。
"mappings" : {
"metadata" : {
"type" : "nested",
"properties" : {
"name" : {
"type" : "text",
"index" : "not_analyzed"
},
"value" : {
"type" : "text",
"index" : "not_analyzed"
}
}
}
}
查询:
"query": {
"bool": {
"must": [
{
"nested": {
"path": "metadata",
"query": {
"bool": {
"must": [
{
"match": {
"metadata.name": "number"
}
},
{
"match": {
"metadata.value": "2014.NWJSD.47"
}
}
]
}
}
}
}
]
}
}
最佳答案
尝试在映射中使用关键字而不是文本,例如:
{
"mappings": {
"your_type_name": {
"properties": {
"metadata" : {
"type" : "nested",
"properties" : {
"name" : {
"type" : "keyword"
},
"value" : {
"type" :"keyword"
}
}
}
}
}
}
}
不会分析这些字段。然后你应该重新索引你的数据并查询你的数据你应该用 term 替换匹配(这是分析的查询)。
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "metadata",
"query": {
"bool": {
"must": [
{
"term": {
"metadata.name": "number"
}
},
{
"term": {
"metadata.value": "2014.NWJSD.47"
}
}
]
}
}
}
}
]
}
}
}
关于Elasticsearch:如何在嵌套字段中获得完全匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47160653/