/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.index.query;

import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;

/**
 *
 */
public class SpanNotQueryBuilder extends BaseQueryBuilder implements SpanQueryBuilder, BoostableQueryBuilder<SpanNotQueryBuilder> {

    private SpanQueryBuilder include;

    private SpanQueryBuilder exclude;

    private int dist = -1;

    private int pre = -1;

    private int post = -1;

    private float boost = -1;

    private String queryName;

    public SpanNotQueryBuilder include(SpanQueryBuilder include) {
        this.include = include;
        return this;
    }

    public SpanNotQueryBuilder exclude(SpanQueryBuilder exclude) {
        this.exclude = exclude;
        return this;
    }

    public SpanNotQueryBuilder dist(int dist) {
        this.dist = dist;
        return this;
    }

    public SpanNotQueryBuilder pre(int pre) {
        this.pre = pre;
        return this;
    }

    public SpanNotQueryBuilder post(int post) {
        this.post = post;
        return this;
    }

    public SpanNotQueryBuilder boost(float boost) {
        this.boost = boost;
        return this;
    }

    /**
     * Sets the query name for the filter that can be used when searching for matched_filters per hit.
     */
    public SpanNotQueryBuilder queryName(String queryName) {
        this.queryName = queryName;
        return this;
    }

    @Override
    protected void doXContent(XContentBuilder builder, Params params) throws IOException {
        if (include == null) {
            throw new ElasticsearchIllegalArgumentException("Must specify include when using spanNot query");
        }
        if (exclude == null) {
            throw new ElasticsearchIllegalArgumentException("Must specify exclude when using spanNot query");
        }

        if ((dist != -1 && (pre != -1 || post != -1)) || (pre != -1 && post == -1) || (pre == -1 && post != -1))  {
            throw new ElasticSearchIllegalArgumentException("spanNot can either use [dist] or [pre] & [post] (or none)");
        }

        builder.startObject(SpanNotQueryParser.NAME);
        builder.field("include");
        include.toXContent(builder, params);
        builder.field("exclude");
        exclude.toXContent(builder, params);
        if (dist != -1) {
            builder.field("dist", dist);
        }
        if (pre != -1) {
            builder.field("pre", pre);
        }
        if (post != -1) {
            builder.field("post", post);
        }
        if (boost != -1) {
            builder.field("boost", boost);
        }
        if (queryName != null) {
            builder.field("_name", queryName);
        }
        builder.endObject();
    }
}

@@ -1119,16 +1119,59 @@
 der%3E %7B%0A%0A
+    public static final int NOT_SET = -1;%0A%0A
     priv
@@ -1259,26 +1259,31 @@
  int dist = 
--1
+NOT_SET
 ;%0A%0A    priva
@@ -1295,18 +1295,23 @@
 t pre = 
--1
+NOT_SET
 ;%0A%0A    p
@@ -1324,26 +1324,31 @@
  int post = 
--1
+NOT_SET
 ;%0A%0A    priva
@@ -1364,18 +1364,23 @@
 boost = 
--1
+NOT_SET
 ;%0A%0A    p
@@ -1826,19 +1826,35 @@
 s.pre = 
+(
 pre
+ %3E=0) ? pre : 0
 ;%0A      
@@ -1943,20 +1943,38 @@
 .post = 
+(
 post
+ %3E= 0) ? post : 0
 ;%0A      
@@ -2214,16 +2214,75 @@
 er hit.%0A
+     * @param queryName The query name%0A     * @return this%0A
      */%0A
@@ -2825,17 +2825,16 @@
      if 
-(
 (dist !=
@@ -2830,26 +2830,31 @@
 if (dist != 
--1
+NOT_SET
  && (pre != 
@@ -2853,18 +2853,23 @@
 (pre != 
--1
+NOT_SET
  %7C%7C post
@@ -2876,75 +2876,21 @@
  != 
--1)) %7C%7C (pre != -1 && post == -1) %7C%7C (pre == -1 && post != -1)) 
+NOT_SET))
  %7B%0A
+ 
     
@@ -2914,17 +2914,17 @@
  Elastic
-S
+s
 earchIll
@@ -3010,32 +3010,226 @@
 )%22);%0A        %7D%0A%0A
+        // set appropriate defaults%0A        if (pre != NOT_SET && post == NOT_SET) %7B%0A            post = 0;%0A        %7D else if (pre == NOT_SET && post != NOT_SET)%7B%0A            pre = 0;%0A        %7D%0A%0A
         builder.
@@ -3440,26 +3440,31 @@
 if (dist != 
--1
+NOT_SET
 ) %7B%0A        
@@ -3521,26 +3521,31 @@
  if (pre != 
--1
+NOT_SET
 ) %7B%0A        
@@ -3601,26 +3601,31 @@
 if (post != 
--1
+NOT_SET
 ) %7B%0A        
@@ -3688,18 +3688,23 @@
 oost != 
--1
+NOT_SET
 ) %7B%0A    

Use static int for easier to read code