【问题标题】:Filter using Range-Elasticsearch使用 Range-Elasticsearch 过滤
【发布时间】:2019-07-12 20:36:31
【问题描述】:

下面是单行的kibana JSON,

{
  "_index": "questionanswers",
  "_type": "doc",
  "_id": "3",
  "_version": 1,
  "_score": 0,
  "_source": {
    "question": {
      "id": 3,
      "text": "Your first salary",
      "answer_type": "FL",
      "question_type": "BQ"
    },
    "candidate": {
      "id": 13
    },
    "job": {
      "id": 6
    },
    "id": 3,
    "status": "AN",
    "answered_on": "2019-07-12T09:26:01+00:00",
    "answer": "12222222"
  },
  "fields": {
    "answered_on": [
      "2019-07-12T09:26:01.000Z"
    ]
  }
}

我有一个类似的 sql 查询,

Select * from questionanswers where question.id = 3 and answer between 1250 and 1253666

我已将其转换为 elasticsearch 查询,如下所示,

{
    "size": 1000,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "question.id":3
                    }
                },
                {
                    "range": {
                        "answer": {
                            "from": 1250,
                            "to": 1253666999,
                            "include_lower": true,
                            "include_upper": true,
                            "boost": 1
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    }
}

这里 answer 被声明为 String ,但我持有 Date、Float 和 String 值。

 "question": {
      "id": 3,
      "text": "Your first salary",
      "answer_type": "FL",
      "question_type": "BQ"
    },

这里answer_type 告诉它期望的答案类型。

当我尝试运行此查询时,我没有得到想要的结果。我对这次打击得到了空洞的回应。 但实际上,有一行满足这个查询。 我的 elasticsearch 查询应该如何过滤,以便我可以使用

question.id = 3 , question.answer_type = "FL" and answer between 1250 and 1253666```

【问题讨论】:

    标签: python elasticsearch kibana


    【解决方案1】:

    再次查看您的文档。 answer 是一个字符串值,您在查询中将其视为number。所以它显然不起作用。

    将此字段的mapping 更改为数字。

    这是我在测试索引中编入索引的文档,然后再次运行您的查询,它可以正常工作

    为文档编制索引(见answer字段)

    POST /so-index4/_doc/1
    {
       "question": {
          "id": 3,
          "text": "Your first salary",
          "answer_type": "FL",
          "question_type": "BQ"
        },
        "candidate": {
          "id": 13
        },
        "job": {
          "id": 6
        },
        "id": 3,
        "status": "AN",
        "answered_on": "2019-07-12T09:26:01+00:00",
        "answer": 12222222,
      "fields": {
        "answered_on": [
          "2019-07-12T09:26:01.000Z"
        ]
      }
    }
    

    和查询(与您在上面提供的查询相同)

    GET /so-index4/_search
    {
        "size": 1000,
        "query": {
            "bool": {
                "must": [
                    {
                        "term": {
                            "question.id":3
                        }
                    },
                    {
                        "range": {
                            "answer": {
                                "from": 1250,
                                "to": 1253666999,
                                "include_lower": true,
                                "include_upper": true,
                                "boost": 1
                            }
                        }
                    }
                ],
                "adjust_pure_negative": true,
                "boost": 1
            }
        }
    }
    

    结果

    {
      "took" : 5,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 2.0,
        "hits" : [
          {
            "_index" : "so-index4",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 2.0,
            "_source" : {
              "question" : {
                "id" : 3,
                "text" : "Your first salary",
                "answer_type" : "FL",
                "question_type" : "BQ"
              },
              "candidate" : {
                "id" : 13
              },
              "job" : {
                "id" : 6
              },
              "id" : 3,
              "status" : "AN",
              "answered_on" : "2019-07-12T09:26:01+00:00",
              "answer" : 12222222,
              "fields" : {
                "answered_on" : [
                  "2019-07-12T09:26:01.000Z"
                ]
              }
            }
          }
        ]
      }
    }
    

    【讨论】:

    • answer 只是一个在 String 中声明的变量,但它包含 Date、String、Float 数据类型您可能已经注意到有一个字段“answer_type”告诉它期望的答案类型
    • 有了这个`"answer": "12222222"`你不能用number查询,除非有某种分析器或其他方式
    • 我已经修改了问题以便更好地理解,基本上我正在寻找过滤条件,question.id = 3,question.answer_type = "FL" 并且答案在 1250 和 1253666 之间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 2012-08-30
    • 2016-12-04
    • 1970-01-01
    相关资源
    最近更新 更多