首页 > 其他分享 >全文检索match_phrase

全文检索match_phrase

时间:2022-11-02 23:12:28浏览次数:41  
标签:name xiaohong 全文检索 offset phrase type match desc

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作为查询方式,那么一定要严格的按照分词顺序进行查询,否则查询不到

标签:name,xiaohong,全文检索,offset,phrase,type,match,desc
From: https://www.cnblogs.com/yeasxy/p/16852891.html

相关文章