SPARQL - Speechnumber with earliest start date for party membership - date

I am looking for all speeches with certain dictionary terms in a particular time frame.
For this, I want name, country, birth, gender, partyname, start date for joining the party, date of speech, and speechnr. In the output however, I get different start versions for the same speechnr. I only want the earliest version (i.e. when they first joined the party). How do I do this?
Here is my code so far:
SELECT DISTINCT ?name ?countryname ?birth ?gender ?partyname ?start ?date ?speechnr ?parlterm ?dictionary
WHERE {
?speech lpv:spokenText ?text.
?speech dcterms:date ?date.
?speech lpv:docno ?speechnr.
?speech lpv:speaker ?speaker.
?speaker lpv:name ?name.
?speaker lpv:dateOfBirth ?birth.
?speaker lpv:gender ?gender.
?speaker lpv:politicalFunction ?function.
?function lpv:institution ?party.
?party rdf:type lpv:NationalParty.
?party rdfs:label ?partyname.
?function lpv:beginning ?start.
?speaker lpv:countryOfRepresentation ?country.
?country rdfs:label ?countryname.
BIND("8" as ?parlterm)
BIND("domestic violence" as ?dictionary)
FILTER ( ?date > "2014-07-01"^^xsd:date )
FILTER(langMatches(lang(?text), "en"))
FILTER(CONTAINS(?text, 'domestic abuse') || CONTAINS(?text, 'domestic violence') || CONTAINS(?text, 'intimate partner violence') || CONTAINS(?text, 'rape') || CONTAINS(?text, 'sexual violence') || CONTAINS(?text, 'sexual assault') || CONTAINS(?text, 'gender base violence') || CONTAINS(?text, 'gender-based violence') || CONTAINS(?text, 'violence against women') || CONTAINS(?text, 'femicide') || CONTAINS(?text, 'witch-hunting death') || CONTAINS(?text, 'femicide-suicide') || CONTAINS(?text, 'excess female mortality'))
FILTER NOT EXISTS {
?function lpv:beginning ?start2
FILTER (?start2 < ?start)
}
} ORDER BY ?date ?speechnr
As you can see, I tried to add a Filter Not exists at the end but it doesn't change anything. I would be really glad for help.
Here is the data for anyone interested:
https://linkedpolitics.project.cwi.nl/web/html/home.html

Related

How can I search based on relevance to flutter?

How can I search based on relevance to flutter? I'm not just talking about displaying the searched data. I am talking about the ones closest to the term to be searched in terms of their letters to the top. Normally the index order with listviewbuilder is 1,2,3 ... normally. But if the search term is closest to the word having the 2nd id among the data, I want the order to change to 2, 1, 3. How can I achieve this?
the codes I tried
_dataResults = _dataList.where((a) {
return (a.data_name.contains(controller.text) ||
a.data_name.toLowerCase().contains(controller.text) ||
a.data_name.toUpperCase().contains(controller.text) ||
a.data_name.startsWith(controller.text) ||
a.data_name.endsWith(controller.text) ||
a.data_title.contains(controller.text) ||
a.data_title.toLowerCase().contains(controller.text) ||
a.data_title.startsWith(controller.text) ||
a.data_title.endsWith(controller.text) ||
a.data_description.contains(controller.text) ||
a.data_description.toLowerCase().contains(controller.text) ||
a.data_description.startsWith(controller.text) ||
a.data_description.endsWith(controller.text));
}).toList();

mismatched input 'from' in rule

I am setting up a project to take in a decision table as input and create the rules.
Error encountered is "mismatched input 'from' in rule".
Fact set is :
Class Member{
double salary;
List<Tag> tagList;
string ruleResult;
}
Class Tag{
string key;
string value;
}
Decision table:
-----------------------------------------
|| Condition || Condition||
-----------------------------------------
m:Member()
tg:Tag() from tagList
-----------------------------------------
|| key==$param || salary >$1 && salary <$2
-----------------------------------------
|| Tag Conditon || sal condition ||
-----------------------------------------
|| "key1" || 1000,2000 ||
-----------------------------------------
I didnt find much documentation on usage of "from". Any help here to proceed further is greatly appreciated.
DRL example: ( I am expecting this as the output for the rule)
rule "drl_rule"
salience 65535
when
m:Member(tg:tagList,salary>1000 && salary<2000)
Tag(key=="key1", value=="value1") from tg
then
m.setRuleResult("Rule Result1");
end

why do i have a date error in my sql query?

I am doing an SQL SELECT query but I have the error message:
"SQL Error [22007]: [SQL0181] A value of date, time, or timestamp
string is incorrect."
Here is my request:
SELECT *
FROM ROXDTA400.STKF0300 A
JOIN ROXDTA400.TABJ00141 B ON A.STNSIT = B.CDSITE
WHERE ( A.STNLIB <> '-- Trémie --'
AND A.STNSIT <> 40
AND DATE(LEFT(STNDAV,4) || '-' || substr(STNDAV,5,2) || '-' || RIGHT(STNDAV,2))
BETWEEN DATE('2019-01-01') AND DATE('2019-01-04') );
The problem seems to come from the date created with the STNDAV field, because if I replace with for example DATE ('2019-01-03'), it works.
DATE (LEFT (STNDAV, 4) || '-' || substr (STNDAV, 5,2) || '-' || RIGHT (STNDAV, 2)) Gives me the correct date format.
Where would the problem come from?
thank you,
Ensure that dates stored in STNDAV are valid. I mean, check there is any invalid date such as February 30th or '99999999'. If the source is an IBM i (iSeries or AS/400), it will be faster if you avoid functions in the WHERE portion, so STNDAV BETWEEN '20190101' AND '20190104' will perform better.

Full-Text Search producing no results

I have the following View:
CREATE VIEW public.profiles_search AS
SELECT
profiles.id,
profiles.bio,
profiles.title,
(
setweight(to_tsvector(profiles.search_language::regconfig, profiles.title::text), 'B'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, profiles.bio), 'A'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, profiles.category::text), 'B'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, array_to_string(profiles.tags, ',', '*')), 'C'::"char")
) AS document
FROM profiles
GROUP BY profiles.id;
However, if profiles.tags is empty then document is empty, even if the rest of the fields (title, bio, and category) contain data.
Is there some way to make the make that field optional such that it having empty data doesn't result in an an empty document?
This seems to be the common string concatenation issue - concatenating a NULL value makes the whole result NULL.
Here it is suggested you should always provide a default value for any input with coalesce():
UPDATE tt SET ti =
setweight(to_tsvector(coalesce(title,'')), 'A') ||
setweight(to_tsvector(coalesce(keyword,'')), 'B') ||
setweight(to_tsvector(coalesce(abstract,'')), 'C') ||
setweight(to_tsvector(coalesce(body,'')), 'D');
If you do not want to provide default values for complex datatypes (like coalesce(profiles.tags, ARRAY[]::text[]) as suggested by #approxiblue), I suspect you could simply do:
CREATE VIEW public.profiles_search AS
SELECT
profiles.id,
profiles.bio,
profiles.title,
(
setweight(to_tsvector(profiles.search_language::regconfig, profiles.title::text), 'B'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, profiles.bio), 'A'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, profiles.category::text), 'B'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, coalesce(array_to_string(profiles.tags, ',', '*'), '')), 'C'::"char")
) AS document
FROM profiles
GROUP BY profiles.id;

Error thrown when trying to chain a text search with a search on a join table that uses group_by

I have three models, Story, Post and Tag. Story is in a one-to-many relationship with Post, and a many-to-many relationship with Tag.
I use the following scope to search for stories that have a least one tag that matches an array of tags:
scope :in_tags, lambda {|category_array, tag_array|
Story.joins('LEFT OUTER JOIN stories_tags ON stories_tags.story_id = stories.id').
joins('LEFT OUTER JOIN tags ON tags.id = stories_tags.tag_id').
where("tags.name in (:tag_array)", :tag_array => tag_array ).
group("stories.id")
}
Note that I use GROUP_BY so that the scope doesn't return a story multiple times if that story has multiple matching tags.
I use the following pg_search_scope to search for stories or posts with specified text:
pg_search_scope :with_text,
:against => :title,
:using => { :tsearch => { :dictionary => "english" }},
:associated_against => { :posts => :contents }
Both of these scopes work fine independent of each other. When I try to chain them together, however, I experience the postgreSQL error:
ActiveRecord::StatementInvalid (PG::Error: ERROR: column "pg_search_79dd68cf7f962ac568b3d7.pg_search_c5f43d73058486e1799d26" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...e"::text, '')) || to_tsvector('english', coalesce(pg_search_...
^
: SELECT "stories".*, ((ts_rank((to_tsvector('english', coalesce("stories"."title"::text, '')) || to_tsvector('english', coalesce(pg_search_79dd68cf7f962ac568b3d7.pg_search_c5f43d73058486e1799d26::text, ''))), (to_tsquery('english', ''' ' || 'track' || ' ''')), 0))) AS pg_search_rank FROM "stories" LEFT OUTER JOIN stories_tags ON stories_tags.story_id = stories.id LEFT OUTER JOIN tags ON tags.id = stories_tags.tag_id LEFT OUTER JOIN (SELECT "stories"."id" AS id, string_agg("posts"."contents"::text, ' ') AS pg_search_c5f43d73058486e1799d26 FROM "stories" INNER JOIN "posts" ON "posts"."story_id" = "stories"."id" GROUP BY "stories"."id") pg_search_79dd68cf7f962ac568b3d7 ON pg_search_79dd68cf7f962ac568b3d7.id = "stories"."id" WHERE (tags.name in (sports') AND (((to_tsvector('english', coalesce("stories"."title"::text, '')) || to_tsvector('english', coalesce(pg_search_79dd68cf7f962ac568b3d7.pg_search_c5f43d73058486e1799d26::text, ''))) ## (to_tsquery('english', ''' ' || 'track' || ' ''')))) GROUP BY stories.id ORDER BY latest_post_id DESC LIMIT 5000 OFFSET 0):
Is there any way to use pg_search with a scope that requires a join table with a GROUP_BY clause?
I ended up having to switch from the .group() method (which I couldn't seem to get working with pg_search) to select("DISTINCT(stories.id), stories.*). I also made use of ActiveRecords#preload so to eagerly load the associated tag records