【问题标题】:ElasticSearch Error: [term] query does not support different field names, use [bool] query insteadElasticSearch 错误:[term] 查询不支持不同的字段名称,请改用 [bool] 查询
【发布时间】:2018-08-08 17:06:02
【问题描述】:

我目前正在使用 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"
            }
        }]
    }
};

我的初始查询在 must 数组 中有 "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" 子句 移动到 filter 数组 现在会产生正确的搜索结果,但仍会引发此错误。有什么想法可以清除此错误吗?

【问题讨论】:

    标签: javascript json elasticsearch lucene


    【解决方案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"
                }
            }]
        }
    };
    

    【讨论】:

      【解决方案2】:

      错误消息意味着您应该将组合的术语过滤器扩展为具有两个子句的布尔过滤器:

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

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

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

      如果你使用mustshould,这取决于你想要得到的确切含义(并注意否定和评分)。

      希望有帮助, 弗里克

      【讨论】:

      • 我最初在 bool-filter 中有这两个 term 子句,但没有产生正确的搜索结果。我编辑了我的问题以反映这一点。
      • 将 not: { bool: { should: { [both clauses] } } } 替换为 {bool: [{ not: { term: { 'listing.rentForSale': "R" } } } , { not: { term: { 'listing.statusCode': "Rented" } } } ] }
      猜你喜欢
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多