How to append or replace document field in elastic search php? - php

I am trying to write update query which replace or append field content
$params = [
'index' => 'products',
'type' => 'product',
'id' => '57b31c5c04736da90a91bc2b',
'body' => [
'script' => 'ctx._source.product_content_changes = ctx._source.product_content)',
]
];
$result =$client->update($params);
My requirement is to replace or append content in product_content field.
For example
product_content => This Is Test Text i want to replace Test to Changed so new field will contain this text This Is Changed Text.
In mysql this query will be something like this
update products set product_content_changes = replace(product_content,'Test','Changed') where id="57b31c5c04736da90a91bc2b";
Thanks

Related

How do I map an index created by fscrawler so that I can do exact full text search on the document?

I have an index of binary files created by fscrawler(has a default mapping).
I am querying my index using php-elasticsearch:
if ($q2 == '') {
$params = [
'index' => 'trial2',
'body' => [
'query' => [
'term' => [
'content' => $q
]
]
]
];
$query = $client->search($params);
$data['q'] = $q;
}
I am trying to do an exact full text search on the content field (body). how do i do it?
You probably need to change the default mapping and use a keyword data type for the content field. That being said, it won't allow searching for individual terms anymore.
Is that really what you want?
May be what you are after is actually using a match query instead of a term query?

Prestashop admin panel custom filter for comma-separated column

here my comma separated column is shops(eg. 1,2,3,4), and with the call back i am already displaying the multiple shops names. with call back i am showing the related shop-names(values shows eg. Shop1,Shop2,Shop3,Shop4).
is there a way i can filter it with the values i am displaying.
$this->fields_list = array(
'id_push' => array('title' => $this->l('ID')),
'shops' => array('title' => $this->l('Shop(s)'),'callback' => 'getShopName','type'=>'editable')
);
You should include a concatenated shop name field in your controller SELECT. Then you should specify filter_key parameter in your shops fieldlist field. Something like this:
$this->_select = ' a.`correct_field_name` AS `shopnames_custom_field`';
$this->fields_list = array(
'id_push' => array('title' => $this->l('ID')),
'shops' => array('title' => $this->l('Shop(s)'),'callback' => 'getShopName','type'=>'editable', 'filter_key' => 'shopnames_custom_field')
);
If this solution does not work you should modify getList function to custom filter results.
Good luck

Use * (asterix) as a term query in Elastic search

I have a document with a tag '*'
Yet when I construct a term query it returns no results. How can I query documents with the tag '*'. My guess is it's a special character that needs to be escaped.
Update with answer
I needed to set the property to not analyzed so that elastic search wouldn't strip out punctuation etc.
$myTypeMapping = array(
'_source' => array(
'enabled' => true
),
'properties' => array(
'tag' => array("type" => "string", "index" => "not_analyzed")
)
);
$indexParams['body']['mappings']['file'] = $myTypeMapping;
If your tag field is analyzed then the the star is not indexed. See for yourself:
curl -XGET 'localhost:9200/_analyze?analyzer=standard' -d '*'
Response:
{"tokens":[]}
You will need to change the field to not_analyzed or to change the analyzer.

Using elasticsearch, how to create an index for a document that contains an array, and append to that array in the future

In my example code I am using the php client library, but it should be understood by anyone familiar with elasticsearch.
I'm using elasticsearch to create an index where each document contains an array of nGram indexed authors. Initially, the document will have a single author, but as time progresses, more authors will be appended to the array. Ideally, a search could be executed by an author's name, and if any of the authors in the array get matched, the document will be found.
I have been trying to use the documentation here for appending to the array and here for using the array type - but I have not had success getting this working.
First, I want to create an index for documents, with a title, array of authors, and an array of comments.
$client = new Client();
$params = [
'index' => 'document',
'body' => [
'settings' => [
// Simple settings for now, single shard
'number_of_shards' => 1,
'number_of_replicas' => 0,
'analysis' => [
'filter' => [
'shingle' => [
'type' => 'shingle'
]
],
'analyzer' => [
'my_ngram_analyzer' => [
'tokenizer' => 'my_ngram_tokenizer',
'filter' => 'lowercase',
]
],
// Allow searching for partial names with nGram
'tokenizer' => [
'my_ngram_tokenizer' => [
'type' => 'nGram',
'min_gram' => 1,
'max_gram' => 15,
'token_chars' => ['letter', 'digit']
]
]
]
],
'mappings' => [
'_default_' => [
'properties' => [
'document_id' => [
'type' => 'string',
'index' => 'not_analyzed',
],
// The name, email, or other info related to the person
'title' => [
'type' => 'string',
'analyzer' => 'my_ngram_analyzer',
'term_vector' => 'yes',
'copy_to' => 'combined'
],
'authors' => [
'type' => 'list',
'analyzer' => 'my_ngram_analyzer',
'term_vector' => 'yes',
'copy_to' => 'combined'
],
'comments' => [
'type' => 'list',
'analyzer' => 'my_ngram_analyzer',
'term_vector' => 'yes',
'copy_to' => 'combined'
],
]
],
]
]
];
// Create index `person` with ngram indexing
$client->indices()->create($params);
Off the get go, I can't even create the index due to this error:
{"error":"MapperParsingException[mapping [_default_]]; nested: MapperParsingException[No handler for type [list] declared on field [authors]]; ","status":400}
HAD this gone successfully though, I would plan to create an index, starting with empty arrays for authors and title, something like this:
$client = new Client();
$params = array();
$params['body'] = array('document_id' => 'id_here', 'title' => 'my_title', 'authors' => [], 'comments' => []);
$params['index'] = 'document';
$params['type'] = 'example_type';
$params['id'] = 'id_here';
$ret = $client->index($params);
return $ret;
This seems like it should work if I had the desired index to add this structure of information to, but what concerns me would be appending something to the array using update. For example,
$client = new Client();
$params = array();
//$params['body'] = array('person_id' => $person_id, 'emails' => [$email]);
$params['index'] = 'document';
$params['type'] = 'example_type';
$params['id'] = 'id_here';
$params['script'] = 'NO IDEA WHAT THIS SCRIPT SHOULD BE TO APPEND TO THE ARRAY';
$ret = $client->update($params);
return $ret;
}
I am not sure how I would go about actually appending a thing to the array and making sure it's indexed.
Finally, another thing that confuses me is how I could search based on any author in the array. Ideally I could do something like this:
But I'm not 100% whether it will work. Maybe there is something fundemental about elasticsearch that I am not understanding. I am completely new to so any resources that will get me to a point where these little details don't hang me up would be appreciated.
Also, any direct advice on how to use elasticsearch to solve these problems would be appreciated.
Sorry for the big wall of text, to recap, I am looking for advice on how to
Create an index that supports nGram analysis on all elements of an array
Updating that index to append to the array
Searching for the now-updated index.
Thanks for any help
EDIT: thanks to #astax, I am now able to create the index and append to the value as a string. HOWEVER, there are two problems with this:
the array is stored as a string value, so a script like
$params['script'] = 'ctx._source.authors += [\'hello\']';
actually appends a STRING with [] rather than an array containing a value.
the value inputted does not appear to be ngram analyzed, so a search like this:
$client = new Client();
$searchParams['index'] = 'document';
$searchParams['type'] = 'example_type';
$searchParams['body']['query']['match']['_all'] = 'hello';
$queryResponse = $client->search($searchParams);
print_r($queryResponse); // SUCCESS
will find the new value but a search like this:
$client = new Client();
$searchParams['index'] = 'document';
$searchParams['type'] = 'example_type';
$searchParams['body']['query']['match']['_all'] = 'hel';
$queryResponse = $client->search($searchParams);
print_r($queryResponse); // NO RESULTS
does not
There is no type "list" in elasticsearch. But you can use "string" field type and store array of values.
....
'comments' => [
'type' => 'string',
'analyzer' => 'my_ngram_analyzer',
'term_vector' => 'yes',
'copy_to' => 'combined'
],
....
And index a document this way:
....
$params['body'] = array(
'document_id' => 'id_here',
'title' => 'my_title',
'authors' => [],
'comments' => ['comment1', 'comment2']);
....
As for the script for apending an element to array, this answer may help you - Elasticsearch upserting and appending to array
However, do you really need to update the document? It might be easier to just reindex it as this is exactly what Elasticsearch does internally. It reads the "_source" property, does the required modification and reindexes it. BTW, this means that "_source" must be enabled and all properties of the document should be included into it.
You also may consider storing comments and authors (as I understand these are authors of comments, not the document authors) as child document in ES and using "has_child" filter.
I can't really give you specific solution, but strongly recommend installing Marvel plugin for ElasticSearch and use its "sense" tool to check how your overall process works step by step.
So check if your tokenizer is properly configured by running tests as described at http://www.elastic.co/guide/en/elasticsearch/reference/1.4/indices-analyze.html.
Then check if your update script is doing what you expect by retrieving the document by running GET /document/example_type/some_existing_id
The authors and comments should be arrays, but not strings.
Finally perform the search:
GET /document/_search
{
'query' : {
'match': { '_all': 'hel' }
}
}
If you're building the query yourself rather than getting it from the user, you may use query_string with placeholders:
GET /document/_search
{
'query' : {
'query_string': {
'fields': '_all',
'query': 'hel*'
}
}
}

How to update/replace a field in an ElasticSearch document using PHP?

I want to update my Elasticsearch indexed document's field. In my case its the tags field.
This is the code I currently have:
// Index tags in the page document
$es_client->update([
'index' => 'myappname',
'type' => 'page',
'id' => $page_id,
'body' => [
'doc' => [
'tags' => $tagsArray
]
]
]);
So, this would update my document by adding the tags array to it, but it won't remove the old tags.
How can I make sure that the old tags get removed when I add the new tags?
I did look in the documentation, but as we all know, the Elasticsearch docs can be very confusing and all-over-the-place. Hence I am asking here after days of searching.
Any help or advice would be greatly appreciated.
Standard update behavior is to merge array/object fields as explained in the update API documentation .
...objects are merged together, existing scalar fields are overwritten
and new fields are added.
So instead you would use a script to modify the document source directly. You can make it generic and thus cacheable, and pass in params for better performance. Php API documentation
// Index tags in the page document
$es_client->update([
'index' => 'myappname',
'type' => 'page',
'id' => $page_id,
'body' => [
'script' => 'ctx._source.tags=tags',
'params' => ['tags' => $tagsArray]
]
]);

Categories