范围过滤器不适用于 "gt" 运算符但适用于 "lt"

Range filter not working for "gt" operator but working for "lt"

我正在使用弹性搜索为我的文档编制索引,并希望根据特定 属性 过滤文档。这是我的代码:

filter.push({
            range: {"audience.ethnicity.asian" : { gt: 50 } }
        })

它不适用于 gt 运算符(发回不一致的结果)但适用于 lt 运算符

filter.push({
                range: {"audience.ethnicity.asian" : { lt: 50 } }
            })

这效果令人惊讶。我仔细检查了 属性 值是一个整数。我尝试了很多不同的方法,但似乎无法弄清楚我错过了什么。 映射:

  "audience": {
    "properties": {
      "age": {
        "properties": {
          "13-17": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "18-24": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "25-34": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "35-44": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "45-64": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "eighteen": {
            "type": "long"
          },
          "fortyfive": {
            "type": "long"
          },
          "thirteen": {
            "type": "long"
          },
          "thirtyfive": {
            "type": "long"
          },
          "twentyfive": {
            "type": "long"
          }
        }
      },
      "ages": {
        "properties": {
          "13-17": {
            "type": "float"
          },
          "18-24": {
            "type": "float"
          },
          "25-34": {
            "type": "float"
          },
          "35-44": {
            "type": "float"
          },
          "45-64": {
            "type": "float"
          }
        }
      },
      "ethnicity": {
        "properties": {
          "African American": {
            "type": "float"
          },
          "Asian": {
            "type": "float"
          },
          "Hispanic": {
            "type": "float"
          },
          "White / Caucasian": {
            "type": "float"
          },
          "african": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "asian": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "hispanic": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "white": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      },
      "gender": {
        "properties": {
          "female": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "male": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      },
      "genders": {
        "properties": {
          "FEMALE": {
            "type": "float"
          },
          "MALE": {
            "type": "float"
          }
        }
      },
      "gendersPerAge": {
        "properties": {
          "13-17": {
            "properties": {
              "FEMALE": {
                "type": "float"
              },
              "MALE": {
                "type": "float"
              }
            }
          },
          "18-24": {
            "properties": {
              "FEMALE": {
                "type": "float"
              },
              "MALE": {
                "type": "float"
              }
            }
          },
          "25-34": {
            "properties": {
              "FEMALE": {
                "type": "float"
              },
              "MALE": {
                "type": "float"
              }
            }
          },
          "35-44": {
            "properties": {
              "FEMALE": {
                "type": "float"
              },
              "MALE": {
                "type": "float"
              }
            }
          },
          "45-64": {
            "properties": {
              "FEMALE": {
                "type": "float"
              },
              "MALE": {
                "type": "float"
              }
            }
          }
        }
      }
    }
  }          "audience": {
    "properties": {
      "age": {
        "properties": {
          "13-17": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "18-24": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "25-34": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "35-44": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "45-64": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "eighteen": {
            "type": "long"
          },
          "fortyfive": {
            "type": "long"
          },
          "thirteen": {
            "type": "long"
          },
          "thirtyfive": {
            "type": "long"
          },
          "twentyfive": {
            "type": "long"
          }
        }
      },
      "ages": {
        "properties": {
          "13-17": {
            "type": "float"
          },
          "18-24": {
            "type": "float"
          },
          "25-34": {
            "type": "float"
          },
          "35-44": {
            "type": "float"
          },
          "45-64": {
            "type": "float"
          }
        }
      },
      "ethnicity": {
        "properties": {
          "African American": {
            "type": "float"
          },
          "Asian": {
            "type": "float"
          },
          "Hispanic": {
            "type": "float"
          },
          "White / Caucasian": {
            "type": "float"
          },
          "african": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "asian": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "hispanic": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "white": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      },
      "gender": {
        "properties": {
          "female": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "male": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      },
      "genders": {
        "properties": {
          "FEMALE": {
            "type": "float"
          },
          "MALE": {
            "type": "float"
          }
        }
      },
      "gendersPerAge": {
        "properties": {
          "13-17": {
            "properties": {
              "FEMALE": {
                "type": "float"
              },
              "MALE": {
                "type": "float"
              }
            }
          },
          "18-24": {
            "properties": {
              "FEMALE": {
                "type": "float"
              },
              "MALE": {
                "type": "float"
              }
            }
          },
          "25-34": {
            "properties": {
              "FEMALE": {
                "type": "float"
              },
              "MALE": {
                "type": "float"
              }
            }
          },
          "35-44": {
            "properties": {
              "FEMALE": {
                "type": "float"
              },
              "MALE": {
                "type": "float"
              }
            }
          },
          "45-64": {
            "properties": {
              "FEMALE": {
                "type": "float"
              },
              "MALE": {
                "type": "float"
              }
            }
          }
        }
      }
    }
  }

在您的映射中,audience.ethnicity.asian 字段是文本类型,因此范围查询进行的是 词汇范围比较 而不是 数值范围比较.

在数值上,123 > 50,但在词汇上,123 < 50,因为 ASCII 字符 1 在 5 之前,这正是您遇到的问题。

我不确定您是否可以使用 audience.ethnicity.Asian 字段,它是一个浮点数。如果不是,您需要将 audience.ethnicity.asian 的映射更改为数字类型(integerfloat 等)并重新索引您的数据。