Elasticsearch java api filteredQuery - java

I have this query used with Exists API from elasticsearch (1.4.4) :
curl -XPOST 'http://elasticsearch:9200/_search/exists' -d '
{
"query": {
"filtered": {
"query": {
"match_phrase": {
"message": "2014-12-04 00:00:01 CET Tx[XXXXXXXX] cmd[INSERT] PID[XXXX] DB[XXXXX] LOG: some log info here ;-) \r"
}
},
"filter": {
"term" : {
"some_field" :"some_value"
}
}
}
}
}'
This works fine (return true when it has to be) but when I tried to do the same with java API like this :
Client client = this.createClient();
QueryBuilder queryBuilder = QueryBuilders.filteredQuery(
QueryBuilders.matchPhraseQuery("message", "the same message"),
FilterBuilders.termFilter("some_field", "some value")
);
System.out.println(queryBuilder.toString());
ExistsResponse response = client.prepareExists("existsMessage")
.setTypes(type)
.setIndicesOptions(IndicesOptions.fromOptions(true, true, true, false))
.setQuery(queryBuilder).execute().actionGet();
System.out.println(response.exists());
client.close();
But the result is always false! So I print the request build by the and it's different than want I wanted. So is there a way to do exactly my first request from source json or other way using api builders?
Edit :
The output of queryBuilder.toString()) :
{
"filtered" : {
"query" : {
"match" : {
"message" : {
"query" : "the same message\r",
"type" : "phrase"
}
}
},
"filter" : {
"term" : {
"some field" : "some value"
}
}
}
}

Related

How to implement Stemmer search in Elasticsearch 6.1 on java

I need to implement stemmer search, I've found this link on elasticsearch documentation. There is json that I've had send to elascticsearch server. But I new in elasticsearch and cannot figure out how to implements this in java. I cannot also find any examples. Ccould you please help me with this?
I've add setting with
PUT /data
{
"settings": {
"analysis" : {
"analyzer" : {
"my_analyzer" : {
"tokenizer" : "standard",
"filter" : ["standard", "lowercase", "my_stemmer"]
}
},
"filter" : {
"my_stemmer" : {
"type" : "stemmer",
"name" : "english"
}
}
}
}
}
after that I am trying to find 'skis' with query:
GET data/_search
{
"query": {
"simple_query_string": {
"fields": [ "value36" ],
"query": "ski"
}
}
}
but result is empty

ElasticSearch | Filter Query

I am using elasticsearch-1.7.1 and java dependency is
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.7.1</version>
Problem : I am using in a clause in ES but when I query ES with filtered query it is giving me a null response.
contentQuery = boolQuery().must(QueryBuilders.matchQuery(PROPERTY_BOOK_ID, bookId).operator(Operator.AND))
.must(QueryBuilders.matchQuery("contentType", "enrichments").operator(Operator.AND))
.must(contentQuery);
FilteredQueryBuilder builder =
QueryBuilders.filteredQuery(contentQuery, FilterBuilders.termFilter("spineId","chapter04.html"));
searchResponse = searchClientService.getClient()
.prepareSearch(INDEX_NAME).setTypes(INDEX_TYPE)
.setQuery(builder)
.execute().actionGet();
without a filter, it is working fine. Can anyone please suggest how to apply a filter to the query or let me know if am going anywhere wrong while making the requests.
Query Details :
{
"filtered" : {
"query" : {
"bool" : {
"must" : [ {
"match" : {
"bookId" : {
"query" : "d563739c-8b46-449b-b695-d662cb32087d",
"type" : "boolean",
"operator" : "AND"
}
}
}, {
"match" : {
"contentType" : {
"query" : "enrichments",
"type" : "boolean",
"operator" : "AND"
}
}
}, {
"bool" : {
"must" : {
"match" : {
"content" : {
"query" : "resources*",
"type" : "phrase_prefix"
}
}
}
}
} ]
}
},
"filter" : {
"term" : {
"spineId" : "chapter04.html"
}
}
}
}
I've also tried other filter queries on my index, none of them is working.Below query must return some result but its returning empty result.
{
"filtered" : {
"query" : {
"match_all" : { }
},
"filter" : {
"bool" : {
"must" : {
"term" : {
"spineId" : "chapter03.html"
}
}
}
}
}
}
Mapping :
private XContentBuilder buildMapping() throws Exception {
return jsonBuilder().prettyPrint()
.startObject()
.startObject(indexType)
.startObject("properties")
.startObject(PROPERTY_SPINE_ID).field("type", "string").field("index", "not_analyzed").endObject()
.endObject()
.endObject()
.endObject();
}
XContentBuilder source = jsonBuilder().startObject();
source.field(PROPERTY_BOOK_ID, bookId)
.field(PROPERTY_WIDGET_ID, widgetId)
.field(PROPERTY_CONTENT, widgetDescription)
.field(PROPERTY_CONTENT_TYPE, contentType.getValue())
.field(PROPERTY_META_DATA, widgetJsonString)
.field(PROPERTY_SPINE_ID, spineId)
.endObject();
any help greatly appreciated.

Elasticsearch: Filter Query not working

I've been working to change my query to a filter in Elasticsearch using the java api. I've made sure that the fields I am running searches on are set to "not_analzed".
Here is the java code for the filter:
FilterBuilder andFilter = FilterBuilders.andFilter(FilterBuilders.termFilter("thread_name", keyword), FilterBuilders.termFilter("site_name", "test_site"));
QueryBuilder filteredQuery = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), andFilter);
SearchResponse threadResponse = client.prepareSearch("thread_and_messages").setQuery(filteredQuery).setSize(20).setFrom(firstRowOffset).execute().actionGet();
This then gets translated to this in JSON:
"filtered" : {
"query" : {
"match_all" : { }
},
"filter" : {
"and" : {
"filters" : [ {
"term" : {
"thread_name" : "apple"
}
}, {
"term" : {
"site_name" : "test_site"
}
} ]
}
}
}
When I try to do any searches it won't return the document I'm looking for which is thread_name: Apple and site_name: test_site.
Also when I run this using a curl command it won't find that document either.
Can anyone see what I'm doing wrong here?
You can use must clause.That will perform and operation
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "thread_name",
"query": "apple"
}
},
{
"query_string": {
"default_field": "site_name",
"query": "test_site"
}
}
]
}
}

java elasticsearch query without java API

I try to query an existing elasticsearch base in java without using the Java API.
This elasticsearch base belongs to an ELK cluster.
The correct cURL query is :
curl -XGET 'http://10.60.74.134:9200/logstash-2015.04.09/_search?pretty' -d '{
"facets": {
"0": {
"date_histogram": {
"field": "#timestamp",
"interval": "5m"
},
"global": true,
"facet_filter": {
"fquery": {
"query": {
"filtered": {
"query": {
"query_string": {
"query": "*"
}
},
"filter": {
"bool": {
"must": [
{
"range": {
"#timestamp": {
"from": 1428558001338,
"to": 1428579601338
}
}
},
{
"terms": {
"_type": [
"akaoatg-monitoring"
]
}
}
]
}
}
}
}
}
}
}
},
"size": 0
}'
which works perfectly fine and returns me my JSON results :
{
"took" : 185,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 9106263,
"max_score" : 0.0,
"hits" : [ ]
},
"facets" : {
"0" : {
"_type" : "date_histogram",
"entries" : [ {
"time" : 1428458700000,
"count" : 2429
}, {
"time" : 1428459000000,
"count" : 21128
}, {
"time" : 1428459300000,
"count" : 21354
} ]
}
}
}
I tried to get the same results using an http request in java :
try {
URL url = new URL("http://10.60.74.134:9200/logstash-2015.04.09/_search?pretty'-d'{\"facets\":{\"terms\":{\"terms\":{\"field\":\"_type\",\"size\":10,\"order\":\"count\",\"exclude\":[]},\"facet_filter\":{\"fquery\":{\"query\":{\"filtered\":{\"query\":{\"bool\":{\"should\":[{\"query_string\":{\"query\":\"*\"}}]}},\"filter\":{\"bool\":{\"must\":[{\"range\":{\"#timestamp\":{\"from\":1428558001341,\"to\":1428579601341}}},{\"terms\":{\"_type\":[\"akaoatg-monitoring\"]}}]}}}}}}}},\"size\":0}");
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String strTemp;
while((strTemp = br.readLine()) != null){
System.out.println(strTemp);
}
} catch (Exception ex) {
ex.printStackTrace();
}
The URL I am using here is the cURL request formatted to fit in an http request.
This request returns me a single String which does not contain the same result.
here is a part of the java result :
{"took":22,
"timed_out":false,
"_shards":{"total":5,
"successful":5,
"failed":0},
"hits":{"total":4621367,
"max_score":1.0,
"hits":[{"_index":"logstash-2015.04.09",
"_type":"xxx",
"_id":"xxx",
"_score":xxx,
"_source":{"#version":"xxx",
"#timestamp":"2015-04-09T01:09:59.347Z",
"host":"xxx",
"type":"xxx",
"sys_priority":"xxx",
"sys_timestamp":"xxx",
"logsource":"xxx",
"application":"xxx",
"year":"2015",
"month":"04",
"day":"09",
"hour":"01",
"minute":"09",
"second":"58",
"trace_level":"3",
"host_name":"xxx",
"adh_port":"xxx",
"timestamp_adh":1428541798954,
"time_adh":27,
"adh_uuid":"xxx",
"Service":"xxx",
"ReturnCode":"0",
"ErrorMessage":"null",
"Site":"null",
"BaseType":"null",
"PlatForm":"0",
"Cad_sender":"",
"Domain":"xxx",
"Freshness":"9",
"ClientProcessID":"xxx",
"CallMode":"S",
"SystemMode":"R",
"Sad_receiver":"",
"ConnectionType":"IP",
"DataFormat":"",
"HeaderType":"H4",
"AdhesionVersion":"null",
"Length":"10",
"ConnectionInfo":"null",
"ConnectionInfoKey":"null",
"Comments":"null",
"ActionCode":"null",
"TimeStamp":20150409010958,
"ServerProgramName":"null",
"TransactionCode":"null",
"TraceLevel":"null",
"LU":"null",
"HostName":"xxx",
"Port":"xxx",
"Timer":20,
"SendQueue":"null",
"ReturnQueue":"",
"PDM":"",
"RFU":"null",
"FTU":"",
"ActivationFlag":"null",
"HistoryQueue":"null",
"ErrorQueue":"null",
"CallReference":"xxx",
"IPAddress":"xxx",
"MessageType":"I",
"ProgramName":"null",
"UserName":"xxx",
"BeginTime":"24:00:00",
"EndTime":"24:00:00",
"duration":0,
"cnx_running":0,
"cnx_max":0}}]}}
Any idea of what I'm doing wrong ?
Your request sent via Java is malformed: there is no "facets": { "0": {...}} part in it. (At least the '0' is missing). Hence the server seems to ignore that part and just give you a bunch of raw entries (the default output)
The problem was the type of request, I had to send a POST request instead of a GET one.
Used this code :
String url = "http://10.60.74.134:9200/logstash-2015.04.09/_search?pretty'-d'";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "{\"facets\":{\"0\":{\"date_histogram\":{\"field\":\"#timestamp\",\"interval\":\"5m\"},\"global\":true,\"facet_filter\":{\"fquery\":{\"query\":{\"filtered\":{\"query\":{\"query_string\":{\"query\":\"*\"}},\"filter\":{\"bool\":{\"must\":[{\"range\":{\"#timestamp\":{\"from\":1428558001338,\"to\":1428579601338}}},{\"terms\":{\"_type\":[\"akaoatg-monitoring\"]}}]}}}}}}}},\"size\":0}";
thanks to this tutorial

Elasticsearch java top level query

I am trying to run a search on the following json using the java api
{
"query": {
"has_child": {
"query" : {
"filtered": {
"query": { "match_all": {}},
"filter" : {
"and": {"filters":[
{"term": {"term": "value"}}
]}
}
}
},
"child_type": "child"
}
}
}
Here is the java code I have,
QueryBuilders.hasChildQuery("child", QueryBuilders
.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.andFilter(FilterBuilders.
termFilter("term", "value"))));
However this just produces the json
{"has_child":
{"query":
{"filtered":
{"query":{"match_all":{}},
"filter":{
"and":
{"filters":[
{"term":{"term":"value"}}
]
}
}
}
},
"child_type":"child"
}
}
As you can see I am missing a top level { "query" : ... } but I cannot find out how to add this top level query using the java api for elasticsearch.
From my understanding inside the code
client.prepareSearch("parent-child")
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(query)
.setFrom(0).setSize(60).setExplain(true)
.execute()
.actionGet();
It autowraps the query with {"query" : ...}
My issue was that I was searching on the wrong index.

Categories

Resources