elasticsearch find posts by comments - elasticsearch

I'm trying to write a query that finds articles based on their comments.
So if a user is trying to find "chocolates"
{
type: "article",
id: "myArticle1",
title: "something about brown food"
}
{
body: "I love chocolates!",
type:"comment",
commentOf: "myArticle1"
}
In this example I have both documents in the same index and I'm trying to get the "myArticle1" document via the comment matching chocolates in body. How do I do this? Is it with the top_children query?

You can use the parent-child in ES to achieve this:
Define the parent (article) and child (comment)
Index data. You should know how to index child data as it will difference from normal (need to specify parent in the index request)
Use has_child query to query for article that matched some
fields in comment
I wrote a full working sample script for it: https://gist.github.com/dqduc/efa66047358dac66461b
You can run it to test and send me your feedback. I guess you're new to ES and parent-child relationship in ES.

Related

Joining two indexes in Elastic Search like a table join

I am relatively new to this elastic search. So, I have an index called post which contain documents like this:
{
"id": 1,
"link": "https:www.instagram.com/p/XXXXX/",
"profile_id": 11,
"like_count": 100,
"comment_count": 12
}
I have another index called profile which contain documents like this:
{
"id": 11,
"username": "superman",
"name": "Superman",
"followers": 12312
}
So, as you guys can see, I have all profiles data under the index called profile and all posts data under the index called post. The "profile_id" present in the post document is linked with the "id" present in the profile document.
Is there any way, when I am querying the post index and filtering out the post documents the profile data will also appear along with the post document based on the "profile_id" present in the post document? Or somehow fetch the both data doing a multi-index search?
Thank you guys in advance, any help will be appreciated.
For the sake of performance, Elasticsearch encourages you to denormalize your data and model your documents accordingly to the responses you wish to get from your queries. However, in your case, I would suggest defining the relation post-profile by using a Join datatype (link to Elastic documentation) and using the parent-join queries to run your searches (link to Elastic documentation).

Elasticsearch what is the best query for website search

I am elasticsearch newbie.
I am trying to use elasticsearch to search some titles let's say books names.
From my client I am sending the user input string every 0.5 second to the server to give suggestions.
So it could be partial words and maybe typos.
What would be the best query/way to deal with that.
Any point on the right direction will be appreciated.
Thanks
// fuzzy query example but it is not answering for multiple words
return await client.search({
index: indexName,
body: {
query: {
fuzzy: {
title: userInputSentence
}
},
},
});
You can achieve auto-complete by adding ngram/prefix tokenizers - this will make the partial text entered to match the docs with the full length text.
Few references:
How to suggest (autocomplete) next word in elastic search?
https://hackernoon.com/elasticsearch-building-autocomplete-functionality-494fcf81a7cf
update:----------
Another option is to use Elasticsearch completion suggester and Index-Time Search-as-You-Type

how elastic search find document content by doc id

There are many articles talking about inverted index and posting list in elastic search. But I did not find any article which explain that how elastic search find document content by doc id.
Could anyone explain this to me?
thx.
Ragav is correct. However, I do have a bit to add that may help you work with document Ids.
When you index documents that don't have an ID, and ID is generated for you by ElasticSearch. That field name is "_id".
If you know the Id value of the document you wish to find, you can simply perform the query like this:
GET my_index/_search
{
"query": {
"terms": {
"_id": [ "1", "2" ]
}
}
}
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-id-field.html
The above query would return documents that have have _id equal to 1 OR 2.
As Ragav said in his answer, if you created documents in the way described with id 1 or 2, you would return them with that sample query I pulled from the ElasticSearch documentation.
Hope this helps.
Elasticsearch is built on top of Lucene.
When you index a new document onto Elasticsearch, it indexes _index, _type and _id as a part of the document along with the actual content(_source).
So, when you try to get a document using the get API _index/_type/_id, it is basically converted into a query which searches for doc matching the _index, _type and the _id.
This is how Elasticsearch is able to return you the document.

Apollo GraphQl Query get data from two documents

i have a query and i want to go through two different documents, is it possible to do it ?
Document A { _id, senders_ID[sender_id]}
Document B { sender_id, firstname}
// i want to loop through document A and get each sender_id and go to document B to get the firstname.
Is it what you are looking for ?

Group by field in found document

The best way to explain what I want to accomplish is by example.
Let us say that I have an object with fields name and color and transaction_id. I want to search for documents where name and color match the specified value and that I can accomplish easily with boolean queries.
But, I do not want only documents which were found with search query. I also want transaction to which those documents belong, and that is specified with transaction_id. For example, if a document has been found with transaction_idequal to 123, I want my query to return all documents with transaction_idequal to 123.
Of course, I can do that with two queries, first one to fetch all documents that match criteria, and the second one that will return all documents that have one of transaction_idvalues found in first query.
But is there any way to do it in a single query?
You can use parent-child relation ship between transaction and your object. Or nest the denormalize your data to include the objects in the transactions. Otherwise you'll have to do an application side join, meaning 2 queries.
Try an index mapping similar to the following, and include a parent_id in the objects.
{
"mappings": {
"transaction": {},
"object": {
"_parent": {
"type": "transaction"
}
}
}
}
Further reading:
https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child-mapping.html

Resources