【发布时间】:2015-11-24 19:31:11
【问题描述】:
只获取那些包含'#test'的文档的结果,并忽略elasticsearch中只包含'test'的文档
【问题讨论】:
标签: elasticsearch search-engine django-haystack
只获取那些包含'#test'的文档的结果,并忽略elasticsearch中只包含'test'的文档
【问题讨论】:
标签: elasticsearch search-engine django-haystack
人们可能会对这个问题抱怨你,所以我会注意到这是对我对this post的评论的回应。
您可能想要阅读analysis in Elasticsearch,以及match queries 与term queries。
无论如何,这里的约定是在字符串字段上使用.raw 子字段。这样,如果您想进行涉及分析的搜索,您可以使用基本字段,但如果您想搜索精确(未分析的)值,您可以使用子字段。
所以这里有一个简单的映射来完成这个:
PUT /test_index
{
"mappings": {
"doc": {
"properties": {
"post_text": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
现在如果我添加这两个文件:
PUT /test_index/doc/1
{
"post_text": "#test"
}
PUT /test_index/doc/2
{
"post_text": "test"
}
针对基本字段的"match" 查询将返回两者:
POST /test_index/_search
{
"query": {
"match": {
"post_text": "#test"
}
}
}
...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.5945348,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.5945348,
"_source": {
"post_text": "#test"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 0.5945348,
"_source": {
"post_text": "test"
}
}
]
}
}
但下面的"term" 查询只会返回一个:
POST /test_index/_search
{
"query": {
"term": {
"post_text.raw": "#test"
}
}
}
...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"post_text": "#test"
}
}
]
}
}
这是我用来测试它的代码:
http://sense.qbox.io/gist/2f0fbb38e2b7608019b5b21ebe05557982212ac7
【讨论】: