【问题标题】:Elasticsearch: Faceted query with terms returning unexpected resultElasticsearch:带有返回意外结果的术语的分面查询
【发布时间】:2013-04-04 13:26:26
【问题描述】:

我正在尝试对存储在 ES 中的一些日志运行分面查询。日志看起来像

{"severity": "informational","message_hash_value": "00016B15", "user-agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1", "host": "192.168.8.225", "version": "1.0", "user": "User_1@test.co", "created_timestamp": "2013-03-01T15:34:00", "message": "User viewed contents", "inserted_timestamp": "2013-03-01T15:34:00"}

我尝试运行的查询是

curl -XGET 'http://127.0.0.1:9200/logs-*/logs/_search' 
-d {"from":0, "size":0, 
    "facets" : { 
         "user" : { 
            "terms" : {"field" : "user", "size" : 999999 } } } }

请注意,日志中的字段 "user" 是一个电子邮件地址。现在的问题是,我使用的terms-facet 搜索查询会返回来自用户字段的术语列表,如下所示。

u'facets': {u'user': {u'_type': u'terms', u'total': 2004, u'terms': [{u'count': 1002,u'term': u'test.co'}, {u'count': 320, u'term': u'user_1'}, {u'count': 295,u'term': u'user_2'}

请注意,该列表包含term

{u'count': 1002,u'term': u'test.co'}

这是用户电子邮件地址的域名。为什么 elasticsearch 将域视为一个单独的术语?

运行查询以检查映射

curl -XGET 'http://127.0.0.1:9200/logs-*/_mapping?pretty=true'

"user" 字段生成以下内容

"user" : {
      "type" : "string"
    },

【问题讨论】:

    标签: elasticsearch faceted-search facets


    【解决方案1】:

    发生这种情况是因为 elasticsearch 的默认全局分析器在索引时标记了“@”(除了空格和标点符号之外)。您可以通过告诉 elasticsearch 不要在该字段上运行分析器来解决此问题,但您必须重新索引所有数据。

    创建新索引

    curl -XPUT 'http://localhost:9200/logs-new'
    

    在此新索引的映射中指定您不想分析“用户”字段

    curl -XPUT 'http://localhost:9200/logs-new/logs/_mapping' -d '{
        "logs" : {
            "properties" : {
                "user" : {
                    "type" : "string", 
                    "index" : "not_analyzed"
                }
            }
        }
    }'
    

    索引文档

    curl -XPOST 'http://localhost:9200/logs-new/logs' -d '{
        "created_timestamp": "2013-03-01T15:34:00", 
        "host": "192.168.8.225", 
        "inserted_timestamp": "2013-03-01T15:34:00", 
        "message": "User viewed contents", 
        "message_hash_value": "00016B15", 
        "severity": "informational", 
        "user": "User_1@test.co", 
        "user-agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1", 
        "version": "1.0"
    }'
    

    elasticsearch facet 现在将显示整个电子邮件地址

    curl -XGET 'http://localhost:9200/logs-new/logs/_search?pretty' -d '{
        "from":0, 
        "size":0, 
        "facets" : { 
             "user" : { 
                "terms" : {
                    "field" : "user", 
                    "size" : 999999 
                }
            } 
        }
    }'
    

    结果:

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
      },
      "hits" : {
        "total" : 1,
        "max_score" : 1.0,
        "hits" : [ ]
      },
      "facets" : {
        "user" : {
          "_type" : "terms",
          "missing" : 0,
          "total" : 1,
          "other" : 0,
          "terms" : [ {
            "term" : "User_1@test.co",
            "count" : 1
          } ]
        }
      }
    }
    

    参考资料: 核心类型:http://www.elasticsearch.org/guide/reference/mapping/core-types/ 使用新映射重新索引:https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/tCaXgjfUFVU

    【讨论】:

    • 优秀的答案。 +1 用于参考和重新索引。干杯
    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多