1
votes

Some documents has category fields.. Some of these docs has category fields its value equals to "-1". I need a query return documents which have category fields and "not equal to -1".

I tried this:

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

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

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

But not work.. returns docs not have "category field"

EDIT

Mapping:

    {
   "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"
               }
            }
         }
      }
   }
}
1

1 Answers

0
votes

If your category field is string and is analyzed by default, then your -1 will be indexed as 1 (stripping the minus sign).

You will need that field to be not_analyzed or to add a sub-field which is not analyzed (as my solution below).

Something like this:

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"
              }
            }
          }
        }
      ]
    }
  }
}