我想修复Elasticsearch的Java客户端中的查询

时间:2017-08-14 04:21:01

标签: java spring curl elasticsearch spring-boot

我用“curl”和“Java Client”执行了“elasticsearch”。

结果不同。

  • totalhits
  • 结果顺序

这些是不同的

我想修复Java客户端代码。 请告诉我如何修复Java客户端。

卷曲

curl -XGET http://localhost:9200/_search?pretty -d '
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "match": {
                  "test1": "java"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "match": {
                  "test2": "java"
                }
              }
            ],
            "boost" : 5.0
          }
        }
      ]
    }
  }
}
'

卷曲结果

{
  "took" : 13,
  "timed_out" : false,
  "_shards" : {
    "total" : 100,
    "successful" : 100,
    "failed" : 0
  },
  "hits" : {
    "total" : 172405,
    "max_score" : 16.018892,
    "hits" : [
      {
        "_index" : "member",
        "_type" : "type",
        "_id" : "2",
        "_score" : 16.018892,
        "_source" : {
          "fw" : [
            "java"
          ]
        }
      }
    ]
  }
}

的java

private String hostnames = "hostname:9300";
private String clusterName = "clustername";

Settings settings = Settings.builder()
.put("cluster.name", clusterName)
.build();
TransportClient client = new PreBuiltTransportClient(settings);
for (String hosname : hostnames.split(",")) {
HostAndPort hp = HostAndPort.fromString(hosname);
String hostName = hp.getHostText();
int port = hp.getPort();
    try {
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostName),port));
    } catch (UnknownHostException e) {
        client.close();
        throw new RuntimeException("hostname is injustice");
    }
}

BoolQueryBuilder shouldQuery = QueryBuilders.boolQuery();
BoolQueryBuilder boolQueryTest1 = QueryBuilders.boolQuery();
BoolQueryBuilder boolQueryTest2 = QueryBuilders.boolQuery();

boolQueryTest1.must(QueryBuilders.matchQuery("test1", "java"));
boolQueryTest2.must(QueryBuilders.matchQuery("test2", "java").boost((float) 5.0));
shouldQuery.should(boolQueryTest1);
shouldQuery.should(boolQueryTest2);

SearchResponse response = client.prepareSearch()
.setQuery(shouldQuery)
.execute()

Java结果

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 100,
        "successful": 100,
        "failed": 0
    },
    "hits": {
        "total": 172449,
        "max_score": 19.487148,
        "hits": [
            {
                "_index": "blog",
                "_type": "type",
                "_id": "3",
                "_score": 19.487148,
                "_source": {
                    "fw": [
                        "java"
                    ]
                }
            }
        ]
    }
}

环境

  • 语言:Java 8
  • 框架:Spring boot
  • Elasticsearch客户端(jar)ver
    • Org.elasticsearch:5.1.1
    • Org.elasticsearch.client:5.1.1
  • Elasticsearch的ver:5.3.1

0 个答案:

没有答案