ElasticSearch字段显示为坏字符串,在搜索时导致错误


ElasticSearch field showing as bad string, causes error when searched

我在elasticsearch中有一个数据问题,基本上我需要存储拥有下面文档的userID,我可以在userID之外的任何东西上搜索,如果我在意义上查看数据,我在userID字段上得到一个"坏字符串"错误。

怎么回事?

   {"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":1.0,"hits":[{"_index":"pheme","_type":"searches","_id":"AU__X0W7bLxGTuBqZB_E","_score":1.0,"_source":{
      userID: "AU__X0W7bLxGTuBqZB_E", **<---- This shows as bad string in Sense**
      mustHave: "Laravel",
      notHave: "Moogle",
      couldHave: "AngularJS",
      exclusions: {
        ID : ""
      }
    }
    },{"_index":"pheme","_type":"searches","_id":"AU__ZNA0bLxGTuBqZCCn","_score":0.30685282,"_source":{
      userID: "AU__X0W7bLxGTuBqZB_E",
      mustHave: "Laravel",
      notHave: "Moogle",
      couldHave: "AngularJS",
      exclusions: {
        ID : ""
      }
    }
    },{"_index":"pheme","_type":"searches","_id":"AVAJl5OZbMFoS2ut2YM9","_score":0.30685282,"_source":{
          userID: "AU__X0W7bLxGTuBqZB_E",
          mustHave: "Laravel",
          notHave: "Moogle",
          couldHave: "AngularJS",
          exclusions: {
            ID : ""
          }
        }
        }]}}

问题在于创建数据的方式,如果您注意到在上面的示例中字段名缺少引号:

  {"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":1.0,"hits":[{"_index":"pheme","_type":"searches","_id":"AU__X0W7bLxGTuBqZB_E","_score":1.0,"_source":{
      userID: "AU__X0W7bLxGTuBqZB_E", **<---- This shows as bad string in Sense**
      mustHave: "Laravel",
      notHave: "Moogle",
      couldHave: "AngularJS",
      exclusions: {
        ID : ""
      }
    }
    }
应该

 {"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":3,"max_score":1.0,"hits":[{"_index":"pheme","_type":"searches","_id":"AU__X0W7bLxGTuBqZB_E","_score":1.0,"_source":{
      "userID": "AU__X0W7bLxGTuBqZB_E", **<---- This shows as bad string in Sense**
      "mustHave": "Laravel",
      "notHave": "Moogle",
      "couldHave": "AngularJS",
      "exclusions": {
        "ID" : ""
      }
    }
    }

值得注意的是,Sense在marvel下不会阻止您插入无效数据,Elastic会简单地将该数据输出到您使用的任何客户端,在PHP下这将导致JsonExceptionError,因为无效的json不能被编码成数组。

我也在做父/子关系错误,没有必要有一个字段的userID插入记录时,你只需指定父,然后使用has_parent/filter查询。