栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

elasticsearch列的唯一过滤器不起作用(插入重复项)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

elasticsearch列的唯一过滤器不起作用(插入重复项)

这是Elasticsearch无法为您提供的开箱即用的东西……您需要在应用程序中提供此唯一性功能。我能想到的唯一想法是将电话号码作为

_id
文档本身,并且每当您插入/更新某项内容时,ES都会使用
contactNumber
as
_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
完全相同。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369632.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号