【问题标题】:ElasticSearch search for part of urlElasticSearch 搜索部分 url
【发布时间】:2017-04-17 09:41:00
【问题描述】:

我正在使用 ElasticSearch 5,但找不到以下解决方案: 我想在文档中搜索带有斜杠(url 的一部分)的字符串。但它不会返回匹配的文档。 我读过一些带有斜线的字符串被 ES 分割的东西,这不是我想要的这个字段。我尝试使用映射在字段上设置“not_analyzed”,但我似乎无法让它以某种方式工作。

“创建索引”: 把http://localhost:9200/test

{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "properties" : {
                "field1" : { "type" : "text","index": "not_analyzed" }
            }
        }
    }
}

“添加文档”:POST http://localhost:9200/test/type1/

{
    "field1" : "this/is/a/url/test"
}

“搜索文档”POST http://localhost:9200/test/type1/_search

{
    "size" : 1000,
    "query" : {
        "bool" : {
            "must" : [{
                    "term" : {
                        "field1" : {
                            "value" : "this/is/a/url/test"
                        }
                    }
                }
            ]
        }
    }
}

回复:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

“映射响应”:GET http://localhost:9200/test/_mapping?pretty

{
  "test": {
    "mappings": {
      "type1": {
        "properties": {
          "field1": {
            "type": "text"
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    使用term 查询获得完全匹配是正确的。但是,您的初始映射是错误的。

    "type" : "text", "index": "not_analyzed"
    

    应该是这个

    "type": "keyword"
    

    (注意:ES5 中的 keyword 类型等价于 ES 2.x 中的 not_analyzed string

    您需要删除索引并使用更正的映射重新创建它。然后您的term 查询将起作用。

    【讨论】:

      【解决方案2】:

      我怀疑您需要的是Match query,而不是条款查询。条款正在寻找单个“术语”/单词,并且不会使用分析器分解您的请求。

      {
          "size" : 1000,
          "query" : {
              "bool" : {
                  "must" : [{
                          "match" : {
                              "field1" :  "this/is/a/url/test"                            
                          }
                      }
                  ]
              }
          }
      }
      

      【讨论】:

      • 它看起来很有效,但是当我添加另一个文档时:{ "field1" : "dit/is/een/url/test/nog/een" } 也返回了,而不仅仅是完全匹配。
      • 根据您的需要,您有两种选择。如果 URL 将成为字符串的一部分,那么您仍然可以将 match 与“and”运算符一起使用。如果该字段仅包含 URL,则 @val 是正确的。我假设前一种情况。
      猜你喜欢
      • 2016-01-07
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多