es - elasticsearch mapping - parameters - norms

747 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :norms有什么特点?
答 :
在这里插入图片描述
问 :norms如何使用?
答 :

# norms
PUT /norms_test
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type"   : "text", 
        "norms"  : false,
        "fields" : {
          "norms" : {
            "type" : "text"
          }
        }
      }
    }
  }
}

# 索引
POST /norms_test/_doc/1
{
  "name" : "hello good"
}

# 索引
POST /norms_test/_doc/2
{
  "name" : "hello hello hello good"
}

# 索引
POST /norms_test/_doc/3
{
  "name" : "hello hello good"
}

# 搜索,norms为false,注意评分
GET /norms_test/_search
{
  "query" : {
    "match" : {
      "name" : "hello"
    }
  }
}

# 结果
{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.24480756,
    "hits" : [
      {
        "_index" : "norms_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.24480756,
        "_source" : {
          "name" : "hello hello hello good"
        }
      },
      {
        "_index" : "norms_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.2259762,
        "_source" : {
          "name" : "hello hello good"
        }
      },
      {
        "_index" : "norms_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.18360566,
        "_source" : {
          "name" : "hello good"
        }
      }
    ]
  }
}


# 搜索,norms为true,注意评分
GET /norms_test/_search
{
  "query" : {
    "match" : {
      "name.norms" : "hello"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.19584605,
    "hits" : [
      {
        "_index" : "norms_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.19584605,
        "_source" : {
          "name" : "hello hello hello good"
        }
      },
      {
        "_index" : "norms_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.18360566,
        "_source" : {
          "name" : "hello hello good"
        }
      },
      {
        "_index" : "norms_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.1546153,
        "_source" : {
          "name" : "hello good"
        }
      }
    ]
  }
}

# 值为false的norms不能修改为true
PUT /norms_test/_mapping
{
  "properties" : {
    "name" : {
      "type"  : "text",
      "norms" : true
    }
  }
}

# 结果
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "Mapper for [name] conflicts with existing mapper:\n\tCannot update parameter [norms] from [false] to [true]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "Mapper for [name] conflicts with existing mapper:\n\tCannot update parameter [norms] from [false] to [true]"
  },
  "status" : 400
}