【问题标题】:Elasticsearch: do exact searches where the query contains special characters like '#'Elasticsearch:在查询包含特殊字符(如“#”)的情况下进行精确搜索
【发布时间】:2015-11-24 19:31:11
【问题描述】:

只获取那些包含'#test'的文档的结果,并忽略elasticsearch中只包含'test'的文档

【问题讨论】:

    标签: elasticsearch search-engine django-haystack


    【解决方案1】:

    人们可能会对这个问题抱怨你,所以我会注意到这是对我对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

    【讨论】:

    • 解释得很漂亮。谢谢。
    猜你喜欢
    • 2016-03-14
    • 2021-10-11
    • 2020-10-09
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多