繁体   English   中英

使用范围弹性搜索进行过滤

[英]Filter using Range-Elasticsearch

以下是单行的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```

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

将此字段的mapping更改为number。

这是我在测试索引中建立索引并再次运行查询的文档,它可以正常工作

为文档编制索引 (请参阅字段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"
            ]
          }
        }
      }
    ]
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM