简体   繁体   English

如何使用 elasticsearch 和 spring-boot (springframework.data.elasticsearch) 返回距离和所有字段

[英]How to return distance and all fields using elasticsearch and spring-boot (springframework.data.elasticsearch)

I found a way to get my data using elasticsearch query.我找到了一种使用 elasticsearch 查询获取数据的方法。 But my current problem is to translate it in spring boot query.但我目前的问题是在 spring 引导查询中翻译它。

My query is the following:我的查询如下:

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "should": [
        {
          "constant_score": {
            "filter": {
              "term": {
                "my_field": "my_value"
              }
            },
            "boost": 10
          }
        },
        {
          "constant_score": {
            "filter": {
              "term": {
                "my_field": "my_value"
              }
            },
            "boost": 8
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "my_array_field": [
              "first_value",
              "second_value"
            ]
          }
        },
        {
          "geo_distance": {
            "distance": "10km",
            "location": {
              "lat": 40,
              "lon": 2
            }
          }
        }
      ]
    }
  },
  "script_fields": {
    "distance": {
      "script": "doc['location'].arcDistance(40, 2) / 1000"
    }
  },
  "_source": true,
  "sort": [
    {
      "_score": "desc"
    }
  ]
}

All documents stored in the index have a fields location , with a type defined as a geo_point存储在索引中的所有文档都有一个字段location ,类型定义为geo_point

My question is:我的问题是:

How to translate the script_fields , which is returning me the distance, using spring-boot.如何使用 spring-boot 翻译返回距离的script_fields

My current code is:我当前的代码是:

BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(PageRequest.of(page, size))
                .build();

elasticsearchOperations.search(nativeSearchQuery, MyIndexedDocument.class);

I tried to use ScriptField from here and saw an example here but didn't find any solution for now.我尝试从这里使用 ScriptField 并在这里看到了一个示例,但目前没有找到任何解决方案。

  1. How should I use it?我应该如何使用它?
  2. If think, after finding how to return distance, I will have to precise to my query that I want all the _source field in return.如果认为,在找到如何返回距离之后,我将不得不精确地查询我想要返回的所有_source字段。 Is it possible to precise it using NativeSearchQuery ?是否可以使用NativeSearchQuery对其进行精确化?

Answer as a workaround作为解决方法回答

I let the question without any validated answer in case of somebody has a better answer.如果有人有更好的答案,我会在没有任何经过验证的答案的情况下提出问题。

In case it's a question which is relevant for somebody, I finally choose to deal the distance return by using geo distance sort.如果这是一个与某人相关的问题,我最终选择使用地理距离排序来处理距离返回。

The query is something like this:查询是这样的:

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "should": [
        {
          "constant_score": {
            "filter": {
              "term": {
                "my_field": "my_value"
              }
            },
            "boost": 10
          }
        },
        {
          "constant_score": {
            "filter": {
              "term": {
                "my_field": "my_value"
              }
            },
            "boost": 8
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "my_array_field": [
              "first_value",
              "second_value"
            ]
          }
        },
        {
          "geo_distance": {
            "distance": "10km",
            "location": {
              "lat": 40,
              "lon": 2
            }
          }
        }
      ]
    }
  },
    "sort" : [
        { "_score": "desc"},{
      "_geo_distance": {
        "location": {
                      "lat": 40,
                      "lon": 2
        },
        "order": "asc",
        "unit": "km",
        "mode": "min",
        "distance_type": "arc",
        "ignore_unmapped": true
      }
    }
   ]
}

So I can get my distance in return, but not as a field of content , but as a value in sortvalues所以我可以获得我的距离作为回报,但不是作为content的字段,而是作为sortvalues中的值

Using spring data elasticsearch:使用spring数据elasticsearch:

BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
ScoreSortBuilder scoreSortBuilder = new ScoreSortBuilder();
GeoDistanceSortBuilder geoDistanceSortBuilder = new GeoDistanceSortBuilder("location", Double.parseDouble(location.getLatitude()), Double.parseDouble(location.getLongitude()));
geoDistanceSortBuilder.unit(DistanceUnit.KILOMETERS);
geoDistanceSortBuilder.ignoreUnmapped(true);
geoDistanceSortBuilder.sortMode(SortMode.MIN);
geoDistanceSortBuilder.order(SortOrder.ASC);

NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(PageRequest.of(page, size))
                .withSort(scoreSortBuilder)
                .withSort(geoDistanceSortBuilder)
                .build();

elasticsearchOperations.search(nativeSearchQuery, MyIndexedDocument.class);

you can use the annotation @ScriptedField你可以使用注解@ScriptedField

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 spring-boot 中指定较新版本的 Elasticsearch - How to specify a newer version of Elasticsearch in spring-boot Spring 引导数据 Elasticsearch - Spring Boot Data Elasticsearch 将 spring-boot 应用程序的 http 端点发送到 elasticsearch - ship http endpoints of a spring-boot app to elasticsearch 如何使用 Java 在 Elasticsearch 中返回所有结果? - How to return all the results in Elasticsearch using Java? 如何使用 Spring Data Elasticsearch 使用 OR 逻辑创建对 Elasticsearch 的复杂查询? - How to create complex query to Elasticsearch with OR logic using Spring Data Elasticsearch? 通过弹簧数据使用Elasticsearch 2.4的弹簧靴2 - spring boot 2 with elasticsearch 2.4 via spring data 更好的 Elasticsearch 客户端从 JAVA Z6971E8BC40C890FA3DE58CD251D6E2B2 连接 AWS Elasticsearch - Better Elasticsearch client to connect AWS Elasticsearch from JAVA Spring-boot 如何使用 Spring 数据 Elasticsearch 删除索引? - How to delete index using Spring Data Elasticsearch? Spring Boot + Spring Data JPA + Spring Data ElasticSearch:弹性不会返回任何结果 - Spring Boot + Spring Data JPA + Spring Data ElasticSearch: elastic doesn't return any results 如何在 Java Boot 中使用 RestHighLevelClient 将文档插入 ElasticSearch Spring - How to insert document into ElasticSearch using RestHighLevelClient in Java Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM