【问题标题】:Range filter not working for "gt" operator but working for "lt"范围过滤器不适用于“gt”运算符但适用于“lt”
【发布时间】:2018-11-22 08:42:11
【问题描述】:

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

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"
              }
            }
          }
        }
      }
    }
  }

【问题讨论】:

  • 我很想看看你的真实映射,即你在运行curl -XGET localhost:9200/your_index 时得到的映射。请分享
  • @Val - 我对弹性搜索的了解有限。你是说我在 Kibana 上运行的查询吗?
  • 在 Kibana / Dev Tools 中,您可以运行:GET your_index(将your_index 替换为真实的索引名称)
  • 完成。但结果是一个巨大的物体。您需要哪些特定部件/属性?
  • @Val 用映射更新了原始问题

标签: elasticsearch indexing kibana


【解决方案1】:

在您的映射中,audience.ethnicity.asian 字段属于文本类型,因此范围查询正在执行词法范围比较,而不是数字范围比较。 p>

数字上是 123 > 50,但词法上是 123

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

【讨论】:

  • 非常感谢您的解释。有没有一种简单的方法可以更改映射?
  • 您无法更改映射,您需要使用正确的映射创建一个新索引,并将您的数据重新索引到该新索引中。
  • 是的。完全有效。使用正确的映射重新索引。谢谢。抱歉,这几天生病了,无法将您的答案标记为正确。
猜你喜欢
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
  • 2013-04-17
  • 2020-08-05
  • 1970-01-01
相关资源
最近更新 更多