elasticsearch - Elasticsearch字词查询不匹配的URL

标签 elasticsearch elasticsearch-net

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

}

我猜问题出在分析器和分词器中,但我无法解决。任何形式的帮助都可以增进我的知识,并且可以帮助我寻求解决方案。
提前致谢。

最佳答案

您有正确的想法,但看来您的设置请求中有些小错误使您误入歧途。这是最终的索引请求:

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

注意在映射中添加的url_test类型。这使ES知道您的映射适用于此文档类型。另外,settingsmappings也是根对象的不同键,因此必须分开。因为您的初始设置请求格式错误,所以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"
            }
         }
      ]

关于elasticsearch - Elasticsearch字词查询不匹配的URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37326126/

相关文章:

elasticsearch - NEST Fluent DSL查询某些URL字段

docker - 将Elasticsearch日志迁移到其他集群

elasticsearch - 为什么Elasticsearch的Nest低层搜索方法会忽略SearchDescriptor <>()对象中定义的类型和索引名称

elasticsearch - 搜索多个索引时,inner_hits 不适用于嵌套过滤器

c# - ElasticSearch:写入后立即查询数据

elasticsearch - 当天时间弹性查询

elasticsearch - 将 elasticsearch 日期范围查询与空值结合起来

Elasticsearch:如何将 "nested query"添加到(嵌套)过滤器中

elasticsearch - 使用 NEST 和属性映射在 ElasticSearch 中设置路由

asp.net - 如何防止索引字段?