How to write into multiple data node via PHP API

Hello

i am using PHP API to write data into 2 nodes of the master node in single cluster

how i can save the specific data into specific node via PHP REST API

is there any setting to define in the REST API that Data will save in particular node

Not clear what you want to do but basically when you write data, you write it within an index.
The index by default can be allocated anywhere on your cluster.

If you want to allocate your index only on a specific set of machines, then you can tag them and use then shard allocation filtering. See https://www.elastic.co/guide/en/elasticsearch/reference/5.4/shard-allocation-filtering.html

But I'm curious on the reason you think that might be a good idea in a 2 nodes context. (BTW 3 nodes should be the minimal for production to avoid split brain issues)

hello

here is my structure

Master Node = 1
data node = 2

Index = 2

i want to write the first index only in first data node and second only in second data node

but the post of the node is 9301 and 9302 that not allowed to access via REST API

REST API only accept 9200- 9300

hope you understand now

Yes. Still I don't understand why you want to do that and not let elasticsearch distribute the indices on the cluster.

Anyway look at https://www.elastic.co/guide/en/elasticsearch/reference/5.4/shard-allocation-filtering.html

1 Like

hello i understand that

thanks

Hum... I think I understand your question.

So you have 2 nodes running on the same machine?
Then you want to send index requests to one of the node and other index requests to the other node. Is that right?

If so, just send requests either to:

or

you are right boss

now you understand me

i read your document to create the index and assign that to node via attributes of shard allocation filters

that i am trying now

so do you think i am on the right track ?

Yes. May be.

I mean that you did not tell WHY you want to do that. What problem you think you are trying to solve here?

now i am facing issue

{"error":{"root_cause":[{"type":"unavailable_shards_exception","reason":"[node_2][0] primary shard is not active Timeout: [1m], request: [BulkShardRequest [[node_2][0]] containing [index {[node_2][test_document][test_document_1], source[{\"testField\":\"abc\"}]}]]"}],"type":"unavailable_shards_exception","reason":"[node_2][0] primary shard is not active Timeout: [1m], request: [BulkShardRequest [[node_2][0]] containing [index {[node_2][test_document][test_document_1], source[{\"testField\":\"abc\"}]}]]"},"status":503}

when i am creating document in the shard of index

here is my nodes log

{
  "cluster_name" : "cs",
  "version" : 4,
  "state_uuid" : "aEflY06dQBW4DZdYG2RuUQ",
  "master_node" : "xs6GUEkCQ9GE9xhUfQPk-A",
  "blocks" : { },
  "nodes" : {
    "H9ROPBHaT5GvR_DKj42W6w" : {
      "name" : "node_2",
      "ephemeral_id" : "xLFEyG7GRaKmIjkq7LFXuw",
      "transport_address" : "127.0.0.1:9301",
      "attributes" : {
        "size" : "big",
        "rack" : "node_2"
      }
    },
    "xs6GUEkCQ9GE9xhUfQPk-A" : {
      "name" : "node_1",
      "ephemeral_id" : "Eq5BP4mxTj6LIRQ4sFw_mw",
      "transport_address" : "127.0.0.1:9300",
      "attributes" : { }
    }
  },
  "metadata" : {
    "cluster_uuid" : "7JtoIEjOTSW9BdPj4sRlSQ",
    "templates" : { },
    "indices" : {
      "node_2" : {
        "state" : "open",
        "settings" : {
          "index" : {
            "routing" : {
              "allocation" : {
                "include" : {
                  "_name" : "node_2",
                  "size" : "big",
                  "rack" : "node_2"
                }
              }
            },
            "number_of_shards" : "5",
            "provided_name" : "node_2",
            "creation_date" : "1495718156182",
            "number_of_replicas" : "1",
            "uuid" : "k0K7KBonQc6lhr4-rngoXQ",
            "version" : {
              "created" : "5040099"
            }
          }
        },
        "mappings" : { },
        "aliases" : [ ],
        "primary_terms" : {
          "0" : 0,
          "1" : 0,
          "2" : 0,
          "3" : 0,
          "4" : 0
        },
        "in_sync_allocations" : {
          "2" : [ ],
          "3" : [ ],
          "1" : [ ],
          "4" : [ ],
          "0" : [ ]
        }
      }
    },
    "index-graveyard" : {
      "tombstones" : [ ]
    }
  },
  "routing_table" : {
    "indices" : {
      "node_2" : {
        "shards" : {
         
          "1" : [
            {
              "state" : "UNASSIGNED",
              "primary" : true,
              "node" : null,
              "relocating_node" : null,
              "shard" : 1,
              "index" : "node_2",
              "recovery_source" : {
                "type" : "EMPTY_STORE"
              },
              "unassigned_info" : {
                "reason" : "INDEX_CREATED",
                "at" : "2017-05-25T13:15:56.274Z",
                "delayed" : false,
                "allocation_status" : "deciders_no"
              }
            },
            {
              "state" : "UNASSIGNED",
              "primary" : false,
              "node" : null,
              "relocating_node" : null,
              "shard" : 1,
              "index" : "node_2",
              "recovery_source" : {
                "type" : "PEER"
              },
              "unassigned_info" : {
                "reason" : "INDEX_CREATED",
                "at" : "2017-05-25T13:15:56.274Z",
                "delayed" : false,
                "allocation_status" : "no_attempt"
              }
            }
          ],
         
          ]
        }
      }
    }
  },
  
    "nodes" : {
      "H9ROPBHaT5GvR_DKj42W6w" : [ ]
    }
  }
}

when i told you that i want to create the multiple nodes in the server because i want to give test for few clients in free

Just create one index per use case in the same node. You don't need multiple nodes and complicate things IMO.

1 Like

ok but what is solution of error just let me know

May be you removed one node and you don't have replicas so no replica has been promoted.

Hard to say without knowing what you are doing.

But again you should really take advices that we are giving on this list seriously. Specifically I think that you have little experience with elasticsearch and you don't know yet all the internals.
If I'm right with this statement I'd encourage you reading https://www.elastic.co/guide/en/elasticsearch/guide/current/getting-started.html to get a better understanding of what is happening behind the scene.
Specifically this chapter: https://www.elastic.co/guide/en/elasticsearch/guide/current/distributed-docs.html

i already send you the log

here is code that i am running

$hosts = [
'127.0.0.1:9200'
];
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
->build();
$params = [
'index' => 'node_2',
'type' => 'test_document',
'id' => 'test_document_1',
'body' => [ 'testField' => 'abc']
];

	// Document will be indexed to my_index/my_type/my_id
	$response = $client->index($params);
   
	echo "<pre>";       
	print_r($response);
	exit;
    echo "<pre>";
	print_r($client);
	exit;

That's useless. Sorry I can't help.

thanks to support me so far

i used code from your document and if that is useless then i think you have to update your document as well

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