Determine terms for facet fields in solr - solr

I base on facet.field and I have one situation. In my store i have base products and variants, when I use facet.field I get count with base products and variants:
Category:
Chairs(30) <- this is count of base products and variants
Tables(20) <- this is count of base products and variants
I want to add some terms for facet.field in order to that facet return count only of variants, every product has field like "productType":"baseProduct" or "productType":"variantProduct"
I want to use those fields.
Any ideas? how can I use this in some query , please help

You can use facet.pivot to get distinct counts for each type:
&facet.pivot=productType,category
You can also use the JSON Facet API to do two separate facets:
{
base: {
type: terms,
field: category,
domain: { filter: "productType:baseProduct" }
},
variant: {
type: terms,
field: category,
domain: { filter : "productType:variantProduct" }
}
}

Related

How to limit facet request to get a certain number of rows

I have a Solr storage with a huge number of documents. Here's an example of my document structure:
{
"country":"USA",
"company":"Corsair",
"product":"RM650X 650W",
"price":"140",
"on_stock":"yes"
},
I'd like to make a facet request to Solr data to receive a certain number of rows (e.g. 200).
Here's a desired result:
The problem is I can't limit the data properly.
In Solr documentation it says that "facet.limit parameter specifies the maximum number of constraint counts (essentially, the number of facets for a field that are returned) that should be returned for the facet fields. This parameter can be specified on a per-field basis to apply a distinct limit to each field with the syntax of f.<fieldname>.facet.limit "
And here comes the tricky part.
I tried to use a limit of 200 for the first column (Country / Region). Here's my request:
country:{
type: terms,
field: country,
limit: 200, # Limit's here
facet:{
company:{
type: terms,
field: company,
limit: -1
facet:{
product:{
type: terms,
field: product,
limit: -1
}
}
}
This query returns 200 results for a country facet, but since every country has a different number of nested companies and every company has a different number of nested products, I get thousands of rows of data.
Then I tried to use a limit of 200 for the last column (Product). Here's my request:
country:{
type: terms,
field: country,
limit: -1,
facet:{
company:{
type: terms,
field: company,
limit: -1
facet:{
product:{
type: terms,
field: product,
limit: 200 # Limit's here
}
}
}
This query returns 200 results for every product lying withing every company lying within every country. In other words, the limit is local for every nested category, not global. And again I get thousands of rows of data.
Is it possible to achieve my goal in Solr?

Firestore: search for keywords in multiple documents

I'm using Firestore to store data for multiple entities. In this example, each document is a company with details on the products it sells, and each product is associated with multiple keywords. Example structure:
Document 1:
company_name: 'Company 1',
products: [
{
name: 'Green tea',
keywords: ['green tea', 'healthy, 'matcha']
},
{
name: 'Sushi',
keywords: ['sushi', 'rice', 'healthy']
}
]
Document 2:
company_name: 'Company 2',
products: [
{
name: 'Apple',
keywords: ['fruit', 'healthy']
},
{
name: 'Cake',
keywords: ['dessert', 'sweet']
}
]
I would like to search for companies that sell products with certain keywords. For example, by searching for the keyword healthy, both documents Company 1 and Company 2 would be returned, as they both sell foods with that keyword. How would I do this with Firestore filtering/searching?
The way you have your data structured now, with multiple field array elements containing values to search, it's not possible to have a single query find everything you want. The problem here is the arrays. It's simply not possible to search the nested contents of array fields.
When you have array elements that need to be matched individually with queries, they should instead be individual documents in a collection. Yes, that's more reads and writes, but that also means your queries become possible.
Imagine instead if your two documents didn't contain a products array field, and instead each document contained a subcollection called products where each item had a field called keywords.
companies (collection)
document 1 (doc)
company_name: string field
products (subcollection)
keywords: string array field
With this, you could then do a collection group query across all products across all companies like this (in JavaScript):
db.collectionGroup("products").where("keywords", "array-contains", keyword)
where keyword is the word you're looking for.

SOLR - group by field and then get distinct value by another field

I'm using apache solr for searching records. In my case I'm having table which has columns category and sub-category, etc.
I want to group by category and then get the distinct list of sub-category from grouped results. Is that possible in apache solr?
If yes, please do help me to solve this.
Thanks in advance.
You can do that with a pivot facet:
facet=on&facet.pivot=category,subcategory
This will give you a facet with all the sub categories for each category.
You can also use the Facet JSON API. Example adopted from that page:
top_categories:{
type: terms,
field: category,
limit: 5,
facet:{
top_subcategories:{
type: terms,
field: subcategory,
limit: 20
}
}
}

Grouped records with aggregate fields

I'm running an instance of Solr 6.2. One of the use cases I'm exploring is to return records grouped by a field, including summed columns (facets) and sorted by those columns. I realize Solr is not meant to be utilized as a relational database, but is this possible?
Using the JSON API, I send the following data payload to the query endpoint of my Solr instance:
{
query: "*:*",
filter: ["status:1", "date:[2016-10-11T00:00:00Z-7DAYS/DAY TO 2016-10-11T00:00:00Z]"],
limit: 10,
params: {
group: true,
group.field: name,
group.facet: true
},
facet: {
funcs: {
type: terms,
field: name,
sort: { sum_v1: desc },
limit: 10,
facet: {
sum_v1: "sum(v1)",
sum_v2: "sum(v2)",
sum_v3: "sum(v3)"
}
}
}
This returns 10 records at a time in both the groups key and facets key of the response JSON. However, the sorted facet buckets do not match up with the grouped records. How can I get the facet counts with the relevant groups?
The only workaround I can come up with is to do a query for the grouped records first, then do another query using the id's from that query to get the facet counts. However, the downside is that I'd lose the ability to sort or filter by any of the facet counts.

Solr facet with additional metadata

Is it possible to use additional metadata fields when using Solr facets? I would like to aggregate one attribute by counting them and desplaying the related group as additional metadata field.
http://localhost:8983/solr/gitIndex/select?indent=on&q=*:*&rows=0&wt=json&
json.facet={
Repository_s: {
type: terms,
field: Repository_s,
limit: 10,
facet: {
x:"count()"
}
}
}
The result should look like this:
...
"facets":{
"count":1354013,
"<name of attribute>":{
"buckets":[{
"val":"<value of attribute>",
"count":173997,
"<metadata_field>":<value of metadata_field>},
...
A solution is to use facet pivots - it'll get you any values in a secondary field under each facet, and if the value is unique for the set of documents, it'll just be a single value.
The reference guide has the syntax for non-json facets.

Resources