elasticsearch - elasticsearch “having not”查询

标签 elasticsearch

一些文档具有类别字段。.其中一些文档具有类别字段,其值等于“-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"
               }
            }
         }
      }
   }
}

最佳答案

如果您的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"
              }
            }
          }
        }
      ]
    }
  }
}

关于elasticsearch - elasticsearch “having not”查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33015533/

相关文章:

multithreading - Elasticsearch 即使在满载情况下也只使用一半的 cpu 内核

elasticsearch - 如何检查 ElasticSearch BulkProcessor 服务是否存在

redis - Logstash、elasticsearch、Kibana,包括IP

sql - Elasticsearch 查询过滤器

ruby-on-rails - 查询查询结果错误

Spring 中的 ElasticSearch 与 @Query

elasticsearch - 安装Kopf-Elasticsearch

python - 使用 elastic4s 查询产生零结果

javascript - 如何在elasticsearch中合并范围和多重匹配查询

elasticsearch - 使Elasticsearch Aggregation无法正常工作