fergiemcdowall / search-index

A persistent, network resilient, full text search library for the browser and Node.js
MIT License
1.39k stars 149 forks source link

Using teaser in express route/view #237

Closed joshterrill closed 8 years ago

joshterrill commented 8 years ago

I would like to use the teaser option on my query, but how do I get it to work in the frame of an express route/view? I'm using res.render("results", {results: searchResults}) to pass my results set to my results view. How can I use teaser in this view? The documentation is unclear in a scenario like this.

fergiemcdowall commented 8 years ago

You specify the field to generate a teaser on. The teaser is then returned as a field in each result, with the search terms marked up by html. You can then choose to highlight the search terms (or not) any way you want.

If you search for "teaser" in the /test directory, you will find some examples.

joshterrill commented 8 years ago

I saw a couple of those but I still didn't find anything that really shows how someone would do it using express routes and handlebars. I'm rendering my search results by passing the results object into my route. And when I render the text in the object, it's just text. I didn't see any teaser text on my result object, wrapped in the element that I specified inside of my result. Maybe I'm overlooking something. Can you show me what the response object should look like when a teaser is specified?

fergiemcdowall commented 8 years ago

Say you search for "jellybabies", the teaser might look a bit like this:

Truly the kings of all sweeties are the <span class="sc-em">jellybabies</span> for they
are the bringers of energy and happiness in times of need

You would then parse the string as HTML. Web frameworks sometimes "sanitise" strings, so you might have to fiddle around a bit to render the HTML.

joshterrill commented 8 years ago

It seems like from the test cases that that should be in the object but I don't see that in my response object. Here's what I get in mine. Sorry if the formatting is off I'm pasting this in from my cell phones browser.

{"totalHits":1,"totalDocsInIndex":2,"documentFrequencies":{"*○restitution":1},"fieldWeight":{"*○restitution":1},"query":{"query":{"*":["restitution"]},"teaser":"div","maxFacetLimit":100,"offset":0,"pageSize":100},"facets":{},"hits":[{"id":"1-797be351eb84d02a3880aa0590ea640f4e0b4b90","tf":[["*○restitution","2.3529073624776258"]],"score":1.1226221130244565,"document":{"title":"Restitution","filename":"restitution.rtf","mimetype":"application/msword","text":"Introduction It is traditional to speak of criminals paying their debt to society by serving time in prison. This, however, is a puzzling expression, since spending time in a small cell, probably with a couple of other inmates, doesn't seem to pay back anyone, anything. The question must be asked: is a conventional prison sentence a fair way to make restitution for a crime? What is a crime? In law, a crime is a transgression of a rule, laid down by society. So, in the abstract sense, a crime is disobedience in action. However, most legal rules but not all exist to circumscribe harm to some party or other. Many laws exist to outlaw the harming of protected parties. To disobey the rule is, therefore, to bring harm to someone or something, or other. Modern punishment Typical punishments, in the modern world, consist of fines, time in jail, or service to the community a non-jail period of work, for the community, served without pay. There is a basic problem with these three forms of punishment as typically presently practiced. In general (from a lifetime's personal observation) there does not seem to be much correspondence between the sentence be it a fine, jail time, or community service and the true harm done to society, individual people or organizations. In many cases, that I have noted, the punishment seems notional: that is a figure is plucked from the air, be it a fine or jail time etc, to symbolize that the perpetrator is being punished. The problem is that, generally speaking, these notional punishments do not reflect the magnitude of the real world harm done. Note that I have excluded from conversation a fourth category of punishment : this is that category that amounts to little more than the suggestion that the perpetrator has been a naughty boy/girl such as the UK's ASBOs (Anti-social behaviour orders) and referral orders and invite them to be good in future. These do not, in my view, have any real, effective component of punishment about them and are simply symbolic of society's disapproval. How do we measure the harm done to society by a criminal? Each crime committed by a perpetrator should be analyzed for its quantum of social harm. This will be unique to each crime. For some crimes it is not difficult. For instance, in the recent London riots of August 2011, an arsonist set fire to the House of Reeves furniture store in Croydon, Surrey [1]. The quantum of harm here is the total value of the property, plus its entire contents of furniture, plus all the business lost during the period of presumed rebuilding. Undoubtedly this will run into many millions of pounds. It may also be fair to factor in compensation for the distressed caused to the Reeves family, through seeing their family business, five generations old, going up in flames. Then again, other crimes are not so easy to assess. What is the harm done by slashing the face of a young girl? This would depend on the characteristics of the girl. Firstly, the harm would include the full cost of any plastic surgery to correct the problem. It would also include a sum to compensate the girl for her mental distress and any attendant social problems she might face, for the duration of her life, from any scarring or indeed, any relic fear she might have of going out in the world thereafter. If the damage cannot be successfully repaired, and is such that it might prevent her ever marrying and having a family, then she should be compensated for the loss of her future family and all the descendants she would otherwise have had. This sum could be calculated based on the notional value of a human life multiplied by the average size of a family, in her country, across the two generations she would have probably seen (children and grandchildren). Furthermore, if the girl had been a professional who uses her face for her work such as a model or actress she should be compensated for the lifetime value of work lost owing to her changed appearance. Again the total damage of this crime could run into millions of pounds. What about England's many looters, recently? Typically, they smashed windows and stole goods. The total damage to society is an amalgam of the cost of the windows broken and any other physical harm to the shop; the value of the property taken; the quantum of business lost during the repair/rebuilding period and any future loss of business due to damage to the reputation and standing of the shopping area in which the riot occurred. Also a quantum should be added for any distress caused to the staff or owners of the shop. This total is likely to be substantially more than one might initially expect. Using like reasoning, any crime may be assessed for its total, overall harm to society. It is this sum that constitutes the debt to society incurred by the criminal. The value of a human life For cases involving murder, there is the question of what value to put on a human life. There is much disagreement on the issue, from agency to agency and nation to nation [2]. However, it is usual for a value to be proposed which serves to represent the value of any life. The Environmental Protection Agency of America, for instance, set that value at 9.1 million USD, last year [2]. From the point of view of the social harm caused by a death, it is clear that different people will have a different value to their societies. Although this might be difficult to accept and may not be seen as politically correct , not all people make the same level of contribution to their societies and so not all people can be said to have the same value to their societies. Thus, I wish to propose that, for the purposes of assessing the debt to society of a murderer: The value of a human life = the conventionally accepted value of a life + the particular value to society of that individual. Thus, it is that everyone will have a baseline value equal to the conventionally accepted value of a life to which would be added a variable sum, depending on the value of that person's contributions to society. This social value of a person is not necessarily dependent on the financial value of a person, to society, but would be influenced by the rarity of their skills. Thus, a Nobel Prize Winning scientist, would be vastly more valuable to society, than a bus driver. This should be reflected in the social value attributed to the deceased and added to the base value of a life, to determine the true value of the social harm done to society, by their murder. Note that, for almost all cases, these variations in the social value of the deceased will not affect the sentencing outcome, in practical terms, since the value of even the least contributory life, is a very significant sum and would, most probably, require a lifetime of reparations from the convict. In cases in which a criminal is wealthy, much of their wealth could be confiscated to pay towards the social cost of the murder incurred. They should also have to serve out a considerable term of reparatory labour perhaps even for the remaining duration of their life. An alternative possible formulation for the value to society of a human life, would be to see the particular value of a person to society as a multiplier, of the base value. Thus, it might be decided, for instance, that a neurosurgeon had 50 times the value to society of a plumber. In which case the baseline value of a human life would be multiplied by 50 to determine the social harm caused by their murder. All professions could be assigned a multiplier, though it would still be necessary to derive a unique multiplier in special cases such as people of unique talents or contributions to society. This relationship could be written as V = LP In which V is the value of a human life to society, L is the conventionally accepted value of a life and P is the multiplier for the particular person who has died, of their value in relation to a typical person. This second way of determining the value to society of a human life should give the same answer, as the first suggested means of determining value. However, it may be determined that one way or the other is easier to implement, in practice. I offer both as viable alternative means to make a decision on the value to society of a particular person. How is a criminal's debt to society to be repaid? Punishment is only effective, in my view, if it teaches a lesson that alters the understanding of the criminal about the nature of their crime. They must come to understand what the harm means, that they have committed. They must come to understand the value of what they have destroyed. The best way to do this, I believe, is to require the criminal to make amends directly for the harm they have caused, to the value of the harm they have caused. This means that, for instance, the arsonist of the House of Reeves, a furniture store, could be required to learn how to make furniture, then to spend however many years are necessary, making furniture, until the value of the work done, equals the value of the harm done. This would truly be repaying one's debt to society . Alternatively, he could be required to learn how to build, and spend his years building commercial property. Note that during the period of repayment to society, the convicted criminal would be notionally paid a commercial rate for his work, but would receive none of these funds: all monies would go towards his debt repayment account. It would be beneficial, from the point of view of effective punishment, if he lived in prison like accommodation during this period which his work could also pay for. In the example of the slashed girl, the convict should work in some medical role, in a hospital context, as the limits of his talents dictate. Again, this would instil a better understanding of the nature of the harm done. Note that in the case of the House of Reeves, the value of the harm done is likely to be more than an entire lifetime's work, for a furniture maker or a construction labourer. In such a case, the entire working lifetime of the convicted criminal would be expended in said labour, as a gesture towards the debt to society incurred. Additionally, the assets of the criminal could be confiscated as partial payment towards the outstanding debt. Note, however, that for a rich criminal, there should be a limit on the extent to which existing assets can be used to pay a debt: all convicted criminals, with a debt to society, should have to spend a certain time working, for no real wage, towards repaying their debts. This practice is summarized in The Principle of Fair Restitution for Harm to Society: A convicted criminal, should be required to repay their full, assessed debt to society, through confiscated assets, and satisfactory unpaid work, in an area related to the harm caused, equal in quantum, to the debt incurred. The criminal shall not be free to live their own life, until such time as the debt has been fully paid up. Note that the work produced by the convict, must be of an acceptable standard. Any work that is below standard, will NOT be accepted as payment towards the outstanding debt. Thus, if the convict, is deliberately slack or lazy on the job, and does not do a good job, they will never be paid (notionally) and will never repay their debt. The rewards of slackness, under this punishment model, are a lifetime of involuntary work. Further note, however, that the convict has it in their power to shorten their sentence, through overtime and extra hard work. If they work longer hours than are required, in a day, they shall be paid (notionally) more and thus will pay down their debt quicker. This system is admirable in that it will reward hard work and instil a work ethic in convicts. Consequences of instituting this Principle as the means to punishment Were this Principle to be the foundation for a new way to punish crime, it would lead to criminals being led to an understanding of their crimes, in a very profound way. By being forced to work to directly repair the kind of damage caused, they will come to understand the value of what they destroyed. Furthermore, the magnitude of their crimes will become apparent to them as it is measured in the years of free labour, they must perform to pay back their debts. In the case of those whose debts are so great that it would require more than a lifetime to repay them, they should be held up as an example to future potential criminals, of what will happen to them, if they cause similar harm: they will lose their lives to hard, incessant, lifelong, involuntary work. One very obvious consequence of this Principle will be that future criminals will become wary of causing any real harm to society. They will be aware that the more harm they cause, the longer their sentences will become. Thus, many crimes will be discouraged. Who, for instance, would set light to a furniture store, knowing that to do so would mean a whole lifetime making furniture for no salary at all? Who would slash the face of a girl, if it meant a lifetime working in a hospital, for instance, in a lowly position, for no salary at all? Who would smash a window and loot a shop, if it meant a couple of years working as a glazier, for free? Very few future criminals would consider these crimes worth it, were the penalty to be that they should directly and fully repay their resultant debts. To whom should the debt be paid? The money that would have been paid to the convicted criminal, for the work done, could be, most fairly, paid to the victim to directly compensate them, over time, for the harm done. Alternatively, the criminal could work directly for the victim, for free, until such time as the debt is paid. In either scenario, the State would not lose financially, since it would have the value of the work of the criminal and any product of that work such as furniture, in compensation. For cases involving insurance payouts, the monies that would have been paid to the convict, for the work done, could, instead, be paid to the insurance company. Either that, or the convict could, in effect, be working, for free, directly for the insurance company. An appropriate financial arrangement could be constructed for each case. Where should this repayment work be done? The conditions of work, of the convict, will depend on the character of the convict. If it is decided that they cannot be trusted in the outside world, to do what they are supposed to do, to make restitution, then they should work within the context of a prison. Thus, the House of Reeves arsonist, could make furniture for the rest of his life, within a prison context. The person who slashed our hypothetical girl, could work in the prison hospital, until his debt was repaid. A person who burnt down a restaurant, could work in the prison canteen for the number of years required to repay that debt, and so on. The important elements to consider are that the true full value of the debt should be repaid, at a rate equal to the true market value of that type of work. Thus, it is that the punishment exactly fits the crime. Should the convict's character (such as it is) prove suitable to working in the community, to repay the debt, then this could be permitted. However, this would incur supervisory costs. This provokes a dilemma because if the cost of the supervisor's salary is factored in, then the convict would, most probably, never be able to work off their debt. Keeping them in prison would have the lowest incremental cost. Alternatively, to keep supervisory costs low, the chain gang model, of enforced work, could be used, if there are a sufficient number of criminals with similar crimes and punishments. Underaged criminals and appropriate punishment This model of punishment is also suitable for punishing criminals under the age of 18. Presently, in the UK, underage criminals are treated very lightly. This, of course, only encourages them to consider committing future crimes. Repeated encounters with the legal system, resulting in a repeated failure to punish them, can only result in entraining criminal behaviour in the youngsters. However, the Principle of Fair Restitution for Harm to Society, allows for a way out of this cycle. No child, however, young, should be let off their debt to society. A child may be trained in an appropriate task such as polishing furniture, or cleaning windows (for those who broke them), or assisting in a hospital (for those who caused physical harm) etc. Then the child could be required to work, part time, in such a setting for however long is required to repay their debt. This could take years, in some instances. However, there should be no reduction, in the amount of time they should have to repay, simply because they were teenagers at the time of the offence. The lesson of the full value of the harm they caused, must be learnt. There is no need to interrupt the child's education, during this period. The involuntary work could take place at the weekend, or other outside school hours. The question of fairness of punishment Some might complain that a lifetime's work, making furniture, for instance, is an unfair punishment for torching a furniture store. They might argue that it is too long a sentence for one crime. I disagree. Any punishment, which involves less reparation than the damage done is unfair to society. Being lenient to a criminal and giving them a sentence less than the full value of the harm done, is being unfair to us all. The only fair sentence, is a sentence of reparation equal to the harm done. The possibility of redemption One problem that a lifetime's involuntary work, in punishment for a particularly harmful crime, brings to mind, is the impossibility of any second chance in this lifetime. The criminal so punished, does not have any life beyond their punishment to look forward to, in which to live a life of better behaviour. To counter this, the possibility could be introduced of suspending the remainder of any sentence, after a significant number of years satisfactory involuntary work has been completed. This should ONLY be done, if the perpetrator appears to have understood their crime and reformed their outlook and attitudes. Indeed, all criminals punished according to the Principle of Fair Restitution for Harm to Society, should be offered this possibility, if their work is of consistently high enough standard, if their behaviour is good and if their attitudes to society have been reformed. Note that if the criminal is released on a suspended sentence and goes on to commit ANY crime AT ALL, no matter how trivial, their full sentence will be reinstated and they shall have to serve out the remainder of their sentence, without the possibility of ever being released from it again. There should be no third chances. This will reinforce the effectiveness of this method in reforming behaviour and eliminating criminality. The issue of benefits Benefits, as they exist in the UK, are unearned monies paid to those without work, or who are disabled etc. With regards to punishment, benefits should not be used as a means to repay the debt to society, for the benefits accrue to the recipient without effort. It would be against the Principle of Fair Restitution for Harm to Society that an iota of benefits should be used to repay the debt to society. All repayment of social debt should come from the efforts of the perpetrator. Furthermore, no-one who commits a crime, should be permitted to receive benefits, of any kind, until the debt to society is settled. The reason for this is simple: the perpetrator has incurred a debt to society he owes society money or money equivalent. Therefore, he should not be able to receive benefits, whilst he (or she) still owes society, since any such receipt would act so as to increase the debt to society. That must not be. It may also be considered that committing the crime for which the perpetrator is being punished, could permanently bar them from ever receiving any benefit, of any kind, ever again. This is so that society would not be in a position of supporting someone who is harmful to that society. This is also a secondary form of punishment. However, this step need not always be implemented, but should remain an option for punishment. Whatever the case, no benefits should be received by the perpetrator, until they have fully repaid their debt to society. Improvement of the criminal It is clear that it is not possible to fulfil the requirements of punishment, under this model, and not acquire new, useful skills. Thus, it is that this model not only punishes criminals, but improves them, by making them employable. By the end of their sentence, they would have acquired useful work skills, habits and experience and may, thereafter, consider real paid work, subsequently, in an area that uses those acquired skills. The future of crime and punishment Were this Principle instituted, every crime would be punished exactly according to its true harm. Thus, justice, in this model, becomes ideal. The cost exacted from the criminal, exactly equals the harm caused society and its individuals and organizations. In this model, there can be no appeal for a sentence. Each sentence is absolutely perfect in its justice for no more is required of the criminal than the harm he or she caused. It is only necessary to show intention. It would, for instance, be unjust to punish someone with a lifetime of involuntary work, were it an accident that they had set fire to a particular furniture store. Should this model of punishment be instituted, the cost of the penal system, for society, would be reduced, since each criminal would be putting back into society, the full value of the harm they caused. The cost of their incarceration could also be figured into the equation, unless said cost would cause sentences to be indefinite, owing to ever increasing debts. Let The Principle of Fair Restitution for Harm to Society be applied in legal systems throughout the world. Were this so, every punishment would exactly fit the magnitude of each crime. Thus, it is that punishments would finally be truly just. References [1] Daily Mail reporter. (2011). Man, 21, arrested on suspicion of arson attack on historic family-run furniture shop that survived the blitz. Daily Mail online. Accessed: August 17th 2011. http://www.dailymail.co.uk/news/article-2023975/London-riots-2011-Man-21-arrested-Croydons-House-Reeves-arson-attack.html [2] Appelbaum, B. (2011) As U.S agencies put more value on a life, businesses fret. New York Times online. Accessed August 19th 2011. http://www.nytimes.com/2011/02/17/business/economy/17regulation.html?pagewanted=all","id":"1-797be351eb84d02a3880aa0590ea640f4e0b4b90"}}]}
fergiemcdowall commented 8 years ago

The document doesn't seem to have a field called div- does it have one when you index it? What does it contain?

joshterrill commented 8 years ago

I see what you mean. We're confusing the term field. I thought it meant define what html field you want the teaser to turn up in, and you mean define what field in the object store that you want to pull from. My bad I was confused. On Feb 12, 2016 1:46 AM, "Fergus McDowall" notifications@github.com wrote:

The document doesn't seem to have a field called div- does it have one when you index it? What does it contain?

— Reply to this email directly or view it on GitHub https://github.com/fergiemcdowall/search-index/issues/237#issuecomment-183255268 .

fergiemcdowall commented 8 years ago

OK :)