Elasticsearch:执行精确搜索,其中查询包含特殊字符,如'#' [英] Elasticsearch: do exact searches where the query contains special characters like '#'

查看:13620
本文介绍了Elasticsearch:执行精确搜索,其中查询包含特殊字符,如'#'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只得到包含'#test'的那些文档的结果,并忽略弹性搜索中只包含'test'的文档。

Get the results of only those documents which contain '#test' and ignore the documents that contain just 'test' in elasticsearch

推荐答案

人们可能会抱怨这个问题,所以我会注意到,这是针对我对这篇文章的评论< a>。

People may gripe at you about this question, so I'll note that it was in response to my comment on this post.

你可能想要阅读 Elasticsearch 中的分析,以及匹配查询术语查询

You're probably going to want to read up on analysis in Elasticsearch, as well as match queries versus term queries.

无论如何,这里的惯例是在字符串字段上使用 .raw 子字段。这样,如果要进行涉及分析的搜索,可以使用基本字段,但如果要搜索精确(未分析)值,则可以使用子字段。

Anyway, the convention here is to use a .raw sub-field on a string field. That way, if you want to do searches involving analysis, you can use the base field, but if you want to search for exact (un-analyzed) values, you can use the sub-field.

所以这里是一个完成这个的简单映射:

So here is a simple mapping that accomplishes this:

PUT /test_index
{
   "mappings": {
      "doc": {
         "properties": {
            "post_text": {
               "type": "string",
               "fields": {
                  "raw": {
                     "type": "string",
                     "index": "not_analyzed"
                  }
               }
            }
         }
      }
   }
}

现在,如果我添加这两个文件:

Now if I add these two documents:

PUT /test_index/doc/1
{
    "post_text": "#test"
}

PUT /test_index/doc/2
{
    "post_text": "test"
}

A match查询y对基地将返回:

A "match" query against the base field will return both:

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查询下面只会返回一个:

But the "term" query below will only return the one:

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"
            }
         }
      ]
   }
}

这是我用来测试的代码它:

Here is the code I used to test it:

http://sense.qbox.io/gist/2f0fbb38e2b7608019b5b21ebe05557982212ac7

这篇关于Elasticsearch:执行精确搜索,其中查询包含特殊字符,如'#'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆