请问es7版本copy_to这里检索问题如何解决?

来源:4-4 -copy_to参数说明

悟亦凡

2022-04-10

创建my_index索引,并指定first_name和last_name字段copy_to到full_name字段:

PUT /my_index
{
  "mappings": {
    "properties": {
      "first_name":{
        "type": "text",
        "copy_to": "full_name"
      },
      "last_name":{
        "type": "text",
         "copy_to": "full_name"
      },
      "full_name":{
        "type": "text"
      }
    }
  }
}

依次创建两个文档如下:

PUT  /my_index/_doc/1
{
  "first_name":"zhang",
  "last_name":"san"
}

PUT  /my_index/_doc/2
{
  "first_name":"zhang",
  "last_name":"san san"
}

根据full_name进行检索名字为zhang san san的人:

GET /my_index/_search
{
  "query": {
    "match": {
      "full_name": "zhang san san"
    }
  }
}

结果响应如下:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.64321595,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.64321595,
        "_source" : {
          "first_name" : "zhang",
          "last_name" : "san san"
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5957041,
        "_source" : {
          "first_name" : "zhang",
          "last_name" : "san"
        }
      }
    ]
  }
}

显然并不是我想要的结果,按照视频里的方式设置operator参数:

GET /my_index/_search
{
  "query": {
    "match": {
      "full_name": "zhang san san",
      "operator": "and"
    }
  }
}

会抛出异常:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "[match] query doesn't support multiple fields, found [full_name] and [operator]",
        "line" : 5,
        "col" : 19
      }
    ],
    "type" : "parsing_exception",
    "reason" : "[match] query doesn't support multiple fields, found [full_name] and [operator]",
    "line" : 5,
    "col" : 19
  },
  "status" : 400
}
写回答

1回答

rockybean

2022-04-13

正确的语句是这个


GET /my_index/_search

{

  "query": {

    "match": {

      "full_name": {

        "query": "zhang san san",

        "operator": "and"

      }

    }

  }

}


但这个也不会符合你的预期,因为 san 和 san san 的分词结果都是 1个 term 'san',所以即便你用 and 也是两个都会搜出来,但是 zhang san san 的 score 会高一些。

如果你想要只匹配一个 zhang san san,那你最好建立一个 keyword 类型的field,然后用 term 来查询做一个补充。

0
0

Elastic Stack从入门到实践,动手搭建数据分析系统

有了Elastic Stack,不用写一行代码,你也可以玩转大数据分析!

1361 学习 · 397 问题

查看课程