首页 文章

ElasticSearch搜索部分网址

提问于
浏览
1

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

"Create index":放http://localhost:9200/test

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

"Add document":POST http://localhost:9200/test/type1/

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

"Search document" 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": []
  }
}

"The mapping response":GET http://localhost:9200/test/_mapping?pretty

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

2 回答

  • 1

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

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

    应该是这样

    "type": "keyword"
    

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

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

  • 1

    我怀疑你需要的是Match query,而不是条款查询 . 条款正在寻找单个"term" / word,并且不会使用分析器来破坏您的请求 .

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

相关问题