我想对ElasticSearch索引进行完全匹配查询,
我有以下数据-
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.21110919,
"hits" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.21110919,
"_source" : {
"id" : 1,
"name" : "test"
}
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.160443,
"_source" : {
"id" : 2,
"name" : "test two"
}
}
]
}
}
我想查询
name
字段,我正在尝试搜索名称
test
,但这给了我两个文件。
预期结果是唯一的文件
1
。映射如下-
{
"test" : {
"mappings" : {
"properties" : {
"id" : {
"type" : "long"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
我尝试了以下操作-
GET /test/_search
{
"query": {
"bool": {
"must": {
"term" : {
"name": "test"
}
}
}
}
}
GET /test/_search
{
"query": {
"match": {
"name": "test"
}
}
}
最佳答案
除了指向我在评论中提供的答案的链接之外,建议您将名称字段定义为:
{
"name":{
"type": "text",
"fields":{
"keyword":{
"type": "keyword"
}
}
}
}
然后在需要完全匹配(区分大小写)时查询
name.keyword
字段,如果想要部分匹配(例如仅搜索名字),则查询name
。
关于elasticsearch - 如何在ElasticSearch中进行完全匹配查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60751711/