美文网首页
Elasticsearch搜索关键字不进行分词

Elasticsearch搜索关键字不进行分词

作者: AC编程 | 来源:发表于2021-10-11 16:41 被阅读0次

    一、搜索关键字分词效果

    示例:查询名称包含手机的记录

    GET /sku/_search
    {
      "query": {
        "match": {
          "name": "手机"
        }
      }
    }
    

    结果结果如下:

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 4,
          "relation" : "eq"
        },
        "max_score" : 0.44183272,
        "hits" : [
          {
            "_index" : "sku",
            "_type" : "_doc",
            "_id" : "Ompe7HEBQIJxGG8U13yY",
            "_score" : 0.44183272,
            "_source" : {
              "name" : "小米手机",
              "price" : 200000,
              "spuId" : 101,
              "createTime" : "2020-05-09",
              "brandName" : "小米",
              "categoryName" : "手机",
              "saleNum" : 10012,
              "commentNum" : 323,
              "spec" : {
                "网络制式" : "移动4g",
                "屏幕尺寸" : "4.5"
              }
            }
          },
          {
            "_index" : "sku",
            "_type" : "_doc",
            "_id" : "3",
            "_score" : 0.44183272,
            "_source" : {
              "name" : "苹果手机",
              "price" : 100000,
              "spuId" : 10112,
              "createTime" : "2020-05-01",
              "brandName" : "苹果",
              "categoryName" : "手机",
              "saleNum" : 10012,
              "commentNum" : 323,
              "spec" : {
                "网络制式" : "全网",
                "屏幕尺寸" : "56"
              }
            }
          },
          {
            "_index" : "sku",
            "_type" : "_doc",
            "_id" : "4",
            "_score" : 0.44183272,
            "_source" : {
              "name" : "vivo手机",
              "price" : 200000,
              "spuId" : 10118,
              "createTime" : "2020-05-01",
              "brandName" : "vivo",
              "categoryName" : "手机",
              "saleNum" : 10012,
              "commentNum" : 323,
              "spec" : {
                "网络制式" : "全网",
                "屏幕尺寸" : "56"
              }
            }
          },
          {
            "_index" : "sku",
            "_type" : "_doc",
            "_id" : "gAWw_nEBqh6AXa2lyxIz",
            "_score" : 0.44183272,
            "_source" : {
              "name" : "三星手机",
              "price" : 200000,
              "spuId" : 104,
              "createTime" : "2020-05-09",
              "brandName" : "三星",
              "categoryName" : "手机",
              "saleNum" : 10012,
              "commentNum" : 323,
              "spec" : {
                "网络制式" : "移动4g",
                "屏幕尺寸" : "4.5"
              }
            }
          }
        ]
      }
    }
    

    如果我们查询“小米电视”会有几条记录被查询出来呢?你可以能说会有一条,但我们测试一下会看到结果为:小米电视、小米手机、三星电视三条结果,这是为什么呢?这是因为在查询时,会先搜索关键字进行分词,对分词后的字符串进行查询(分成小米、电视两个词),只要是包含这些字符串的都是要被查询出来的,多个词之间是or的关系。

    但注意,查询结果的匹配分值_score 是不一样的,分值高的排在前面。

    搜索:小米电视

    GET /sku/_search
    {
      "query": {
        "match": {
          "name": "小米电视"
        }
      }
    }
    

    查询结果:

    {
      "took" : 6,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 3,
          "relation" : "eq"
        },
        "max_score" : 2.059239,
        "hits" : [
          {
            "_index" : "sku",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 2.059239,
            "_source" : {
              "name" : "小米电视",
              "price" : 100000,
              "spuId" : 10111,
              "createTime" : "2020-05-01",
              "brandName" : "小米",
              "categoryName" : "电视",
              "saleNum" : 10012,
              "commentNum" : 323,
              "spec" : {
                "网络制式" : "全网",
                "屏幕尺寸" : "56"
              }
            }
          },
          {
            "_index" : "sku",
            "_type" : "_doc",
            "_id" : "Ompe7HEBQIJxGG8U13yY",
            "_score" : 1.0296195,
            "_source" : {
              "name" : "小米手机",
              "price" : 200000,
              "spuId" : 101,
              "createTime" : "2020-05-09",
              "brandName" : "小米",
              "categoryName" : "手机",
              "saleNum" : 10012,
              "commentNum" : 323,
              "spec" : {
                "网络制式" : "移动4g",
                "屏幕尺寸" : "4.5"
              }
            }
          },
          {
            "_index" : "sku",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0296195,
            "_source" : {
              "name" : "三星电视",
              "price" : 100000,
              "spuId" : 10111,
              "createTime" : "2020-05-01",
              "brandName" : "三星",
              "categoryName" : "电视",
              "saleNum" : 10012,
              "commentNum" : 323,
              "spec" : {
                "网络制式" : "全网",
                "屏幕尺寸" : "56"
              }
            }
          }
        ]
      }
    }
    

    如果我们想要进行精确查询,想要的是查询“小米电视”这一条记录,怎么办呢?我们可以这样写:

    GET /sku/_search
    {
      "query": {
        "match": {
          "name": {
            "query": "小米电视",
            "operator": "and"
          }
        }
      }
    }
    

    查询结果只有小米电视一条记录了:

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 2.059239,
        "hits" : [
          {
            "_index" : "sku",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 2.059239,
            "_source" : {
              "name" : "小米电视",
              "price" : 100000,
              "spuId" : 10111,
              "createTime" : "2020-05-01",
              "brandName" : "小米",
              "categoryName" : "电视",
              "saleNum" : 10012,
              "commentNum" : 323,
              "spec" : {
                "网络制式" : "全网",
                "屏幕尺寸" : "56"
              }
            }
          }
        ]
      }
    }
    

    operator 指定为and,不指定时默认为or。

    二、Java关键代码

    SearchRequest request = new SearchRequest(index);
    
    MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery(pageSearchVO.getField(), keyword);
    
    //精确查询(关键字不分词查询)
    queryBuilder.operator(Operator.AND);
    
    builder.query(queryBuilder);
    
    request.source(builder);
    
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    

    相关文章

      网友评论

          本文标题:Elasticsearch搜索关键字不进行分词

          本文链接:https://www.haomeiwen.com/subject/myololtx.html