I want to fix queries in Elasticsearch's Java client


(SHUSAKU MIYAHARA) #1

I executed "elasticsearch" with "curl" and "Java Client".

The result is different.

  • totalhits
  • Order of results

These are different

I want to fix the Java client code.
Please let me know how to fix the Java client.

curl

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

curl result

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

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

Environment

  • Language: Java 8
  • Framework: Spring boot
  • Elasticsearch client (jar) ver
    • Org.elasticsearch: 5.1.1
    • Org.elasticsearch.client: 5.1.1
  • Elasticsearch's ver: 5.3.1

(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.