这是Elasticsearch无法为您提供的开箱即用的东西……您需要在应用程序中提供此唯一性功能。我能想到的唯一想法是将电话号码作为
_id文档本身,并且每当您插入/更新某项内容时,ES都会使用
contactNumberas
_id,它将该文档与现有文档相关联或创建一个新文档。
例如:
PUT /test-index2{ "mappings": { "business": { "_id": { "path": "contactNumber" }, "properties": { "contactNumber": { "type": "string", "analyzer": "keyword" }, "address": { "type": "string" } } } }}然后您索引一些内容:
POST /test-index2/business{ "contactNumber": "(+12)415-3499", "address": "whatever 123"}取回它:
GET /test-index2/business/_search{ "query": { "match_all": {} }}看起来像这样:
"hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "test-index2", "_type": "business", "_id": "(+12)415-3499", "_score": 1, "_source": { "contactNumber": "(+12)415-3499", "address": "whatever 123" } } ] }您在那里看到
_id文档的是电话号码本身。如果要更改或插入另一个文档(地址不同,会有一个新字段
whatever_field--但
contactNumber相同):
POST /test-index2/business{ "contactNumber": "(+12)415-3499", "address": "whatever 123 456", "whatever_field": "whatever value"}Elasticserach“更新”现有文档并通过以下方式回复:
{ "_index": "test-index2", "_type": "business", "_id": "(+12)415-3499", "_version": 2, "created": false}created是
false,这表示文档已更新,而不是创建。
_version是
2再次表示,该文档已被更新。而且
_id是电话号码本身,这表明这是一个已经被更新的文件。
再次在索引中查看,ES将存储以下内容:
"hits": [ { "_index": "test-index2", "_type": "business", "_id": "(+12)415-3499", "_score": 1, "_source": {"contactNumber": "(+12)415-3499","address": "whatever 123 456","whatever_field": "whatever value" } } ]因此,新字段在那里,地址已更改,并且
contactNumber和
_id完全相同。



