Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Search parent with same type

Sorry if this is a duplicate (I did try searching), or if this is a silly question. New to posting questions.

I am trying to do parent child relations and queries in ElasticSearch with the following:

    #!/bin/bash

curl -XDELETE 'http://localhost:9200/test/'
echo

curl -XPUT 'http://localhost:9200/test/' -d '{
"settings" : {
        "index" : {
            "number_of_shards" : 1
        }
    }
}'
echo

curl -XPUT localhost:9200/test/_mapping/nelement -d '{
        "nelement" : {
            "_id" : { "path" : "nid", "store" : true, "index" : "not_analyzed"},
            "_parent" : { "type" : "nelement"},
            "properties" : {
                "name" : { "type" : "string",  "index" : "not_analyzed" },
                "nid": { "type" : "string", "copy_to" : "_id" }
            }
        }
}'
echo

#curl -s -XPOST localhost:9200/_bulk --data-binary @test_data.json
test_data.json is as follows:
{"index":{"_index":"test","_type":"element", "_parent":"abc"}
{"nid":"1a","name":"parent1"}
{"index":{"_index":"test","_type":"element", "_parent":"1a"}
{"nid":"2b","name":"child1"}
{"index":{"_index":"test","_type":"element", "_parent":"2b"}
{"nid":"2c","name":"child2"}


curl -XGET 'localhost:9200/test/nelement/_search?pretty=true' -d '{

 "query": {
    "has_child": {
      "child_type": "nelement", 
      "query": {
        "match": {
          "nid": "2c"
        }
      }
    }
  }

}'



echo
echo
curl -XGET 'localhost:9200/test/nelement/_search?pretty=true' -d '{

 "query": {
    "has_parent": {
      "type": "nelement", 
      "query": {
        "term": {
          "nid": "2b"
        }
      }
    }
  }

}'

For some reason, my search queries get no results. I have confirmed that the objects are indexed....

like image 837
r-k Avatar asked Jul 30 '15 03:07

r-k


People also ask

Can we join two indexes in Elasticsearch?

While fetching data from elasticsearch can we join two indexes in query. Is it possible? No, Elasticsearch does not support joins between indices.

Is Elasticsearch good for relational data?

Because Elasticsearch is not a relational database, joins do not exist as a native functionality like in an SQL database. It focuses more on search efficiency as opposed to storage efficiency. The stored data is practically flattened out or denormalized to drive fast search use cases.

How do you join in Elasticsearch?

Joining queriesedit Instead, Elasticsearch offers two forms of join which are designed to scale horizontally. Documents may contain fields of type nested . These fields are used to index arrays of objects, where each object can be queried (with the nested query) as an independent document.


1 Answers

Because you are using self referential(set parent and query in the same index type) to parent/child query.

For now Elasticsearch is not supporting it.

Explore parent/child self referential support

like image 89
chengpohi Avatar answered Oct 10 '22 10:10

chengpohi