【问题标题】:How to create an ElasticSearch Type and make it searchable inside the Index如何创建 ElasticSearch 类型并使其在索引中可搜索
【发布时间】:2017-06-03 16:07:28
【问题描述】:

我是iOS Swift 开发人员,我在我的应用程序中使用ElasticSearch。我正在努力思考如何在ES 中声明typetypedocument 之间的区别是什么,与object/data model 最相似。

Swift 中,我会像这样创建objectdata 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

我迷失的是如何设置索引以使用我的 Sneakers 数据模型作为搜索参考?在我的应用中,用户可以根据运动鞋名称(耐克、阿迪达斯等)和状况(旧、新、旧等)搜索鞋类。

我知道是这样的

curl -XPOST <bonsai_url>/myIndexName/sneakerType -d '
{
   "sneakers": {
      "properties": {
        "condition": {
          "type": string
        },
        "name": {
          "type": string
        }
      }
   }
}
'

我的问题在 ES 中:

  1. typedocument 有什么区别
  2. fields 是否等同于properties
  3. 创建index 名称和type 后,如何将type 设置为 请参考我的data model,它是properties,以便可以搜索到
  4. 我的最后一个问题是_mapping 的用途是什么,我应该在 curl 命令中使用它吗?

【问题讨论】:

标签: curl elasticsearch types document datamodel


【解决方案1】:

ES 中,type 相当于class/data model/object

ES 中,fields 等效于类/数据模型/对象的properties

document 是在索引中实际搜索的结果。如果index 里面有两双运动鞋types 那么index 里面就会有两双documents

Mappings 是 1- 如何使 typeindex 中可搜索,以及 2- 如何设置 index 以便 type 及其 fields 等同于 object/data model及其properties。这个type 和它的fields 是您要运行搜索的对象。

我首先通过创建一个名为sneakerfile.json 的文件来创建type 并在其中添加了这段代码

{
   "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- 现在我有一个名为firebaseindex,2- 一个名为sneakersES type,3- 我现在需要使typeindex 中可搜索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.jsonTEST TEST TEST 看看会发生什么。我自己没有在下面尝试过,但我知道它更容易阅读。

{
   "sneakersTest": {
        "condition": "string"
        "name": "string"
   }
}

假设您已经创建了名为 firebaseindex 运行:

curl -XPOST <BONSAI_URL>/firebase/_mappings/sneakersTest -d@sneakersTestFile.json

然后运行:

curl -XGET <BONSAI_URL>/firebase/_mappings?pretty

搜索它看看会发生什么

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 2013-12-28
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    相关资源
    最近更新 更多