Is it possible to get data contained in another document by id, when map function is running for some document in couchbase view? - view

I have two kinds of documents in my couchbase bucket with keys like -
product.id.1.main
product.id.2.main
product.id.3.main
and
product.id.1.extended
product.id.2.extended
product.id.3.extended
I want to write a view for documents of first kind, such that when some conditions are matched for a document, I can emit the attributes contained in the documents of first kind as well as the document of second kind.
Something like -
function(doc, meta){
if((meta.id).match("product.id.*.main") && doc.attribute1.match("value1"){
var extendedDocId = replaceMainWithExtended(meta.id)
emit(meta.id, doc.attribute1 + getExtendedDoc(extendedDocId).extendedAttribute1 );
}
}
I want to know how to implement this kind of function in couchbase views -
getExtendedDoc(extendedDocId).extendedAttribute1

Related

Filebeat Script Processor Event.Get All Fields In Log

I am looking to get all of the fields in a record in filebeat using the Script processor and perform an action on them. Using the event.Get() from the script processor, it says, "Get a value from the event (either a scalar or an object). If the key does not exist null is returned. If no key is provided then an object containing all fields is returned."
https://www.elastic.co/guide/en/beats/filebeat/current/processor-script.html
Therefore, my question is, what would I do to ensure that no key is provided to get an object that contains all of the fields are returned?
The event.Get() field will provide the top level fields. To look through these top level fields, use a for loop like:
- script:
lang: javascript
id: get_fields
source: >
function process(event) {
var a = event.Get();
for (var key in a) {
if(event.Get(key) == ""){
event.Delete(key);
}
}
}
I am unsure how to do this for nested fields in this way nor have I tried to extend it to nested fields, but this is how it works for now.

Maats Laraval Excel: Can you export with multiple queries?

I have gone through the docs and also Googled. I see little mention of returning multiple queries on the same sheet from Maat's Laravel Excel. I presume therefore it is 1 query for 1 downloaded spreadsheet. I also presume that if you do have multiple queries that you will need to place each query on an additional sheet.
Have got this right ?
Many thanks
In a perfect world, every query would get its own sheet. But in reality, it will export whatever you give it so long as it receives a single array or collection for the output, depending on your configuration. It would be up to you to determine how to combine your queries into a format that could be interpreted as rows and columns.
Basic example with two queries:
class ExportSample implements FromCollection
{
// ...
public function collection()
{
// query 1
$a = User::where('id',2)->get();
// query 2
$b = User::where('id',4)->get();
// merge collections
return $a->merge($b);
}
}
Of course, if your queries result in different column structures, there may be additional obstacles.

Elasticsearch query not returning expected results for multiple should filters

I am performing an Elasticsearch query using the high-level-rest-api for Java and expect to see records that are either active or do not have a reference id. I'm querying by name for the records and if I hit the index directly with /_search?q=, I see the results I want.
Is my logic correct (pseudo-code):
postFilters.MUST {
Should {
MustNotExist {referenceId}
Must {status = Active}
}
Should {
MustNotExist {referenceId}
Must {type = Person}
}
}
What I get are records that are active with a reference id. But, I want to include records that also do not have a referenceId, hence why I have MustNotExist {referenceId}.
For simplicity, the second Should clause can be dropped (for testing) as the first one is not working as expected by itself.
In my case, I had to use a match query instead of a term query because the value I was querying for was not a primitive or a String. For example, the part where Must, type = Person, Person was an enum, and so looking for "Person" was not quite right, whereas match allowed it to "match".

Optimize Sitecore Lucene / Solr query

I am trying to optimize some Lucene/Solr queries that are done via the Sitecore ContentSearch API. Specifically, when it comes to searching a MultiListField.
Environment:
Sitecore 8.1u2, Solr
I have the following Method for querying the values of a multilistfield:
public static Expression<Func<SearchResultItem, bool>> MultiFieldContainsExpression(IEnumerable<string> fieldNames, IEnumerable<string> ids)
{
//fieldNames = ["field_A", "field_X"]
//ids = [GUID_A, GUID_X]
Expression<Func<SearchResultItem, bool>> expression = PredicateBuilder.True<SearchResultItem>();
foreach (string fieldname in fieldNames)
{
ids.ForEach(id =>
{
expression = expression.Or(i => i[fieldName].Contains(IdHealper.NormalizeGuid(id, true)));
});
}
return expression;
}
The resulting Lucene query looks like this:
((field_A:(*GUID_A*) OR field_A:(*GUID_X*) OR field:_X:(*GUID_A*) OR field_X:(*GUID_X*)))
I want the query to be more like this (or even better if possible):
((field_A:(*GUID_A* OR *GUID_X*) OR (field_X:(*GUID_A* OR *GUID_X*)))
Basically, to check if the array of values in the field contains any value from another array. Thank you very much in advance.
Sitecore by default indexs a multilist field as a space separated list of lowercase Guids (Guid.ToString("N")). It could be useful to denormalize the relationship and store the item name or content within the item document through the use of a Computed Field. With the Computed Field it can iterate over the referenced items and turn them into a single field containing their names. You’ll still want to keep the Guid field around for cases when you want to limit the results to just a particular referenced item for the case where you know the exact Guid.

couchdb view that searches an array field for values passed in as a key array

I have some documents in couchdb that have fields that are arrays of id's for different associated documents:
{
associatedAssets: ["4c67f6241f4a0efb7dc2abc24a004dfe", "270fd4508a1222a1e2a27cbe7f002d9z"]
}
I would like to write a view that will let me pass in a key that is itself an array of ids, and then return documents whose associatedAssets fields contain one or more of the ids passed in via the key array e.g.
$.ajax({
url: "/db/_design/design_doc/_view/summaryByAssociatedAssets",
type: "post",
data: JSON.stringify({keys: ["4c67f6241f4a0efb7dc2abc24a004dfe", "6c67f6241f4a0efb7dc2abc24a004dfd"]}),
dataType: "json",
contentType: "application/json",
})
.done(function(resp){
console.log(resp[0]);
});
would return documents whose associatedAssets array contains one or more of the keys "4c67f6241f4a0efb7dc2abc24a004dfe", "6c67f6241f4a0efb7dc2abc24a004dfd".
I can't access the keys in my view, so I'm not sure if I can do this? Is there a better way to accomplish this?
Thanks!
Your view just needs to generate an output row per associatedAssets element, something like this:
function(doc) {
if( doc.associatedAssets ) {
for( var i=0, l=doc.associatedAssets.length; i<l; i++) {
emit( doc.associatedAssets[i], doc );
}
}
}
Then you'd need to adjust your call so it ends up passing that keys array as a query string parameter, which will return only the rows from the view that match keys in that array.
A total aside - assuming a recent CouchDB version, the best practices would be to replace doc in your emit with { _id: doc._id } and then use include_docs=true in your query so your view index doesn't get filled up (unnecessarily) with full documents.
In the view code you can access anything in the document itself, but there is no way to access any parameters that you pass in.
You could use temporary views that are generated for the specific query you are doing.
You could also use ElasticSearch instead. ElasticSearch has a much richer query DSL because that's the whole point of ElasticSearch. The CouchDB River allows you to automatically index all documents from CouchDB.
Using ElasticSearch, you could just search for any document whose associatedAssets contains any element from a list that you pass in.

Resources