范围过滤器不适用于"gt".运算符,但为"lt"工作 [英] Range filter not working for "gt" operator but working for "lt"

查看:61
本文介绍了范围过滤器不适用于"gt".运算符,但为"lt"工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using elastic search to index my documents and wanted to filter documents based on a specific property. Here is my code:

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

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

It doesn't work with gt operator (sends back results that are not consistent) but works with lt operator

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

这令人惊讶.我已经仔细检查过,属性值是一个整数.我尝试了许多不同的方法,但似乎无法弄清我想念的是什么. 映射:

This works surprisingly. I have double checked that the property value IS an integer. I have tried many different ways but can't seem to figure out what am I missing. Mapping:

  "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字段为文本类型,因此范围查询正在执行词法范围比较,而不是数字范围比较.

In your mapping, the audience.ethnicity.asian field is of type text, hence the range query is doing a lexical range comparison instead of a numerical range comparison.

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

Numerically, 123 > 50, but lexically 123 < 50 because ASCII character 1 comes before 5, that's exactly the issue you're suffering from here.

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

I'm not sure if you can use the audience.ethnicity.Asian field, which is a float. If not, you'll need to change the mapping of audience.ethnicity.asian to a numeric type (integer, float, etc) and reindex your data.

这篇关于范围过滤器不适用于"gt".运算符,但为"lt"工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆