Python ElasticSearch查询错误

时间:2017-02-21 04:26:38

标签: python elasticsearch

我已经使用映射查询在ElasticSearch中执行搜索,它可以正常工作,如下所示。

// Variable to indicate whether the most recently generated image
// was valid.
window.indicator = true;

// Give the file base-name and index as stored in the local address.
// Return the corresponding Image() object.
function initImg(name, ind) {
    var img = new Image();
    // The file is local, the image is always of the form
    // baseName_i.png
    window.indicator = true;
    img.src = name + "_" + ind + ".png";
    img.onerror = function() {
        // Find the appropriate canvas state element, and 
        // update its state back to 1.
        for (i = 0; i < canStates.length; i++) {
            var n = canStates[i][0].getAttribute("id");
            n = n.split("_")[1];
            if (name == canStates[i][0].getAttribute("id")) {
                canStates[i][1] = 1;
            }

            window.indicator = false;
        }
    };
    return img;
}

// Give the canvas context and image objects.  Draw the image to
// the context, no return value.
function draw(ctx, img) {
    // Check that the image is loaded before writing.  Keep
    // checking every 50 milliseconds.
    if (!img.complete) {
        setTimeout( function() {
            draw(ctx, img);
        }, 50);
    }

    // Clear the current image and draw the new.
    ctx.clearRect(0,0, 200,200);
    ctx.drawImage(img, 0,0, 200,200);
}

// Give the string canvas id and string base-name, create the
// canvas object and draw the first image to the canvas.
function slideShow(canId, name) {
    var can = document.getElementById(canId);
    can.width = 300;
    can.height = 300;
    var ctx = can.getContext('2d');
    var img = initImg(name, 1);
    draw(ctx, img);
}

// Next button function.  Give the name of the canvas, draw the
// next image or cycle to the start.
function next(button) {
    var name = button.getAttribute("name");
    // Find the appropriate canvas element.
    for (i = 0; i < canStates.length; i++) {
        var id = canStates[i][0].getAttribute("id");
        id = id.split("_")[1];
        if (id == name) {
            // Use the states to produce an image, and update the
            // states.
            canStates[i][1] += 1;
            var img = initImg(name, canStates[i][1]);
            if (!window.indicator) {
                img = initImg(name, 1);
            }
            // Draw to the canvas.
            draw(canStates[i][0].getContext('2d'),img);
        }
    }


}

// Create a global variable tracking all states of "Next" buttons.
// Stored as a list, each element is a list, the left coordinate is
// a canvas and the right coordinate is its state (image index).
// Also initialize all slide shows.
// The variable r stores the canvases and states, initialized
// outside the function in order to pass-by-reference so as to act
// as a global variable.
var canStates = new Array();
window.onload = function() {
    var cans = document.getElementsByTagName("canvas");

    for (i=0; i < cans.length; i++) {
        var c = cans[i];
        var n = c.getAttribute("id").split("_")[1];
        slideShow("can_"+n, n);
    }

    for (i = 0; i < cans.length; i++) {
        canStates[i] = [cans[i],1];
    }
}

但是,即使我添加&#34;术语&#34;,我收到的错误:TransportError(400,u&#39; parsing_exception&#39;,u&#39; [term]格式错误的查询,预计[END_OBJECT]但找到[FIELD_NAME]&#39;)

{
                "query": {
                    "bool" : {
                        "must" : {
                            "match_all" : {}
                        },
                        "filter" : {
                            "geo_distance" : {
                                "distance" : "{}mi".format(list_info.radius_b),
                                'location': {
                                    "lat": zip.lat,
                                    "lon": zip.lng
                                }
                            }
                        },
                    },
                },
                "sort" : [
                    {
                        "_geo_distance" : {
                            'location': {"lat": zip.lat,
                                        "lon": zip.lng},
                            "order" : "asc",
                            "unit" : "mi",
                            "mode" : "min",
                            "distance_type" : "sloppy_arc"
                        }
                    }
                ],
                "from": 0,
                "size": 0,
            }

1 个答案:

答案 0 :(得分:3)

您的新term查询必须位于bool/filter查询内:

{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": [
        {
          "geo_distance": {
            "distance": "{}mi".format(list_info.radius_b),
            "location": {
              "lat": zip.lat,
              "lon": zip.lng
            }
          }
        },
        {
          "term": {
            "status": "approved"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": zip.lat,
          "lon": zip.lng
        },
        "order": "asc",
        "unit": "mi",
        "mode": "min",
        "distance_type": "sloppy_arc"
      }
    }
  ],
  "from": 0,
  "size": 0
}