好的想法是十分钱一打,真正无价的是能够实现这些想法的人。

nested keyword 关键词带空格 精确查询

Elasticsearch | 作者 danishlyy | 发布于2020年12月29日 | 阅读数:1774

定义的mapping结构如下:
{
"my_index": {
"mappings": {
"my_type": {
"properties": {
"user": {
"type": "nested",
"properties": {
"first": {
"type": "keyword"
},
"last": {
"type": "keyword"
}
}
}
}
}
}
}
}
user是一个nested类型,里面有两个属性分别是first和last,都是keyword类型,不分词
然后新增一条文档,语法如下:
 
PUT my_index/my_type/1
{
"user" : [
{
"first" : "按悪綾 意斡謂",
"last" : "Smith"
},
{
"first" : "按悪綾 意斡謂",
"last" : "White"
},
{
"first" : "槹逆瞥 ぜタ礒x3…",
"last" : "White"
}
]
}
然后进行查询,查询语法如下:
GET my_index/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"match": {
"user.first": "按悪綾 意斡謂"
}
}
}
}
}
搜索结果如下:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.47000363,
"hits": [
{
"_index": "my_index",
"_type": "my_type",
"_id": "1",
"_score": 0.47000363,
"_source": {
"user": [
{
"first": "按悪綾 意斡謂",
"last": "Smith"
},
{
"first": "按悪綾 意斡謂",
"last": "White"
},
{
"first": "槹逆瞥 ぜタ礒x3…",
"last": "White"
}
]
}
}
]
}
}
期望:返回两个文档,精确匹配,
实际:返回了三个文档
 
没想明白是写的语法不对还是哪里有问题?es版本是5.6
已邀请:

xiaowuge - 90后

赞同来自: w_b

在查询语句中添加inner_hits:
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "match": {
          "user.first": "按悪綾 意斡謂"
        }
      },"inner_hits": {}
    }
  }
}
 
返回结果,取inner_hits中内容。
    "hits": [
      {
        "_index": "tt2",
        "_type": "my_type",
        "_id": "2",
        "_score": 0.47000363,
        "_source": {
          "user": [
            {
              "first": "按悪綾 意斡謂",
              "last": "Smith"
            },
            {
              "first": "按悪綾 意斡謂",
              "last": "White"
            },
            {
              "first": "槹逆瞥 ぜタ礒x3…",
              "last": "White"
            }
          ]
        },
        "inner_hits": {
          "user": {
            "hits": {
              "total": 2,
              "max_score": 0.47000363,
              "hits": [
                {
                  "_index": "tt2",
                  "_type": "my_type",
                  "_id": "2",
                  "_nested": {
                    "field": "user",
                    "offset": 1
                  },
                  "_score": 0.47000363,
                  "_source": {
                    "first": "按悪綾 意斡謂",
                    "last": "White"
                  }
                },
                {
                  "_index": "tt2",
                  "_type": "my_type",
                  "_id": "2",
                  "_nested": {
                    "field": "user",
                    "offset": 0
                  },
                  "_score": 0.47000363,
                  "_source": {
                    "first": "按悪綾 意斡謂",
                    "last": "Smith"
                  }
                }
              ]
            }
          }
        }
      }
    ]

要回复问题请先登录注册