全文搜索与elasticsearch

时间:2015-10-16 15:20:40

标签: elasticsearch

刚刚开始使用elasticsearch。并坚持第一个问题:

我的es索引中有以下字符串:

"收藏夹&#34 ;, "收藏夹\ A1"

如果我尝试仅使用

进行搜索
'query' => [
    'match' => [
        'name' => 'favourites'
    ]
],

'filtered' => [
    'filter' => [
        'term' => [
            'name' => 'favourites'
        ],
    ]
]

两者都匹配。怎么解决?

1 个答案:

答案 0 :(得分:2)

尝试在地图中使用"index": "not_analyzed",在查询中使用term filter(或term query)。

例如,我可以设置一个这样的简单索引:

PUT /test_index
{
    "mappings": {
        "doc": {
            "properties": {
                "name": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}

添加几个文档

POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"name": "favourites"}
{"index":{"_id":2}}
{"name": "favourites\\a1"}

然后此查询将仅返回第一个文档:

POST /test_index/_search
{
   "query": {
      "term": {
         "name": {
            "value": "favourites"
         }
      }
   }
}
...
{
   "took": 45,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.30685282,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.30685282,
            "_source": {
               "name": "favourites"
            }
         }
      ]
   }
}

以下是我用于快速测试的一些代码:

http://sense.qbox.io/gist/accb3e9aedc43144a30bb96fd483115427c6c441