【问题标题】:elasticsearch "having not" queryelasticsearch“没有”查询
【发布时间】:2015-10-08 20:35:14
【问题描述】:

一些文档具有类别字段。其中一些文档具有类别字段,其值等于“-1”。我需要一个查询返回具有类别字段且“不等于-1”的文档。

我试过这个:

GET webproxylog/_search
{
  "query": {
    "filtered": {

      "filter": {
        "not":{
          "filter": {"and": {
            "filters": [
              {"term": {
                "category": "-1"
              }

              },
              {
                "missing": {
                "field": "category"
                }
              }
            ]
          }}
        }
      }
    }
  }
}

但不起作用..返回文档没有“类别字段”

编辑

映射:

    {
   "webproxylog": {
      "mappings": {
         "accesslog": {
            "properties": {
               "category": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "clientip": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "clientmac": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "clientname": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "duration": {
                  "type": "long"
               },
               "filetype": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "hierarchycode": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "loggingdate": {
                  "type": "date",
                  "format": "dateOptionalTime"
               },
               "reqmethod": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "respsize": {
                  "type": "long"
               },
               "resultcode": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "url": {
                  "type": "string",
                  "analyzer": "slash_analyzer"
               },
               "user": {
                  "type": "string",
                  "index": "not_analyzed"
               }
            }
         }
      }
   }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    如果您的 category 字段是 string 并且默认被分析,那么您的 -1 将被索引为 1(去除 minus 符号)。

    您需要将该字段设为not_analyzed 或添加未分析的子字段(如下面的解决方案)。

    类似这样的:

    DELETE test
    
    PUT /test
    {
      "mappings": {
        "test": {
          "properties": {
            "category": {
              "type": "string",
              "fields": {
                "notAnalyzed": {
                  "type": "string",
                  "index": "not_analyzed"
                }
              }
            }
          }
        }
      }
    }
    
    POST /test/test/1
    {"category": "-1"}
    POST /test/test/2
    {"category": "2"}
    POST /test/test/3
    {"category": "3"}
    POST /test/test/4
    {"category": "4"}
    POST /test/test/5
    {"category2": "-1"}
    
    GET /test/test/_search
    {
      "query": {
        "bool": {
          "must_not": [
            {
              "term": {
                "category.notAnalyzed": {
                  "value": "-1"
                }
              }
            },
            {
              "filtered": {
                "filter": {
                  "missing": {
                    "field": "category"
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 我将映射更改为 not_ananlyzed(已编辑问题)。但查询仍然返回没有类别字段的文档
    • 您是否在映射更改后重新索引文档?
    • 你用我的或你的查询测试了吗?
    • 是的,重新索引所有,我尝试了我们的两个查询。但不起作用..您的查询没有返回任何我的返回文档没有类别字段
    • 我觉得你说的是真的,让我等一分钟
    猜你喜欢
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 2018-02-10
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多