【问题标题】:Elastic Search: including #/hashtags in search results弹性搜索:在搜索结果中包含 #/hashtags
【发布时间】:2015-03-19 23:26:35
【问题描述】:

使用弹性搜索的查询 DSL,这是我目前构建查询的方式:

    elastic_sort = [
        { "timestamp": {"order": "desc" }},
        "_score",
        { "name": { "order": "desc" }},
        { "channel": { "order": "desc" }},
    ]

    elastic_query = {
    "fuzzy_like_this" : {
            "fields" : [ "msgs.channel", "msgs.msg", "msgs.name" ],
            "like_text" : search_string,
            "max_query_terms" : 10,
            "fuzziness": 0.7,
        }
    }

    res = self.es.search(index="chat", body={
        "from" : from_result, "size" : results_per_page,
        "track_scores": True,
        "query": elastic_query,
        "sort": elastic_sort,
    })

我一直在尝试实现允许在搜索中包含“#”的过滤器或分析器(我希望搜索“#thing”以返回包含“#thing”的结果),但我是短缺。我收到的错误消息没有帮助,只是告诉我我的查询格式不正确。

我试图合并此处找到的方法:http://www.fullscale.co/blog/2013/03/04/preserving_specific_characters_during_tokenizing_in_elasticsearch.html,但它在上下文中对我没有任何意义。

有人知道我该怎么做吗?

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    您是否为索引创建了映射?您可以在映射中指定不分析某些字段。

    例如,推文映射可以是:

    "tweet": {
      "properties": {
        "id": {
          "type": "long"
        },
        "msg": {
          "type": "string"
        },
        "hashtags": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
    

    然后,您可以对“hashtags”执行术语查询,以获得精确的字符串匹配,包括“#”字符。

    如果您也想对“hashtags”进行标记,您可以随时为“hashtags”创建一个多字段。

    【讨论】:

      猜你喜欢
      • 2012-08-23
      • 2015-09-11
      • 2021-03-11
      • 1970-01-01
      • 2022-07-07
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多