Suppose I have a multivalue field "category" and two documents:
doc1: category=[A,B]
doc2: category=[B,A]
A document may belong to more than category, and the categories are listed in the order of their relevance. So, when searching for "category:A" I want doc1 sorted before doc2. This means I have to sort by a function that takes term's order of appearance into account. Is this possible?
Related
I have parent and child documents in my Elasticsearch index related through a join: https://www.elastic.co/guide/en/elasticsearch/reference/6.3/parent-join.html.
I would like to be able to submit a query which matches on child documents and returns the siblings of the matching child documents.
My situation is i have students divided into groups, each student in my index is a separate child document and all students in the same group have the same parentId. The parent document contains no meaningful fields other than a groupId. My query is I want to get the list of all of the students who are in the group with student X with a single query.
For example my query would look similar to:
{
"query": {
"match": {
"studentName": "Bob"
}
}
}
And my response would list all the students who are in the same group as "Bob"
NOTE: I realize this problem could easily be solved by nesting the children who are in a group together into a single document, however, for my use case i cannot do this as i need to support a second query which is to be able to search for a student by name and return the results in sorted order based on relevancy. If i nest the student documents inside the same document, to my understanding, i can no longer achieve this second query.
Does anyone know if the search for siblings query is possible?
Or more broadly does anyone know of any ES construct that would allow me to achieve both searching for students in the group with student X with a single query AND searching for student by name in a single query?
Looks like that can be achieved by nesting has_child inside has_parent. Still, you cant sort by child doc's properties + this query is going to be slow depending on your index size.
The main difference between must and filter query is the _score calculation.
Can anyone tell me what is the purpose of the score shown in the query result?
How can we use the score?
The score gives you the relevance of a given document to the executed query. The higher the score, the more relevant is the document. For example, consider the following documents:
# Doc 1
{
"title": "What is the purpose of score for a user in elastic search query result?"
}
# Doc 2
{
"title": "What is the purpose of score in life?"
}
Then, if you query for a title that includes the words purpose score elastic (something you would do, for example, in the stackoverflow search bar), the first document will get a higher score and will appear on top of the list of results.
On the other hand, filters tell you whether a document matches or not the query. It is either a yes or no, therefore, it is not necessary to calculate the score.
For further details, have a read at the always very good Elastic documentation.
I want to create an index in elasticsearch that has a field of weighted keywords list, so when I search by term in this keywords - it will give better scores to those documents that has this key with higher weight?
For instance:
Doc1
"id" : "111"
"keywords" : "house"(20), "dog"(2)
Doc2
"id" : "222"
"keywords" : "house"(3), "dog"(40)
I want when searching "dog" to get doc2 with higher score.
How would you build the mapping and the query?
Note that it's different than searching with regular boost, as the boost per each term is different per document.
What about Elasticsearch payloads? See DrTech's answer with the delimited payload token filter to a separate unrelated question which might help you out. But, what you are describing seems to very much lend itself to the use of payloads and using script scoring to access these payloads and influence the scoring. Take note of the performance cost he mentions.
I'm using elastic search and would like to sort my collection based on a combination of relevance and id.
When I search by 'id', if I were to search my name, "john kealy", I would have tons of John's come up before me. If I search by relevance, I lose all ability to search my john kealy's by id. From my understanding, a combination sort would sort by a second parameter in the event of a tie, but I think that there's such a difference between a result "john kealy" and "john blabla", that i'd like the john kealy's to come first, sorted by id, then everything else sorted by id.
is this possible?
First recommendation for this: custom scoring lets you weight/adjust/emphasize/deemphasize/whatever to reflect the ranking you want/need.
EDIT: Crikey, I'm getting old. What you want is function score, not custom score. Sorry about that.
https://www.elastic.co/guide/en/elasticsearch/reference/0.90/query-dsl-function-score-query.html
im trying to use elasticsearch to search through products. If product is a car for instance, it will have some field like "color", "brand", "model", "km", ...
If it is clothes, it will only have "color", "size", ...
I would like to index all this info in elastic to be able then to search cars with km between aaa km and bbb km, and / or xxxx model, same for clothes or any other products.
how can I create such field(s) in elasticsearch ? I want all products to be in same index, so user can search through all products, but also if user search a type a product, then he should be able to specify some more details according to this kind of product.
I was thinking about array field, but does that mean that all products will have all fields corresponding to all type of products even if some fields are not relevant with some products (ie clothes will have km field ??) ? Or is it possible on indexing to put just info needed corresponding to each product ?
thanks
You could use types. Create a type called car with fields color, brand, model, k etc. and then a type called cloth with fields color, size, etc.
A single index can have many types. The following two links might help you in this:
Creating indices
Creating types and mapping to the index
You could easily search across types so that you could issue a search like this to return all documents form all types within that index:
curl -XGET http://localhost:9200/_search?pretty=true -d '{"query":{"matchAll":{}}}'
Additional information - Searching across types
Having an array field is not a good idea since you would not be utilizing the ability of elasticsearch to index semi structured documents.
All the best.