sphinx MVA with rt_attr_string - sphinx

I am not sure about question title. But i have tried it very hard.
My rt_index conf:
type = rt
path = C:\nginx\www\public_html\sphinx\scripts\data\rt_index
rt_field = title
rt_field = property
rt_attr_string = title
rt_attr_string = brand_name
rt_attr_string = property
rt_attr_multi = categories
rt_attr_uint = price
rt_attr_uint = brand_id
I am following : https://github.com/adriannuta/SphinxFacetingExample
Here am getting unique categories and number of product related to this category.
But it only return the category id as it is "rt_attr_multi".
My query is here how I can i get the names of categories with the id ?
I have created another index of categories :
index facetcats
{
type = rt
path = C:\nginx\www\public_html\sphinx\scripts\data\facetcats
rt_field = cat_name
rt_attr_string = cat_name
}
But not able to join two index.
My goal is getting the category names without query to DB.
plz help me.

Sphinx doesn't have a string MVA. (and there is no 'joins' in sphinx)
Frankly your best idea is to just use the database to lookup the names. They will be simple PK lookups so ridiculously fast - can even cache lookups with memcache or similar.
Or you can just use a plain string attribute, store it as say comma seperated (some seperator dont use in names)
The main gotcha is sphinx will reorder MVAs into numeric order (internally), so you should presort your category names into id numeric order before storing into attribute. (so can still match up category names to ids in the attributes)

Related

Sphinx 3 index a relational database

Here is my Database scheme:
Restaurants -> id, title
Categories -> id, restaurant_id, type(enum: cafe, restaurant, fastfood, burger, chiness)
Reviews -> id, restaurant_id, body
Reviews belong to a restaurant and a restaurant belongs to multiple categories
how can we query reviews belong to a category?
here is the current config:
sql_query = select reviews.id as id, reviews.body as body, restaurants.url as url, restaurants.id as restaurant_id from reviews inner join restaurants on(restaurants.id = reviews.restaurant_id)
sql_attr_multi = uint category_id from query; SELECT restaurant_id, categories.id as category_id FROM categories
the problem is in the sql_attr_multi it replaces restaurant_id by the reviews.id! it doesn't know the restaurant_id in the sql_attr_multi and it thinks we mean the reviews.id by it!
Firstly the first column in the resultset in sql_attr_multi, should be the 'document_id' from the main query. In your example the document_id is reviews.id, but then seem to use restaruant_id in the MVA. It can't match arbitrary attributes like that.
So would actually need something like
sql_attr_multi = uint category_id from query; \
SELECT reviews.id, categories.id as category_id \
FROM categories \
INNER JOIN reviews ON(reviews.restaurant_id = categories.restaurant_id) \
ORDER BY reviews.id
To get the review id to perform the join. Split into lines to make it easier to read. Have found it best to explictly order the rows via document_id.
But that still wouldnt let you query via the 'type' , ie 'cafe' etc, because your 'categories.id' seems to be a unique id per restaurant anyway.
(ie each cafe would would a different categories.id!)
So actually seems you would want the 'type' as the MVA value. MVAs are numberic (not string), but luckily, enum is easily converted to a integer!
sql_attr_multi = uint category_type from query; \
SELECT reviews.id, type+0 \
FROM categories \
INNER JOIN reviews ON(reviews.restaurant_id = categories.restaurant_id) \
ORDER BY reviews.id
The column names in the sql_attr_multi dont matter, just uses first column as document_id, second as the MVA value.
THen knowing cafe (for example) is the first value in enum, its 1. So query reivews for cafe... (burger would be 4 for example)
SphinxQL> SELECT * FROM myindex WHERE MATCH('keyword') AND category_type = 1

Join Two table in Criteria Query

I have three tables one is ItemCategory,ItemMaster and Price. I am referring itemaCategoryId in ItemMaster table and like that referring itemmasterid in price. Now i have to display contents of price order by itemcategory id. This is my criteria query.
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Price> cq = cb.createQuery(Price.class);
Root<Price> root = cq.from(Price.class);
Root<ItemMaster> itemMasterRoot = cq.from(ItemMaster.class);
Root<ItemCategory> itemCategoryRoot = cq.from(ItemCategory.class);
Join<ItemMaster, ItemCategory> s=itemMasterRoot.join(ItemMaster_.category);
Join<Price,ItemMaster> y=root.join(Price_.itemMaster);
Path<Long> itemMasterId=root.get(Price_.itemMasterId);
cq.select(root).where(cb.equal(root.get(Price_.priceMasterId), priceMasterId))
.orderBy(cb.asc(itemMasterId));
TypedQuery<Price> q = entityManager.createQuery(cq);
Above my criteria Query
If you use multiple from statements, you get the cartesian product of all entities. If you want to preserve the relationships, use join instead:
Root<Price> price = cq.from(Price.class);
Join<Price,ItemMaster> itemMaster = price.join(Price_.itemMaster);
Join<ItemMaster, ItemCategory> itemCategory = itemMaster.join(ItemMaster_.category);
However it looks like at least the second join may be useless, because you are able to access the category property directly using the getter, isn't it?:
Price aPriceResult;
ItemCategory categoryResult = aPriceResult.getItemMaster().getCategory();

dynamic remove or add fields in a structure

how can I read consecutive structures from a file, when they have different fields, and create for each of them the appropriate fields (title: value)? I am a beginner. I think it is about dynamic adding new fields while reading i-th structure and dynamic removing the fields from the i-1 structure, which remained empty after reading a structure i. But how am I able to do it not knowing the names of all the fields before? For this I couldn't find example in documentation nor in the forum.
Thanks!
If some fields appear in every object, have them in a common structure that your array has instances of. For the variables fields, make a field "variable" or something in the main structure, and then dynamically assign field names and values within that structure. So for example, your structure might be:
a.name = 'Name1';
a.value = 'Value1';
a.variable.price = 50;
b.name = 'Name2';
b.value = 'Value2';
b.variable.year = 1996;
data(1) = a; data(2) = b;
where every object has fields "name" and "price" and object a has a price field but not a year field, and object b has a year field and no price field.
This will work for the kind of data you want to read in.

Complex SphinxQL Query

I'm trying to write a SphinxQL query that would replicate the following MySQL in a Sphinx RT index:
SELECT id FROM table WHERE colA LIKE 'valA' AND (colB = valB OR colC = valC OR ... colX = valX ... OR colY LIKE 'valY' .. OR colZ LIKE 'valZ')
As you can see I'm trying to get all the rows where one string column matches a certain value, AND matches any one of a list of values, which mixes and matches string and integer columns / values)
This is what I've gotten so far in SphinxQL:
SELECT id, (intColA = intValA OR intColB = intValB ...) as intCheck FROM rt_index WHERE MATCH('#requiredMatch = requiredValue');
The problem I'm running into is in matching all of the potential optional string values. The best possible query (if multiple MATCH statements were allowed and they were allowed as expressions) would be something like
SELECT id, (intColA = intValA OR MATCH('#checkColA valA|valB') OR ...) as optionalMatches FROM rt_index WHERE optionalMatches = 1 AND MATCH('#requireCol requiredVal')
I can see a potential way to do this with CRC32 string conversions and MVA attributes but these aren't supported with RT Indexes and I REALLY would prefer not switch from them.
One way would be to simply convert all your columns to normal fields. Then you can put all this logic inside the MATCH(..). Ie not using attributes.
Yes you can only have one MATCH per query.
Otherwise, yes you could use the CRC trick to make string attributes into integer ones, so can use for filtering.
Not sure why you would need MVA, but they are now supported in RT indexes in 2.0.2

simple LINQ query

i am having trouble joinging tables on a LINQ query.
(source: kalleload.net)
As you can see there are three tables there. On my webpage, i want to display the following data in a List View.
betid | bet.title | bet.description | match.location | match.begindate/enddate | teamone NAME | teamtwo Name.
so let me explain this.
I want 3 fields from the bets table.
I want 2 fields from the match table (where the match.matchid = bet.matchid)
I want 2 fields from the TEAM table (where match.teamone = team.teamid and match.teamtwo = team.teamid)
i hope this makes sense.
thankyou
It looks like you already have the relationships defined. If you are using the designer, you should have existing entities in the generated classes.
var result = bet.Select( b => new {
BetID = b.betid,
Title = b.title,
Description = b.description,
BeginDate = b.match.begindate,
EndDate = b.match.enddate,
TeamOneName = b.match.team_1.teamname,
TeamTwoName = b.match.team_2.teamname
} );
Note that I'm only guessing at the association names. You should be able to figure out the names given to them by the designer.
The key to this one is to include a self join on the team table, for team one and team two, like this:
var result = from bet in db.bet
join match in db.match on bet.matchid equals match.matchid
join team1 in db.team on match.teamone equals team1.teamid
join team2 in db.team on match.teamtwo equals team2.teamid
select new {bet.betid, bet.title, bet.description, match.location, match.begindate, match.enddate, team1.name, team2.name};