Elasticsearch _default_ mapping via API - mapping

i make this call to map by default, all datatypes as strings-
curl -XPUT 'http://localhost:9200/_all/_default_/_mapping' -d '
{
"mappings": {
"_default_": {
"dynamic_templates": [
{
"match": "*",
"mapping": {
"type": "string"
}
}
]
}
}
}
'
The mapping does not work, so i make this call to verify-
curl -XGET 'http://localhost:9200/_all/_mapping'
{
"logstash-2014.02.05": {
"_default_": {
"properties": {}
}
}
Why is the properties part empty?

You should delete the mappings key from your PUT request. You only specify mappings when you are creating an index, not when updating mappings.

Related

How to construct nested query using criteria and criteriaQuery in spring-data-elasticsearch

I am using spring-data-elasticsearch. I have constructed most of the query conditions using criteria with lots of sub-criteria. Now I want to include a simple query condition for a nested field. But criteria corms query using uery_string API which is not working for nested fields. I am expecting Nested query.
How to support this using Criteria without NativeSearchQuery?
Nested Mapping
{
"ae": {
"type": "nested",
"properties": {
"atb": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"su": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
I want to query "ae.su.keyword" field. Building criteriaQuery using this field constructs query_string query using this field, which is not returing correct documents in response as expected. My expectationis, Is there any way to build nested query using criteria? or Override existing criteria query?
Criteria criteria = new Criteria("ae.su.keyword").is("VALUE");
CriteriaQuery query = new CriteriaQuery(criteria);
elasticOperations.search(query, Foo.class, index);
Currently Spring Data Elasticsearch's CriteriaQuery does not support creating nested queries.
The query that is created currently with your example code is (cleaning out irrelevant parts):
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "one",
"fields": [
"ae.su.keyword^1.0"
]
}
}
]
}
}
}
With the nested object you have the needed query would be:
{
"query": {
"nested": {
"path": "ae",
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "one",
"fields": [
"ae.su.keyword"
]
}
}
]
}
}
}
}
}
basically wrapping the query that is built now in an additional nested query.
As I wrote, creating this nested query is not supported at the moment. What you can do, besides building a
NativeSearchQuery, is using a StringQuery (yes, it's very ugly as well):
Query query = new StringQuery("{\"nested\":{\"path\":\"ae\",#" +
"\"query\":{\"bool\":{\"must\":[{\"query_string\": {\"query\":\"$1\"," +
"\"fields\":[\"ae.su.keyword\"]}}]}}}}".replace("$1", value));
return operations.search(query, Foo.class);
Edit 01.04.2021:
This topic came up again in an issue, I opened a bug issue to address this. I will go to implement this fix these days.
Edit 05.04.2021:
fixed from 4.2.0.GA on

Filtered search with Authorization for Elasticsearch

I'm trying to do a search where I look for "test" in any field while filtering for a specific client in the client_id field. Can't seem to figure this one out. This is how fat I got (but it's not working):
{
query: {
filtered: {
query: "test",
filter: {
term: {client_id: #client.id}
}
}
}
}
This is the right syntax
{
"query": {
"filtered": {
"query": {
"match": {
"_all": "test"
}
},
"filter": {
"term": {
"client_id": #client.id
}
}
}
}
}
From ES Docs: The _all field allows you to search for values in documents without knowing which field contains the value

How do I design a ruby equivalent objects for this json structure

I have a json structure that I need to build up based on url parameters provided by a client. Currently I've been building the json structure out using Jbuilder.encode but it's getting pretty hairy.
self.query = Jbuilder.encode do |json|
json.query do
json.filtered do
json.filter do
json.bool do
if(search_term && username)
json.array!(should) do
........
How can I build ruby objects so that I convert them into json based on how they are initialized?
Below is the full json structure I'd like to capture in ruby models/poros (plain old ruby objects).
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"query": {
"query_string": {
"query": "tablet",
"fields": [
"standard_analyzed_name",
"standard_analyzed_message"
]
}
}
},
{
"term": {
"username": "feedmatic"
}
}
],
"must": [
{
"terms": {
"status_type": [
"3",
"4"
]
}
},
{
"range": {
"created_on": {
"gte": 20140712,
"lte": 1405134711
}
}
}
]
}
}
}
}
}
Hmm i'm not really sure about Poro's, but one thing I've seen is that when the structure starts to get hairy is to make a method that returns the hash representation of what you would like to show. Have you tried making a query method that returns a hash with that structure and then calling it in a jbuilder template?
There's an .attributes method for rails that returns a hash with the attributes, but you would have to look into how to use it with a PORO and if it works for this purpose.

Elasticsearch gem - API call format

I using this gem for elasticsearch API
I trying to convert the following curl statement to an equivalent API call
curl -X GET 'localhost:9200/_search?pretty=true' -d '{
"size": 100,
"fields": [
"#message",
"#timestamp"
],
"query": {
"term": {
"#message": "drop"
}
}
}'
I tried these but not getting intended results
Elasticsearch API
#esearch = Elasticsearch::Client.new log: true
#data2 = #esearch.search q: {
term:{
"#message" => "drop"
}
},
size:'100',
fields:'["#message", "#timestamp"]'
Transport API
client = Elasticsearch::Client.new
#data = client.perform_request 'GET', '_search', {
:size=> 100,
:query=> {
:term=> {
"message"=> "drop"
}
},
{
:fields=> [
'#message',
'#timestamp'
]
}
}
Please help
You need to wrap all of those parameters in a body element:
#data2 = #esearch.search
body: {
query: {term:{"#message" => "drop"}},
size:'100',
fields:'["#message", "#timestamp"]
}

Elasticsearch params presence

How do I construct the query JSON so that while filtering it checks for the presence of external params.
{"query": {
"filtered": {
"query": {
"match_all": {}
}}},
"filter": {
"and": {
"filters": [
{
"term": {
"locality_name": params[:locality_name] if params[:locality_name].present?
}
}
]
}
}}:
The if clause in the JSON is invalid syntax for query DSL.
I think you can combine existFilter and termFilter with and filter like this.
This will retrieve documents for which locality_name field exist and locality_name field value is equal to your specified value.
"filter" : {
"and" : [
{
"exists" : { "field" : "locality_name" }
},
{
"term" : { "locality_name" : "your_locality_name" }
}
]
}
http://www.elasticsearch.org/guide/reference/query-dsl/exists-filter/
http://www.elasticsearch.org/guide/reference/query-dsl/and-filter/

Resources