为什么elasticsearch无法按文档ID检索文档?

时间:2017-05-20 14:33:13

标签: python elasticsearch

我是elasticsearch的新手。我使用以下REST API创建了一个新索引: -

req = {
    "settings": {
        "analysis": {
          "analyzer": {
            "hinglish_analyzer": {
              "type":      "custom",
              "tokenizer": "standard",
              "char_filter": [
                "html_strip"
              ],
              "filter": [
                "lowercase",
                "asciifolding",
                "hinglish-token-filter"
              ]
            }
          }
        }
      },
    "mappings" : {
      "p_ss__user" : {
        "properties" : {
          "age" : {
            "type": "integer"
          },
          "first_name" : {
            "type" : "text",
            "analyzer": "hinglish_analyzer"
          },
          "gender" : {
            "type" : "long"
          },
          "is_alive" : {
            "type" : "boolean"
          },
          "last_name" : {
            "type" : "text",
            "analyzer": "hinglish_analyzer"
          },
          "marital_status" : {
            "type" : "long"
          },
          "user_gotra" : {
            "properties" : {
              "Gotra" : {
                "type" : "text",
                "analyzer": "hinglish_analyzer"
              },
              "id" : {
                "type" : "long"
              },
              "kuldevi" : {
                "properties" : {
                  "Kuldevi" : {
                    "type" : "text",
                    "analyzer": "hinglish_analyzer"
                  },
                  "id" : {
                    "type" : "long"
                  }
                }
              }
            }
          },
          "user_village" : {
            "properties" : {
              "areaOrVillageName" : {
                "type" : "text",
                "analyzer": "hinglish_analyzer"
              },
              "id" : {
                "type" : "long"
              },
              "tehsil" : {
                "properties" : {
                  "city" : {
                    "properties" : {
                      "cityName" : {
                        "type" : "text",
                        "analyzer": "hinglish_analyzer"
                      },
                      "id" : {
                        "type" : "long"
                      },
                      "state" : {
                        "properties" : {
                          "country" : {
                            "properties" : {
                              "countryCode" : {
                                "type" : "text"
                              },
                              "countryName" : {
                                "type" : "text",
                                "analyzer": "hinglish_analyzer"
                              },
                              "id" : {
                                "type" : "long"
                              }
                            }
                          },
                          "id" : {
                            "type" : "long"
                          },
                          "stateCode" : {
                            "type" : "text"
                          },
                          "stateName" : {
                            "type" : "text",
                            "analyzer": "hinglish_analyzer"
                          }
                        }
                      }
                    }
                  },
                  "id" : {
                    "type" : "long"
                  },
                  "tehsilName" : {
                    "type" : "text",
                    "analyzer": "hinglish_analyzer"
                  }
                }
              },
              "zipcode" : {
                "type" : "text"
              }
            }
          },
          "username" : {
            "type" : "text",
            "analyzer": "hinglish_analyzer"
          }
        }
      }
    }
}

在这里,'hinglish-token-filter'是我自定义的令牌过滤器,我已经写过,非常好。

现在,我已经在python脚本的帮助下在elasticsearch中创建了一个文档(这里我在请求中传递了自己的_id变量值),如下所示: -

{
    "username" : "Gopi_Chand",
    "first_name" : "Gopi Chand",
    "last_name" : "",
    "gender" : 2,
    "age" : 44,
    "user_gotra" : {
        "Gotra" : "Thanak",
        "kuldevi" : {
        "Kuldevi" : "Maa Jagdambaa",
        "id" : 1
        },
        "id" : 1,
        "kulrishi" : {
            "Rishi" : "Parashar",
            "id" : 1
        }
    },
    "user_village" : {
        "areaOrVillageName" : "Sanatra",
        "tehsil" : {
            "city" : {
                "state" : {
                    "country" : {
                        "countryName" : "India",
                        "id" : 1,
                        "countryCode" : "IND"
                    },
                    "stateName" : "Rajasthan",
                    "id" : 1
                },
                "cityName" : "Barmer (Meru)",
                "id" : 1
            },
            "tehsilName" : "Baitu",
            "id" : 1
        },
        "id" : 1,
        "zipcode" : ""
  },
  "marital_status" : 1,
  "is_alive" : true
}

文档已成功存储在elasticsearch中,其中包含我已传递的ID以及其他值。

但是当我尝试使用id设置检索文档时,问题出现了: -

http://localhost:9200/users/p_s_s__user/3222/

它给了我以下回复: -

{"_index":"users","_type":"p_s_s__user","_id":"3222","found":false}

但是当我尝试跟随查询时: -

http://localhost:9200/users/_search?pretty=true

它显示了我的文档,如下所示: -

{
  "took" : 13,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "users",
        "_type" : "p_ss__user",
        "_id" : "3222",
        "_score" : 1.0,
        "_source" : {
          "username" : "Gopi_Chand",
          "first_name" : "Gopi Chand",
          "last_name" : "",
          "gender" : 2,
          "age" : 44,
          "user_gotra" : {
            "Gotra" : "Thanak",
            "kuldevi" : {
              "Kuldevi" : "Maa Jagdambaa",
              "id" : 1
            },
            "id" : 1,
            "kulrishi" : {
              "Rishi" : "Parashar",
              "id" : 1
            }
          },
          "user_village" : {
            "areaOrVillageName" : "Sanatra",
            "tehsil" : {
              "city" : {
                "state" : {
                  "country" : {
                    "countryName" : "India",
                    "id" : 1,
                    "countryCode" : "IND"
                  },
                  "stateName" : "Rajasthan",
                  "id" : 1
                },
                "cityName" : "Barmer (Meru)",
                "id" : 1
              },
              "tehsilName" : "Baitu",
              "id" : 1
            },
            "id" : 1,
            "zipcode" : ""
          },
          "marital_status" : 1,
          "is_alive" : true
        }
      }
    ]
  }
}
你帮我解决了,我做错了什么?此外,其他查询,如"匹配"查询,也无法正常工作。

提前致谢。

0 个答案:

没有答案