弹性搜索 Spring Boot

huangapple go评论145阅读模式
英文:

Elasticsearch Spring boot

问题

尝试使用 Java API 转换此 ElasticSearch 查询:

QueryBuilder query = QueryBuilders.functionScoreQuery(QueryBuilders.matchAllQuery(), ScoreFunctionBuilders.randomFunction());

我正在尝试以随机顺序返回所有匹配项,但似乎无法正常工作。

英文:

Am trying to convert this elastic search query using java api

{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "random_score": {}
        }
      ],
       "score_mode": "sum"
    }
  }
}

this is what i have done

  QueryBuilder query = QueryBuilders.functionScoreQuery(QueryBuilders.matchAllQuery(), ScoreFunctionBuilders.randomFunction());

Am trying to return all matches in a random order, does not seem to work

答案1

得分: 0

Please see the below example and what I observed. It should probably help. I've created a sample mapping with a single field myfield of text type.

Sample Documents:

POST my_function_index/_doc/1
{
"myfield": "Doesn't remind me of anything"
}

POST my_function_index/_doc/2
{
"myfield": "I like playing in sand, what's mine is yours"
}

POST my_function_index/_doc/3
{
"myfield": "I like travelling backwards in the fog"
}

Java API Code

package com.example.demo;

import java.io.IOException;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery.ScoreMode;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class FunctionScore {

public static void main(String[] args) throws IOException {

    RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(new HttpHost("localhost", 9200, "http")));

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    // Below is the logic for creating functionScore via API
    FunctionScoreQueryBuilder functionScore = QueryBuilders.functionScoreQuery(QueryBuilders.matchAllQuery(), ScoreFunctionBuilders.randomFunction());
    functionScore.scoreMode(ScoreMode.SUM);
    sourceBuilder.query(functionScore);

    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("my_function_index");
    searchRequest.source(sourceBuilder);

    // The API Response you get
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    SearchHit[] searchHits = searchResponse.getHits().getHits();

    // Printing the results
    for (SearchHit theHit : searchHits) {
        System.out.println("-----------------------------------------------------");
        System.out.println(theHit);
        System.out.println("-----------------------------------------------------");
    }
}

}

Response Observed:

Below are two sample responses where you get the same order of documents however note their _score values. They are different.

Note that each time I run the above API, I get a different response. I guess due to my index size being 3, at some point I am bound to get the same order.

If you have more documents, the variations would be a lot more.

First Instance


{
"_index": "my_function_index",
"_type": "_doc",
"_id": "3",
"_score": 0.8976933,
"_source": {
"myfield": "I like travelling backwards in the fog"
}
}


{
"_index": "my_function_index",
"_type": "_doc",
"_id": "2",
"_score": 0.85642433,
"_source": {
"myfield": "I like playing in sand, what's mine is yours"
}
}


{
"_index": "my_function_index",
"_type": "_doc",
"_id": "1",
"_score": 0.42347366,
"_source": {
"myfield": "Doesn't remind me of anything"
}
}

Second Instance:


{
"_index": "my_function_index",
"_type": "_doc",
"_id": "3",
"_score": 0.8440073,
"_source": {
"myfield": "I like travelling backwards in the fog"
}
}


{
"_index": "my_function_index",
"_type": "_doc",
"_id": "2",
"_score": 0.79675066,
"_source": {
"myfield": "I like playing in sand, what's mine is yours"
}
}


{
"_index": "my_function_index",
"_type": "_doc",
"_id": "1",
"_score": 0.7430876,
"_source": {
"myfield": "Doesn't remind me of anything"
}
}

英文:

Please see the below example and what I observed. It should probably help. I've created a sample mapping with a single field myfield of text type.

Sample Documents:

POST my_function_index/_doc/1
{
  "myfield": "Doesn't remind me of anything"
}

POST my_function_index/_doc/2
{
  "myfield": "I like playing in sand, what's mine is yours"
}

POST my_function_index/_doc/3
{
  "myfield": "I like travelling backwards in the fog"
}

Java API Code

package com.example.demo;

import java.io.IOException;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery.ScoreMode;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class FunctionScore {

	public static void main(String[] args) throws IOException {

		RestHighLevelClient client = new RestHighLevelClient(
				RestClient.builder(new HttpHost("localhost", 9200, "http")));
		
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		
        //Below is the logic for creating functionScore via API
		FunctionScoreQueryBuilder functionScore = QueryBuilders.functionScoreQuery(QueryBuilders.matchAllQuery(), ScoreFunctionBuilders.randomFunction());
		functionScore.scoreMode(ScoreMode.SUM);
		sourceBuilder.query(functionScore);

		SearchRequest searchRequest = new SearchRequest();
		searchRequest.indices("my_function_index");
		searchRequest.source(sourceBuilder);
        
        //The API Response you get
		SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);		
		SearchHit[] searchHits = searchResponse.getHits().getHits();
		
        //Printing the results
		for(SearchHit theHit: searchHits) {
			System.out.println("-----------------------------------------------------");
			System.out.println(theHit);
			System.out.println("-----------------------------------------------------");
		}

	}

}

Response Observed:

Below are two sample responses where you get the same order of documents however note their _score values. They are different.

Note that each time I run the above API, I get a different response. I guess due to my index size being 3, at some point I am bound to get same order.

If you have more documents, the variations would be a lot more.

First Instance

-----------------------------------------------------
{
  "_index" : "my_function_index",
  "_type" : "_doc",
  "_id" : "3",
  "_score" : 0.8976933,
  "_source" : {
    "myfield" : "I like travelling backwards in the fog"
  }
}
-----------------------------------------------------
-----------------------------------------------------
{
  "_index" : "my_function_index",
  "_type" : "_doc",
  "_id" : "2",
  "_score" : 0.85642433,
  "_source" : {
    "myfield" : "I like playing in sand, what's mine is yours"
  }
}
-----------------------------------------------------
-----------------------------------------------------
{
  "_index" : "my_function_index",
  "_type" : "_doc",
  "_id" : "1",
  "_score" : 0.42347366,
  "_source" : {
    "myfield" : "Doesn't remind me of anything"
  }
}
-----------------------------------------------------

Second Instance:

-----------------------------------------------------
{
  "_index" : "my_function_index",
  "_type" : "_doc",
  "_id" : "3",
  "_score" : 0.8440073,
  "_source" : {
    "myfield" : "I like travelling backwards in the fog"
  }
}
-----------------------------------------------------
-----------------------------------------------------
{
  "_index" : "my_function_index",
  "_type" : "_doc",
  "_id" : "2",
  "_score" : 0.79675066,
  "_source" : {
    "myfield" : "I like playing in sand, what's mine is yours"
  }
}
-----------------------------------------------------
-----------------------------------------------------
{
  "_index" : "my_function_index",
  "_type" : "_doc",
  "_id" : "1",
  "_score" : 0.7430876,
  "_source" : {
    "myfield" : "Doesn't remind me of anything"
  }
}
-----------------------------------------------------

huangapple
  • 本文由 发表于 2020年8月14日 21:00:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63413269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定