1、精确值 VS 全文

Elasticsearch 中的数据可以概括的分为两类:精确值和全文。

精确值如它们听起来那样精确,例如日期或者用户ID,对于精确值来讲,Foo 和 foo 是不同的,2020 和 2020-12-25 也是不同的。

全文是指文本数据,通常以人类容易识别的语言书写,例如一篇文章的内容或一封邮件的内容。

精确值很容易查询,其结果要么匹配,要么不匹配。但是,查询全文数据要微妙的多。我们问的不只是“这个文档匹配查询吗”,而是“该文档匹配查询的程度有多大?”换句话说,该文档与给定查询的相关性如何?

我们很少对全文类型的内容做精确匹配,并且,我们还希望搜索能够理解我们的意图:

搜索 UK ,会返回包含 United Kindom 的文档。
搜索 jump ,会匹配 jumped , jumps , jumping ,甚至是 leap 。

2、映射

针对不同的数据类型,应该设置不同类型的映射,从而达到精确查询或者全文搜索的功能。默认情况下,Elasticsearch 会帮我们自动设置好映射类型,请看下面的例子。

2.1、新建一个索引

curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/myindex' -d '
{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
    }
}
'

备注:查看和删除索引

curl 'localhost:9200/_cat/indices?v'
curl -X DELETE 'localhost:9200/myindex?pretty'

2.2、向索引中添加文档

curl -X PUT "localhost:9200/myindex/_doc/1?pretty" -H 'Content-Type: application/json' -d'{
    "id":1,
    "title":"网站公告",
    "content":"网站核心社群正式启动!",
    "date":"2020-05-12"
}'

curl -X PUT "localhost:9200/myindex/_doc/2?pretty" -H 'Content-Type: application/json' -d'{
    "id":2,
    "title":"网站公告",
    "content":"网站核心社群正式启动! 启动时间为2020-05-12。",
    "date":"2020-05-12"
}'

2.3、查看默认的映射

curl -X GET http://localhost:9200/myindex/_mappings?pretty

默认的映射设置为:

{
  "myindex" : {
    "mappings" : {
      "properties" : {
        "content" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "date" : {
          "type" : "date"
        },
        "id" : {
          "type" : "long"
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

3、映射类型决定搜索结果

3.1、全文搜索

curl -X POST -H 'Content-Type: application/json'  "localhost:9200/myindex/_search?pretty" -d '{
    "query":{
        "query_string":{
            "query":"2020-05"
        }
    }
}'

此时的搜索结果为:命中文档2。

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.241217,
    "hits" : [
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.241217,
        "_source" : {
          "id" : 1,
          "title" : "网站公告",
          "content" : "网站核心社群正式启动! 启动时间为2020-05-12。",
          "date" : "2020-05-12"
        }
      }
    ]
  }
}

3.2、精确搜索(有命中)

curl -X POST -H 'Content-Type: application/json'  "localhost:9200/myindex/_search?pretty" -d '{
    "query":{
        "query_string":{
            "query":"2020-05-12"
        }
    }
}'

此时的搜索结果为:同时命中文档1和文档2。

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.8618255,
    "hits" : [
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.8618255,
        "_source" : {
          "id" : 1,
          "title" : "网站公告",
          "content" : "网站核心社群正式启动! 启动时间为2020-05-12。",
          "date" : "2020-05-12"
        }
      },
      {
        "_index" : "myindex",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "title" : "网站公告",
          "content" : "网站核心社群正式启动!",
          "date" : "2020-05-12"
        }
      }
    ]
  }
}

3.3、精确搜索(无命中)

curl -X POST -H 'Content-Type: application/json'  "localhost:9200/myindex/_search?pretty" -d '{
    "query":{
        "match":{
            "date":"2020"
        }
    }
}'

此时的搜索结果为:无命中。


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

标签: none

[网站公告]-[2024年兼职介绍]


添加新评论