elasticsearch - Elasticsearch查询过滤器

标签 elasticsearch query-builder

我正在尝试使用索引上的过滤器进行查询,但是当我尝试对映射中的任何属性进行过滤时,查询均不会返回任何结果。

查询如下,如果我仅使用geo_distance部分运行,则会得到结果。我想使用映射中的一个属性(在这种情况下为等级,但可以是城市,州ecc)来过滤结果。
查询是通过Elasticsearch库(v 52.0)中的QueryBuilder用Java生成的。但是目前,我试图了解如何构建有效的查询并通过CURL执行。

{
  "query": {
    "bool": {
      "filter": [
        {
          "geo_distance": {
            "geometry.coordinates": [
              12.3232,
              12.2323
            ],
            "distance": 200000,
            "distance_type": "plane",
            "validation_method": "STRICT",
            "ignore_unmapped": false,
            "boost": 1
          }
        },
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "rating": [
                    "0"
                  ],
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

如果我对邮政编码或ID运行查询过滤,则可以正常工作。
例如这样的查询:
{"query":{"bool":{"filter":{"term":{"zipCode":"111111"}}}}}

我的映射的一个片段是这个
{
  "my_index": {
    "mappings": {
      "poielement": {
        "dynamic_templates": [
          {
            "suggestions": {
              "match": "suggest_*",
              "mapping": {
                "analyzer": "my_analyzer",
                "copy_to": "auto_suggest",
                "search_analyzer": "my_analyzer",
                "store": true,
                "type": "text"
              }
            }
          },
          {
            "integers": {
              "match_mapping_type": "long",
              "mapping": {
                "type": "text"
              }
            }
          },
          {
            "geopoint": {
              "match": "coordinates",
              "mapping": {
                "type": "geo_point"
              }
            }
          },
          {
            "property": {
              "match": "*",
              "mapping": {
                "analyzer": "my_analyzer",
                "search_analyzer": "my_analyzer"
              }
            }
          }
        ],
        "date_detection": false,
        "numeric_detection": false,
        "properties": {
          "city": {
            "type": "text",
            "analyzer": "my_analyzer"
          },
          "country": {
            "type": "text",
            "analyzer": "my_analyzer"
          },
          "geometry": {
            "properties": {
              "coordinates": {
                "type": "geo_point"
              },
              "type": {
                "type": "text",
                "analyzer": "my_analyzer"
              }
            }
          },
          "id": {
            "type": "text"
          },
          "name": {
            "type": "keyword"
          },
          "rating": {
            "type": "text"
          },
          "total_rate": {
            "type": "text",
            "analyzer": "my_analyzer"
          },
          "type": {
            "type": "text",
            "analyzer": "my_analyzer"
          },
          "zipCode": {
            "type": "text"
          }
        }
      }
    }
  }
}

当我通过http://elasticsearchpat/my_index/_search检索数据时,数据看起来像这样
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 4,
    "successful": 4,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 7517,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index",
        "_type": "poielement",
        "_id": "58768",
        "_score": 1,
        "_source": {
          "zipCode": 111111,
          "country": "USA",
          "city": "Portland",
          "rating": 0,
          "type": "",
          "id": 123,
          "geometry": {
            "coordinates": [
              12.205061,
              12.490463
            ],
            "type": "Point"
          }
        }
      }
    ]
  }
}

我将非常感谢您的帮助。

谢谢

最佳答案

试试这个查询

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "rating": 0
          }
        }
      ],
      "filter": [
        {
          "geo_distance": {
            "geometry.coordinates": [
              12.3232,
              12.2323
            ],
            "distance": 200000,
            "distance_type": "plane",
            "validation_method": "STRICT",
            "ignore_unmapped": false,
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

关于elasticsearch - Elasticsearch查询过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52600691/

相关文章:

elasticsearch - ELK最合适的时间戳名称_或@

Grails 启动错误 : groovy. lang.Closure.rehydrate(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Lgroovy/lang/Closure;

php - Symofny2 查询构建器 leftJoin 与来自多个关系的条件

elasticsearch - 如何在 Golang 中使用 Olivere 将数据插入 Elasticsearch

amazon-web-services - Elasticsearch-按多个数组中数组匹配的分数排序

elasticsearch - 没有setTimeout的Elasticsearch后端测试失败

php - 为什么 Codeigniter 的查询生成器返回错误的 MySQL 查询两次?

php - 使用 id 参数查询返回错误记录

mysql - 我在查询生成器 SYMFONY 中遇到问题

join - Doctrine orm 计数 1 :n relationship on join