ElasticSearch错误:[term]查询不支持不同的字段名称,请改用[bool]查询

时间:2018-02-28 18:10:47

标签: javascript json elasticsearch lucene

我目前正在使用 ElasticSearch v2.3.2 ,并且在查询中调试错误时出现问题。该查询似乎工作正常,但它仍然抛出错误:

  

[query_parsing_exception] [term]查询不支持不同   字段名称,改为使用[bool]查询

我当前的ElasticSearch查询如下:

var must = [];

must.push({ not: { term: { 'zip': '00000' }}});

var request = {
    index: 'my-index',
    type: 'property',
    body: {
        from : page * perPage,
        size : perPage,
        query: {
            bool: {
                must,
                filter : [
                    {
                        geo_distance : {
                            distance : range + 'mi',
                            'position' : point
                        }    
                    },
                    {
                        "not" : {
                            "term" : {
                                    "listing.rentForSale" : "R",
                                    "listing.statusCode" : "Rented"
                            }
                        }
                    }
                ]

            }
        },
        sort: [{
            _geo_distance: {
                "position": point,
                "order":         "asc",
                "unit":          "mi",
                "distance_type": "plane"
            }
        }]
    }
};

我的初始查询在必须数组中包含“not”子句,如下所示:

var must = [];

must.push({ not: { term: { 'zip': '00000' }}});
must.push({ not: { term: { "listing.rentForSale" : "R" }}});
must.push({ not: { term: { "listing.statusCode" : "Rented" }}});

var request = {
    index: 'my-index',
    type: 'property',
    body: {
        from : page * perPage,
        size : perPage,
        query: {
            bool: {
                must,
                filter : [
                    {
                        geo_distance : {
                            distance : range + 'mi',
                            'position' : point
                        }    
                    }
                ]

            }
        },
        sort: [{
            _geo_distance: {
                "position": point,
                "order":         "asc",
                "unit":          "mi",
                "distance_type": "plane"
            }
        }]
    }
};

这没有产生正确的结果。将“not”子句移动到过滤器数组现在可以生成正确的搜索结果,但仍然会抛出此错误。我有什么想法可以清除这个错误吗?

2 个答案:

答案 0 :(得分:1)

正确的方法是使用bool/must_not

var request = {
    index: 'my-index',
    type: 'property',
    body: {
        from : page * perPage,
        size : perPage,
        query: {
            bool: {
                must_not: [
                    {
                        "term" : {
                                "listing.rentForSale" : "R"
                        }
                    },
                    {
                        "term" : {
                                "listing.statusCode" : "Rented"
                        }
                    },
                    {
                        "term" : {
                                "zip" : "00000"
                        }
                    }
                ],
                filter : [
                    {
                        geo_distance : {
                            distance : range + 'mi',
                            'position' : point
                        }    
                    }
                ]

            }
        },
        sort: [{
            _geo_distance: {
                "position": point,
                "order":         "asc",
                "unit":          "mi",
                "distance_type": "plane"
            }
        }]
    }
};

答案 1 :(得分:0)

错误消息意味着您应该将组合的term-filter扩展为包含两个子句的bool-filter:

"term" : { "listing.rentForSale" : "R" }

"term" : { "listing.statusCode" : "Rented" }

这两个被not: { bool: { should: { [both clauses] } } }

包围

如果您使用mustshould(并提防否定和评分),这取决于您想要获得的确切含义。

希望有所帮助, FRICKE