phrase的作用是短语匹配。

比如我插入一条创建一个索引并且插入数据,并且以name is xiaohong开始作为关键短语开始查询

PUT /test/_doc/1
{
  "desc":"hello, my name is xiaohong"
}

查看mapping关系可以得到的结果如下,可以知道插入的desc字段是被分词了的

请求:
GET /test/_mapping

返回:
{
  "test" : {
    "mappings" : {
      "properties" : {
        "desc" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

由于没有指定分词器,那么当前索引就是使用默认的分词器,我们来看一下是怎么分词的。

请求
GET _analyze
{
  "analyzer": "standard",
  "text": ["hello, my name is xiaohong"]
}

返回
{
  "tokens" : [
    {
      "token" : "hello",
      "start_offset" : 0,
      "end_offset" : 5,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "my",
      "start_offset" : 7,
      "end_offset" : 9,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "name",
      "start_offset" : 10,
      "end_offset" : 14,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "is",
      "start_offset" : 15,
      "end_offset" : 17,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "xiaohong",
      "start_offset" : 18,
      "end_offset" : 26,
      "type" : "<ALPHANUM>",
      "position" : 4
    }
  ]
}

可以看到的是按照空格来进行分词

下面开始查询测试

  • 使用短语进行完整的查询,是可以查询出结果的
请求:
GET /test/_search
{
  "query": {
    "match_phrase": {
      "desc": "name is xiaohong"
    }
  }
}

返回:
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.8630463,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.8630463,
        "_source" : {
          "desc" : "hello, my name is xiaohong"
        }
      }
    ]
  }
}
  • 颠倒词语顺序,变成xiaohong is name作为短语来查询。
    得到的结果是无结果
请求:
GET /test/_search
{
  "query": {
    "match_phrase": {
      "desc": "xiaohong is name"
    }
  }
}

返回:
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
  • 短语进行跳单词查询,变成name xiaohong作为短语来查询。
    得到的结果是无结果
请求:
GET /test/_search
{
  "query": {
    "match_phrase": {
      "desc": "name xiaohong"
    }
  }
}

返回:
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

所以如果使用phrase作为查询方式,那么一定要严格的按照分词顺序进行查询,否则查询不到

原文地址:http://www.cnblogs.com/yeasxy/p/16852891.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性