curl - 如何创建 ElasticSearch 类型并使其可在索引内搜索

标签 curl elasticsearch types document datamodel

我是 iOS Swift开发人员,我正在使用 ElasticSearch在我的应用程序中。我正在努力思考如何声明 typeES , 和 type 有什么区别和 document ,这与 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

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

我知道这有点像
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 年没有回到这个答案了,但也许可以尝试创建和设置另一个 typesneakersTestFile.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/

    相关文章:

    ssl - 本地主机上的 Behat cURL SSL 证书错误

    php - 结合 SSL/TLS (HTTPS) 的 HTTP 基本身份验证将用于对用户进行身份验证

    PHP 有什么比curl 更快的方法来检查远程图像文件大小大约3000 倍?

    bash - CURL 403 禁止错误,但在 POSTman 请求中运行良好

    json - 在Elasticsearch中按脚本输出过滤

    python - 无要求的Elasticsearch模糊查询

    java - 在 Java 中使用什么数据类型来赚钱?

    elasticsearch - 有没有一种方法可以在弹性中执行嵌套过滤而不影响文档是否匹配?

    arrays - 直接访问R中data.frame中的Vector元素?

    c - 使用自己的 getch 函数进行隐式转换警告