我是 iOS Swift
开发人员,我正在使用 ElasticSearch
在我的应用程序中。我正在努力思考如何声明 type
在 ES
, 和 type
有什么区别和 document
,这与 object/data model
最相似.
在 Swift
我会创建一个 object
或 data model
像这样:
class Sneakers {
var condition: String?
var name: String?
}
这是说我创建了一个名为 Sneakers 的对象,它有 2 个属性:
Optional
的“条件”和“名称”。 (问号)类型String
.我知道要创建并将我的 ES 设置为
Index
我使用以下内容:curl -XPOST <bonsai_url>/myIndexName //I'm using Heroku & Bonsai for my ES cluster
然后我可以像这样设置一个类型
curl -XPOST <bonsai_url>/myIndexName/sneakerType
我迷失的地方是如何设置索引以使用我的运动鞋数据模型作为搜索引用?在我的应用程序中,用户可以根据运动鞋的名称(耐克、阿迪达斯等)和状况(旧的、新的、二手的等)搜索鞋类。
我知道这有点像
curl -XPOST <bonsai_url>/myIndexName/sneakerType -d '
{
"sneakers": {
"properties": {
"condition": {
"type": string
},
"name": {
"type": string
}
}
}
}
'
我的问题是在 ES 中:
type
和有什么区别和一个 document
fields
相当于 properties
? index
之后姓名和 type
, 我该如何制作 type
到引用我的
data model
它是 properties
以便可以搜索 _mapping
因为我应该在 curl 命令中使用它吗? 最佳答案
在 ES
type
相当于 class/data model/object
.
在 ES
fields
相当于类/数据模型/对象的 properties
.
一个 document
是在索引中实际搜索的结果。如果在index
里面有2双运动鞋types
然后是 index
将有 2 documents
在它里面。Mappings
是 1- 您如何制作 type
可在 index
内搜索和 2- 如何设置 index
使type
和它的 fields
将等同于 object/data model
和它的 properties
.此 type
和它的 fields
是您要进行搜索的对象。
我首先创建了一个 type
通过创建一个名为 sneakerfile.json
的文件
并在其中添加了此代码
{
"sneakers": { // this is a type
"properties": {
"condition": { // it has a field named ‘condition’
"type": "string"
},
"name": { // it has a field named ‘name’
"type": "string"
}
}
}
}
// sneakers is being treated like my Sneakers Swift class/dataModel from the original question
// the fields “condition” & “name” are of type String just like my Swift's Sneakers’ class' properties (since Optionals are a Swift thing that’s not included here)
然后在终端内我创建了我的 ES index
名为 firebase
通过运行:curl -XPOST <BONSAI_URL>/firebase
1- 现在我有一个 index
命名 firebase
, 2- ES type
命名 sneakers
, 3- 我现在需要制作 type
可在 index
中搜索通过 4- 填充 _mappings
关键是:curl -XPOST <BONSAI_URL>/firebase/_mappings/sneakers -d@sneakerfile.json
现在我想看看我的 mappings
里面有什么我运行时的键:curl -XGET <BONSAI_URL>/firebase/_mappings?pretty
我去拿{
"firebase" : {
"mappings" : {
"sneakers" : {
"properties" : {
"condition" : {
"type" : "string"
},
"name" : {
"type" : "string"
}
}
}
}
}
}
// Final Result -the index named firebase now can return documents of type ‘sneakers’. You can search for those documents by searching on the ‘condition’ and ‘name’ fields
作为旁注上述答案 definitely 100% works
所以你可以使用它。我已经超过 2 年没有回到这个答案了,但也许可以尝试创建和设置另一个 type
到 sneakersTestFile.json
像这样。 测试测试看看会发生什么。我自己没有在下面尝试过这个,但我知道它更容易阅读。{
"sneakersTest": {
"condition": "string"
"name": "string"
}
}
假设您已经创建了 index
命名 firebase
跑:curl -XPOST <BONSAI_URL>/firebase/_mappings/sneakersTest -d@sneakersTestFile.json
然后运行:curl -XGET <BONSAI_URL>/firebase/_mappings?pretty
搜索它看看会发生什么
关于curl - 如何创建 ElasticSearch 类型并使其可在索引内搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41733655/