Boost solr results based on a field value - solr

I am new to solr and trying to fetch results from it. I want results which exactly matches my phrase ex: "test test1 test2" and any document which has any of these keywords, so kind of union of exact match and partial matches.
I am pretty much achieving it using dismax. Now i want the results to be sorted/boosted on the basis of some field value eg: cat:A, then cat:B then cat:C also I want this sorting to be applied first on exactly matched result then partial matched results.
Indexed data is:
[
{
"id": "001",
"name": "test test1 test2 Z",
"price": 12.9,
"cat": "A"
},
{
"id": "002",
"name": "test test1 test2 X",
"price": 91.5,
"cat": "B"
},
{
"id": "003",
"name": "test test1 test2 R",
"price": 45.5,
"cat": "C"
},
{
"id": "004",
"name": "test test1 test2 C",
"price": 78.67,
"cat": "B"
},
{
"id": "005",
"name": "data",
"price": 91.5,
"cat": "A"
},
{
"id": "008",
"name": "test test1 test2 D",
"price": 45.09,
"cat": "A"
},
{
"id": "009",
"name": "test test1 test2 B",
"price": 34.09,
"cat": "B"
},
{
"id": "010",
"name": "test test1 test2 A",
"price": 39.19,
"cat": "C"
},
{
"id": "011",
"name": "test this data",
"price": 89.19,
"cat": "A"
},
{
"id": "012",
"name": "this is my data",
"price": 89.19,
"cat": "C"
},
{
"id": "013",
"name": "test1 is this data title",
"price": 89.19,
"cat": "A"
}
]
Please help.Thanks in advance.

Related

In Azure Search how can I filter records that have tags with IDs matching any in a list

Given the following search index document schema:
{
"value": [
{
"Id": "abc",
"Name": "Some name",
"Tags": [
{
"Id": "123",
"Name": "Tag123"
},
{
"Id": "456",
"Name": "Tag456"
}
]
},
{
"Id": "xyz",
"Name": "Some name",
"Tags": [
{
"Id": "123",
"Name": "Tag123"
},
{
"Id": "789",
"Name": "Tag789"
}
]
},
]
}
What is the correct syntax for an OData query that will return all records with any Tag/Ids that are contained in input list?
The closest I have got is:
Tags/any(object: object/Id search.in ('123,456,789'))

Sort child documents

In Solr, how can I sort the child documents by score? As I tested, it seems to be only possible to sort the documents and not the child documents that are retrieved inside the parent document.
I have the following document:
{
"id": 0,
"type": "parent",
"name": "Arnold",
"_childDocuments_": [
{
"id": 1,
"type": "child",
"field": "foo bar"
},
{
"id": 2,
"type": "child",
"field": "foo baz"
},
{
"id": 3,
"type": "child",
"field": "bar baz"
},
{
"id": 4,
"type": "child",
"field": "foobar baz bar"
}
]
}
Now I want to filter by foo baz. I'm using:
q= {!parent which=type:parent}
fl= *, [child parentFilter=type:parent childFilter="field:foo OR field:baz"]
score= score desc
Since ID 2 is foo baz, I would expect to see this one as the first child document retrieved, but I'm seeing ID 1 foo bar as the first one because ID 1 was the first one to be inserted.
Instead of using the [child] transformer, you can use the [subquery] transformer.
q= {!parent which=type:parent}
fl= *, my_childs:[subquery]&my_childs.q=field:foo OR field:baz&my_childs.fl=*, score
Results in:
"response": {
"numFound": 1,
"start": 0,
"docs": [
{
"id":"0",
"type": ["parent"],
"name": ["Arnold"],
"_version_": 1603334242311340032,
"name_str": ["Arnold"],
"type_str": ["parent"],
"my_childs": {
"numFound": 4,
"start": 0,
"docs":[
{
"id": "2",
"type": ["child"],
"field": ["foo baz"],
"field_str": ["foo baz"],
"_version_": 1603334242311340032,
"type_str": ["child"],
"score": 1.0998137
},
{
"id": "1",
"type": ["child"],
"field": ["foo bar"],
"field_str": ["foo bar"],
"_version_": 1603334242311340032,
"type_str": ["child"],
"score": 0.7261542
},
{
"id": "3",
"type": ["child"],
"field": ["bar baz"],
"field_str": ["bar baz"],
"_version_": 1603334242311340032,
"type_str": ["child"],
"score": 0.3736595
},
{
"id": "4",
"type": ["child"],
"field": ["foobar baz bar"],
"field_str": ["foobar baz bar"],
"_version_": 1603334242311340032,
"type_str": ["child"],
"score": 0.31387395
}
]
}
}
]
}

Get specific object from JSONPath array result after predicate is applied

In JSONPath 0.9.1 the following Json path was valid:
http://jsonpath.herokuapp.com/?path=$.store.book[?(#.author==%27Nigel%20Rees%27)][0]
returning
{
"category" : "reference",
"author" : "Nigel Rees",
"title" : "Sayings of the Century",
"price" : 8.95
}
I've upgraded to the latest one (2.3) and the query now returns empty array.
Is this a bug or the way to retrieve an element from the resulting array has changed?
Given this document:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
Using JsonPath 2.3.0, the following code returns a JSONArray (rather than an Object[]):
JsonPath.parse(JSON).read("$.store.book[?(#.author==\"Nigel Rees\")]");
So, the following code ...
JSONArray read = JsonPath.parse(JSON).read("$.store.book[?(#.author==\"Nigel Rees\")]");
System.out.println(read.get(0));
... will print:
{category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95}

How to retrieve data from two objects in json file?

If I have records for users and also records for orders, it is proper to save them like that:
{
"users": [
{"id": "1", "name": "Allan", "age": "40"},
{"id": "2", "name": "Jack", "age": "50"}
],
"orders": [
{ "id": "1", "item": "item 1", "userId": "1"},
{ "id": "2", "item": "item 3", "userId": "1"},
{ "id": "1", "item": "item 4", "userId": "1"},
{ "id": "1", "item": "item 51", "userId": "1"},
{ "id": "2", "item": "item 4", "userId": "1"}
]
}
as you can see I'm coming from the SQL world so I'm saving the 'orders' data with foreign key, is that the right way to save this data as json and if so how can I get all the users with there orders?
Thanks!
If your json object is in the above mentioned structure you can use this code to filter the orders
var oJson = {
"users": [
{"id": "1", "name": "Allan", "age": "40"},
{"id": "2", "name": "Jack", "age": "50"}
],
"orders": [
{ "id": "1", "item": "item 1", "userId": "1"},
{ "id": "2", "item": "item 3", "userId": "1"},
{ "id": "1", "item": "item 4", "userId": "1"},
{ "id": "1", "i`enter code here`tem": "item 51", "userId": "1"},
{ "id": "2", "item": "item 4", "userId": "1"}
]}
for(var i=0; i<oJson.users.length;i++){
for(var j=0; j<oJson.orders.length;j++){
if (oJson.users[i].id == oJson.orders[j].userId) {
console.log( oJson.orders[j].item, oJson.orders[j].userId)
//your logic here
}
}
}
But it is better to use sql joints to merge the orders and return the objects like this
{
"users": [
{"id": "1", "name": "Allan", "age": "40","orders": [{ "id": "1", "item": "item 1", "userId": "1"},{ "id": "2","item": "item 3", "userId": "1"},{ "id": "1", "item": "item 4", "userId": "1"},{ "id": "1", "item": "item 51", "userId": "1"},{ "id": "2", "item": "item 4", "userId": "1"}
]},
{"id": "2", "name": "Jack", "age": "50","orders":[]}
]}
Fetch the objects for json file into two varible
var a=value.users;
var b=value.orders;
value is nothing but the whole json file
then you can match them against each other like
a[0].id==b.[0].id

How can i handle this scenario

I am using Backbone.js, unexpectedly i am in the confused position now, because of this scenario..
my test object :
test: {
"links": {
"nameLink": {
"name": "name 1",
"name": "name 2",
"name": "name 3"
},
"ageLink": {
"age": "1",
"age": "2",
"age": "3"
}
},
"data": {
"element": {
"project": "project 1",
"title": "title1",
"brand": "brand1"
},
"element": {
"project": "project 2",
"title": "title2",
"brand": "brand2"
},
"element": {
"project": "project 3",
"title": "title3",
"brand": "brand3"
}
}
}
I do have the josn data from server, it has the project name, links(there is nested links under), time stamps.
how can i split the data, and append appropriate UL elements. ( by name, by age ect.)
On click on the name, age links, how can sort (by name, age) the datas i appended to body tag? - without sorting the links menu?
How can i keep all this updated?
any good suggestion please?

Resources