How to use minimum_should_match to search in multiple fields? - search

I'm trying to filter my elasticsearch's result, it must return the results which are 80% compatible with the search text.
When I do this for just one key, the minimum_should_match works perfect:
{
"size":30,
"from":930,
"query":{
"filtered":{
"query":{
"query_string":{
"default_field":"campo1",
"query":"portugues",
"minimum_should_match":"80%"
}
}
}
}
}
When I search using more than one key, the minimum_should_match doesn't work right, return the results with 70% compatibility:
{
"size":30,
"from":123420,
"query":{
"filtered":{
"query":{
"query_string":{
"default_operator":"or",
"query":"portugues",
"fields":[
"campo1",
"campo2^5",
"campo3"
],
"minimum_should_match":"80%"
}
}
}
}
}
As far as I can think of, I need to set minimum_should_match by key, but I don't know how to do the same.
If someone can help me out in doing so, will be great.

I needed to use bool and multi_match, this is right way:
{
"size":"30",
"from":0,
"query":{
"filtered":{
"query":{
"bool":{
"should":[
{
"multi_match":{
"query":"portugues",
"type":"cross_fields",
"fields":[
"campo1^3",
"campo2^5",
"campo3^3"
],
"minimum_should_match":"80%"
}
}
]
}
}
}
}
}

Related

How to count a field if value is not null in elasticsearch

I have indexed some documents in ElasticSearch.
Short view looks like this:
{
"tenant_id":"abcd1234"
"provider_id":"3456"
.
.
.
"doctor_summary": ["line1", "line2", "line3"] OR it could be null.
}
I want to calculate this list as 1 if not null,
query_template = {
"bool":{
"must":[
{"term":{"tenant_id.keyword":tenant_id}},
{"term":{"provider_id.keyword":provider_id}},
{"range":{"visit_date":{"gte":from_date,"lte":to_date}}},
],
"filter":[]
}
}
aggs_query = {
"doctor_summary_count":{
"value_count":{"field":"doctor_summary.keyword"}
}
}
res = CLIENT.search(index = config['elasticsearch']['abcd'],
query = query_template,
size = 10000,
aggs = aggs_query)
After calling this aggregation query, it gives result as ( size of the list * total doctor_summary field).
For example: the result of above query on above document should be 1. (As it is not null).
But it gives as 3(because list contains 3 lines.)
You can use exist query in aggregation with filter aggregation.
Query:
{
"aggs": {
"count": {
"filter": {
"exists": {
"field": "doctor_summary.keyword"
}
}
}
}
}
Response:
"aggregations" : {
"count" : {
"doc_count" : 1
}
}

Bixby: page might be empty warnings

I'm really stuck and don't know what to do.
I've never worked in Bixby Studio and got that warning "page might be empty".
I've seen two similar posts about this problem, but they didn't help me at all.
item-selection-question {
first warning--> if (isFirstNavPage(page) && isLastNavPage(page)) {
template ("")
second warning--> } else-if (!isLastNavPage(page)) {
template-macro (HANDS_FREE_OPTION_ITEM_SELECTION_MORE_PAGES)
} else {
template-macro (HANDS_FREE_OPTION_ITEM_SELECTION_LAST_PAGES)
}
}
overflow-statement {
template-macro (HANDS_FREE_OPTION_OVERFLOW_STATEMENT)
}
overflow-question {
template-macro (HANDS_FREE_OPTION_OVERFLOW_QUESTION)
}
page-marker {
third warning--> if (!isFirstNavPage(page) && isLastNavPage(page)) {
template-macro (HANDS_FREE_OPTION_LAST_OPTION) {
param (page) {
expression (page)
}
}
}
}
I had a similar warning for,
content {
template ("Here's #{value(audioInfo.audioItem[audioInfo.startAudioItemIndex].artist)}.") {
speech ("Here's #{value(audioInfo.audioItem[audioInfo.startAudioItemIndex].title)}[ from #{value(audioInfo.audioItem[audioInfo.startAudioItemIndex].artist)}].")
}
}
WARN audioInfo.startAudioItemIndex might be empty
I changed the above snippet to,
content {
if(exists(audioInfo.startAudioItemIndex)){
template ("Here's #{value(audioInfo.audioItem[audioInfo.startAudioItemIndex].artist)}.") {
speech ("Here's #{value(audioInfo.audioItem[audioInfo.startAudioItemIndex].title)}[ from #{value(audioInfo.audioItem[audioInfo.startAudioItemIndex].artist)}].")
}
}
}
And it no longer shows the warning.
Try and see if it helps.

How do I get rid of WARN Pet.species can be multi-cardinal?

In my result-view, in this if clause, I get the warning "WARN pet.species can be multi-cardinal". What does this mean and how do I get rid of it?
result-view {
match: Pet (pet)
render{
layout{
section{
content{
hbox{
content{
vbox{
content{
text{
value ("#{upper(pet.name)}")
style (Title_S)
}
}
}
if (pet.species == 'dog'){
vbox{
content{
single-line{
image{
shape (Circle)
style (Title_S)
url ("/icons/dog-icon.png")
}
}
}
}
} else-if (pet.species == 'cat') {
vbox{
content{
single-line{
image{
shape (Circle)
style (Title_S)
url ("/icons/cat-icon.png")
}
}
}
}
}
}
}
}
}
}
}
}
Is the syntax wrong or something? The code still works but it'd be nice to get rid of the warnings.
In concept model, set it as Max(One).
Or put the render block in a if statement using size-of.

Creating selection-of from enum?

I am looking to create an input-view where the user can pick a list of enum symbols.
For example, I have a finite number of Genres, so I placed them into an enum, these are things such as "Fantasy", and "Sci-fi"
I have tried the code below but nothing displays; I check the documentation but all of the selection-of examples are based off concepts that already have data.
Is there a way to create a selection-of input-view where the user can select one or more items, then outputs this as a concept?
input-view {
match {
GenreNames(gnames)
}
message (Select Genre(s))
render {
selection-of (gnames) {
where-each (one) {
single-line {
text {
value (one)
style (Detail_M_Soft)
}
}
}
}
}
}
Try this:
input-view {
match {
GenreNames(gnames)
}
message (Select Genre(s))
render {
selection-of (gnames) {
where-each (gnames) {
single-line {
text {
value {
template ("#{value(gnames)}")
}
style (Detail_M_Soft)
}
}
}
}
}
}

Elastic Search Parent-Child Data Search Java API

I am new to ELastic Search.
Data in Elastic search is in Parent-Child Model.I want to perform search in this data using java api.
parent type contains author details and child type contains book details like book name,book publisher, book category.
While performing a search on child details,I need to get the parent details also and vice versa. Sometimes search conditions will be on parent type as well as child. eg search for books written by author1 and type Fiction.
How can i implement this in java? I have referred the elastic search documentation but not able to get a solution
Please help
First set up your index with the parent/child mapping. In the mapping below I have also added a untokenized field for categories so you can execute filter queries on that field. (For creating the index and documents I'm using the JSON API not the Java API as that was not part of the question.)
POST /test
{
"mappings": {
"book": {
"_parent": {
"type": "author"
},
"properties":{
"category":{
"type":"string",
"fields":{
"raw":{
"type":"string",
"index": "not_analyzed"
}
}
}
}
}
}
}
Create some author documents:
POST /test/author/1
{
"name": "jon doe"
}
POST /test/author/2
{
"name": "jane smith"
}
Create some book documents, specifying the relationship between book and author in the request.
POST /test/book/12?parent=1
{
"name": "fictional book",
"category": "Fiction",
"publisher": "publisher1"
}
POST /test/book/16?parent=2
{
"name": "book of history",
"category": "historical",
"publisher": "publisher2"
}
POST /test/book/20?parent=2
{
"name": "second fictional book",
"category": "Fiction",
"publisher": "publisher2"
}
The Java class below executes 3 queries:
Search on all books that have the term 'book' in the title and
return the authors.
Search on all authors that have the terms 'jon doe' in the name and
return the books.
Search for books written by 'jane smith' and that are of type Fiction.
You can run the class from the command line, or import into Eclipse and right click on the class and select 'Run As > Java Application'. (You'll need to have the Elasticsearch library in the classpath.)
import java.util.concurrent.ExecutionException;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.HasChildQueryBuilder;
import org.elasticsearch.index.query.HasParentQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermFilterBuilder;
public class ParentChildQueryExample {
public static void main(String args[]) throws InterruptedException, ExecutionException {
//Set the Transport client which is used to communicate with your ES cluster. It is also possible to set this up using the Client Node.
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "elasticsearch").build();
Client client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(
"localhost",
9300));
//create the searchRequestBuilder object.
SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client).setIndices("test");
//Query 1. Search on all books that have the term 'book' in the title and return the 'authors'.
HasChildQueryBuilder bookNameQuery = QueryBuilders.hasChildQuery("book", QueryBuilders.matchQuery("name", "book"));
System.out.println("Exectuing Query 1");
SearchResponse searchResponse1 = searchRequestBuilder.setQuery(bookNameQuery).execute().actionGet();
System.out.println("There were " + searchResponse1.getHits().getTotalHits() + " results found for Query 1.");
System.out.println(searchResponse1.toString());
System.out.println();
//Query 2. Search on all authors that have the terms 'jon doe' in the name and return the 'books'.
HasParentQueryBuilder authorNameQuery = QueryBuilders.hasParentQuery("author", QueryBuilders.matchQuery("name", "jon doe"));
System.out.println("Exectuing Query 2");
SearchResponse searchResponse2 = searchRequestBuilder.setQuery(authorNameQuery).execute().actionGet();
System.out.println("There were " + searchResponse2.getHits().getTotalHits() + " results found for Query 2.");
System.out.println(searchResponse2.toString());
System.out.println();
//Query 3. Search for books written by 'jane smith' and type Fiction.
TermFilterBuilder termFilter = FilterBuilders.termFilter("category.raw", "Fiction");
HasParentQueryBuilder authorNameQuery2 = QueryBuilders.hasParentQuery("author", QueryBuilders.matchQuery("name", "jane smith"));
SearchResponse searchResponse3 = searchRequestBuilder.setQuery(QueryBuilders.filteredQuery(authorNameQuery2, termFilter)).execute().actionGet();
System.out.println("There were " + searchResponse3.getHits().getTotalHits() + " results found for Query 3.");
System.out.println(searchResponse3.toString());
System.out.println();
}
}
You can use Parent-Child documents for this.
Let's create an index bookstore with simple mappings for author documents and book documents. You can add more fields as per your requirements. See this for more information about indexing parent/child documents.
PUT bookstore
{
"mappings": {
"author": {
"properties": {
"authorname": {
"type": "string"
}
}
},
"book": {
"_parent": {
"type": "author"
},
"properties": {
"bookname": {
"type": "string"
}
}
}
}
}
Now let's add two authors:
PUT bookstore/author/1
{
"authorname": "author1"
}
PUT bookstore/author/2
{
"authorname": "author2"
}
Now let's add two books of author author1:
PUT bookstore/book/11?parent=1
{
"bookname": "book11"
}
PUT bookstore/book/12?parent=1
{
"bookname": "book12"
}
Now let's add two books of author author2:
PUT bookstore/book/21?parent=2
{
"bookname": "book21"
}
PUT bookstore/book/22?parent=2
{
"bookname": "book22"
}
We're done indexing documents. Now let's start searching.
Search all books authored by author author1 (Read more about this here)
POST bookstore/book/_search
{
"query": {
"has_parent": {
"type": "author",
"query": {
"term": {
"authorname": "author1"
}
}
}
}
}
Search the author of book book11 (Read more about this here)
POST bookstore/author/_search
{
"query": {
"has_child": {
"type": "book",
"query": {
"term": {
"bookname": "book11"
}
}
}
}
}
Search for books named book12 and authored by author1. You need to use bool queries to achieve this. (There can be a better example for this scenario with more fields in the documents)
POST bookstore/book/_search
{
"query": {
"bool": {
"must": [
{
"has_parent": {
"type": "author",
"query": {
"term": {
"authorname": "author1"
}
}
}
},
{
"term": {
"bookname": {
"value": "book12"
}
}
}
]
}
}
}
I've done something similar with "spring-data-elasticsearch" library. There are heaps of samples available on their test suite.
Follow this link on git : https://github.com/spring-projects/spring-data-elasticsearch/blob/master/src/test/java/org/springframework/data/elasticsearch/NestedObjectTests.java
List<Car> cars = new ArrayList<Car>();
Car saturn = new Car();
saturn.setName("Saturn");
saturn.setModel("SL");
Car subaru = new Car();
subaru.setName("Subaru");
subaru.setModel("Imprezza");
Car ford = new Car();
ford.setName("Ford");
ford.setModel("Focus");
cars.add(saturn);
cars.add(subaru);
cars.add(ford);
Person foo = new Person();
foo.setName("Foo");
foo.setId("1");
foo.setCar(cars);
Car car = new Car();
car.setName("Saturn");
car.setModel("Imprezza");
Person bar = new Person();
bar.setId("2");
bar.setName("Bar");
bar.setCar(Arrays.asList(car));
List<IndexQuery> indexQueries = new ArrayList<IndexQuery>();
IndexQuery indexQuery1 = new IndexQuery();
indexQuery1.setId(foo.getId());
indexQuery1.setObject(foo);
IndexQuery indexQuery2 = new IndexQuery();
indexQuery2.setId(bar.getId());
indexQuery2.setObject(bar);
indexQueries.add(indexQuery1);
indexQueries.add(indexQuery2);
elasticsearchTemplate.putMapping(Person.class);
elasticsearchTemplate.bulkIndex(indexQueries);
elasticsearchTemplate.refresh(Person.class, true);
SearchQuery searchQuery = new NativeSearchQueryBuilder().build();
List<Person> persons = elasticsearchTemplate.queryForList(searchQuery, Person.class);

Resources