【问题标题】:Elastic Search Term Query Not Matching URL'sElasticsearch 术语查询不匹配 URL
【发布时间】:2016-05-19 14:22:18
【问题描述】:

我是 Elastic 搜索的初学者,从上周开始我正在研究 POC。 我的文档中有一个 URL 字段,其中包含以下格式的 URL:“http://www.example.com/foo/navestelre-04-cop”。

我无法定义到我的整个对象的映射,因为每个对象都有不同的键,除了 URL。

这是我创建索引的方式:

POST 
{
    "settings" : {
        "number_of_shards" : 5,
    "mappings" : {
            "properties" : {
                "url" : { "type" : "string","index":"not_analyzed" }
            }
    }
}
}

我将我的 URL 字段保持为 not_analyzed,因为我从一些资源中了解到,将字段标记为 not_analyzed 会阻止它进行标记化,因此我可以在术语查询中查找该字段的完全匹配。

我也尝试使用空白分析器作为 URL 值,因此没有任何空白字符。但是我又一次无法成功命中。

以下是我的术语查询:

{
"query":{
    "constant_score": {
       "filter": {
       "term": {
          "url":"http://www.example.com/foo/navestelre-04-cop"
       }
       }
    }
}

}

我猜问题出在分析器和标记器的某个地方,但我无法找到解决方案。任何形式的帮助都会极大地增强我的知识并帮助我找到解决方案。 提前致谢。

【问题讨论】:

  • 检查索引的映射是否正确。您用于创建索引的查询错误且不完整。
  • 或者,在你试图隐藏索引名和类型名时,你拿出了太多东西。完整的命令是PUT /my_index { "settings": { "number_of_shards": 5 }, "mappings": { "my_type": { "properties": { "url": { "type": "string", "index": "not_analyzed" } } } } }

标签: elasticsearch elasticsearch-net


【解决方案1】:

您的想法是对的,但您的设置请求中的一些小错误似乎让您误入歧途。这是最终的索引请求:

POST /test
{
    "settings": {
        "number_of_shards" : 5
    },                           
   "mappings": {
      "url_test": {
         "properties": {
            "url": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}

注意映射中添加的url_test 类型。这让 ES 知道您的映射适用于该文档类型。另外settingsmappings也是根对象的不同key,所以要分开。因为您的初始设置请求格式不正确,ES 只是忽略了它,并在您的文档上使用了标准分析器,这导致您无法使用查询来查询它。我指给你the ES Mapping docs

我们可以索引两个文档来测试:

POST /test/url_test/1
{
    "url":"http://www.example.com/foo/navestelre-04-cop"
}

POST /test/url_test/2
{
    "url":"http://stackoverflow.com/questions/37326126/elastic-search-term-query-not-matching-urls"
}

然后执行你未修改的搜索查询:

GET /test/_search
{
   "query": {
      "constant_score": {
         "filter": {
            "term": {
               "url": "http://www.example.com/foo/navestelre-04-cop"
            }
         }
      }
   }
}

产生这个结果:

"hits": [
         {
            "_index": "test",
            "_type": "url_test",
            "_id": "1",
            "_score": 1,
            "_source": {
               "url": "http://www.example.com/foo/navestelre-04-cop"
            }
         }
      ]

【讨论】:

  • 感谢@IanGabes.. 它解决了问题,我将其标记为答案....
  • 两年后的问候。不确定从什么时候开始,但目前有 UAX URL 电子邮件标记器。在过去,这将是这个问题的正确答案。 elastic.co/guide/en/elasticsearch/reference/current/…
猜你喜欢
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
  • 2021-05-03
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多