Фильтр запросов Elastisearch

Я пытаюсь сделать запрос с фильтром в моем индексе, но когда я пытаюсь выполнить фильтрацию по любому атрибуту в сопоставлении, запрос не возвращает результата.

Запрос следующий: если я запускаю только часть geo_distance, я получаю результаты. Я хотел бы отфильтровать результаты, используя одно из свойств в сопоставлении (в данном случае рейтинг, но это может быть город, штат и т. д.). Запрос создается на Java через QueryBuilder из библиотеки elasticsearch (v 52.0). Но пока я пытаюсь понять, как создать рабочий запрос и выполнить его через 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
    }
  }
}

Если я запускаю фильтрацию запросов по почтовому индексу или идентификатору, она работает. Например, такой запрос:

{"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"
          }
        }
      }
    ]
  }
}

Буду очень признателен за любую помощь.

Спасибо

1
0
53
1

Ответы 1

Попробуйте вместо этого этот запрос

{
  "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
    }
  }
}

Удачи с этим?

Val 27.11.2018 17:55

Другие вопросы по теме