es - elasticsearch mapping - parameters - similarity

147 阅读1分钟

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

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

# similarity
PUT /similarity_test
{
  "mappings" : {
    "properties" : {
      "name1" : {
        "type"   : "text",
        "fields" : {
          "boolean" : {
            "type"       : "text",
            "similarity" : "boolean"
          }
        }
      }
    }
  }
}

# 索引
POST /similarity_test/_doc/1
{
  "name1" : "hello good"
}

# 索引
POST /similarity_test/_doc/2
{
  "name1" : "hello me hello"
}

# 搜索,有评分排行
GET /similarity_test/_search
{
  "query" : {
    "match" : {
      "name1" : "hello"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.16994905,
    "hits" : [
      {
        "_index" : "similarity_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.16994905,
        "_source" : {
          "name1" : "hello me hello"
        }
      },
      {
        "_index" : "similarity_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.14181954,
        "_source" : {
          "name1" : "hello good"
        }
      }
    ]
  }
}


# 搜索,没有评分排行
GET /similarity_test/_search
{
  "query" : {
    "match" : {
      "name1.boolean" : "hello"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "similarity_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name1" : "hello good"
        }
      },
      {
        "_index" : "similarity_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name1" : "hello me hello"
        }
      }
    ]
  }
}