【问题标题】:elastic search is not returning record for nearby input date弹性搜索未返回附近输入日期的记录
【发布时间】:2020-11-11 16:22:01
【问题描述】:

下面是索引定义和带有查询的示例记录

***正在寻找匹配名称为“aaa”且日期从 2020-01-01 开始的记录,所以我不应该得到任何记录,因为弹性搜索中没有日期记录 >2020-01-05 *** 但是,通过以下输入,我仍然从弹性搜索中获取记录,就好像它只考虑名称字段查询一样。

创建索引 邮政 http://localhost:9200/gauss_index1/date_format

  "mappings": {
    "properties": {
      "date": {
        "type":   "date",
        "format": "yyyy-MM-dd"
      },
      "name":{
        "type":   "text"
          
      }
    }
  }
}Add records : 
PUT http://localhost:9200/gauss_index1/date_format/1
{

      "date": "2019-01-05",
      "name": "aaa"
}
PUT http://localhost:9200/gauss_index1/date_format/2
{

      "date": "2019-02-05",
      "name": "bbb"
}

PUT http://localhost:9200/gauss_index1/date_format/3
{
      "date": "2019-03-05",
      "name": "ccc"
}

Search 
POST http://localhost:9200/gauss_index1/date_format/_search

 {
    "query": {
        "function_score": {
            "functions": [
                {
                    "gauss": {
                        "date": {
                            "origin": "2020-03-01",
                            "scale": "10d",
                            "offset": "5d"
                        }
                    }
                }
            ],
            "query": {
                "multi_match": {
                    "query": "aaa"
                    "fields": [
                        "name"
                    ]
                }
            }
        }
    }
}


 **Returns only record matching with name aaa but as mentioned in gauss origin is march 1 onwards needs to be searched 
 but I am getting records of only conditin matching name equals "aaa"
 

【问题讨论】:

  • 高斯函数不会排除文档 1,而只是简单地给它打分,因为它的日期超出了定义的高斯边界。这就是功能分数查询的工作方式。你到底想达到什么目的?
  • 例如在 Elastisearch db 中添加了“日期”为 2019-01-05,2019-02-05 的多个文档,因此需要找到下一个日期,例如对于查询输入 2019-01-06 和名称为“bbb”,当我按 10 天缩放时,我应该得到 id 为 2 的文档。但在这里我得到了我添加的所有记录。此外,我必须继续增加日期范围,直到获得最接近的匹配日期,即在查询中配置偏移量 5 的原因

标签: elasticsearch elasticsearch-5 elasticsearch-query


【解决方案1】:

正如@Val 指出的那样,分配的分数将为0 - 您可以通过使用?explain=true 运行查询来验证这一点。

如果您确实想严格排除此类文档,请应用范围过滤器:

{
  "query": {
    "function_score": {
      "functions": [
        {
          "gauss": {
            ...
          }
        }
      ],
      "query": {
        "bool": {
          "must": [
            {
              "range": {                     <-------
                "date": {
                  "gte": "2020-03-01",
                  "format": "yyyy-MM-dd"
                }
              }
            },
            {
              "multi_match": {
                ...
              }
            }
          ]
        }
      }
    }
  }
}

【讨论】:

  • 我需要在弹性搜索中逐步进行日期搜索。例如我从 2 月 1 日开始,然后 es 应该搜索文档直到 2 月 10 日,如果没有找到,那么应该添加 5 天的偏移量,即 2 月 15 日,同样直到我得到至少 1 个结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 2019-03-08
  • 1970-01-01
  • 2016-11-05
相关资源
最近更新 更多