Need a customized index in the elasticsearch - elasticsearch

My query is, if i search for a keyword "apparel" my current result is
{
"_index":"myindex",
"_type":"pages",
"_id":"www.abc.com",
"_score":1.726047,
"_source":{
"organization_website":"www.abc.com",
"organization_values":[
{
"unpsc":15,
"industry":"apparel",
"codevalue":0.15
},
{
"unpsc":20,
"industry":"textile",
"codevalue":0.2
}
]
}
}
I can override the _score in Elasticsearch using function score, but i need to add the add the score (which i have created) as the default score in the index. For instance if i search for the keyword "apparel", the default score will 1 , but i want to override the score of the apparel (which i have created) i.e unpsc value to the score.
Is this possible to create a index in the elasticsearch to refer an another node as a default score or i have go with the function score/ boost.....?

I don't think it's possible to assign a default score based on some value.
You'll have to use function score as suggested by yourself.

Related

Querying ElasticSearch document based on particular value without knowing field name

I need to query the entire index based on particular text value. I don't have field name to query for. Is it possible to search the documents based on particular text?
You can use query string.
You can specify multiple fields. If no field is specified it will search in entire document
{
"query": {
"query_string" : {
"query" : "text"
}
}
}

Multiple filter by array of object in Elastic 6.*

Need help with building query through the array in ElasticSearch 6. I have documents that represent some property units with a number of attributes:
{
"Unit":{
"Attributes":{
"Attribute":[
{
"Name":"Elevator",
"Text":"No"
},
{
"Name":"Pet Friendly",
"Text":"Yes"
}
...
]
}
}
}
How can I filter my documents to find all pet friendly units or all units without elevator?
P.S. I am using NEST.
Map Attribute as a nested type, probably with Text mapped as keyword for term level matching. To query, use a bool query with filter clauses, where the clauses will be nested queries.

ElasticSearch Function Score Query not defaulting to 1 if missing field

I am trying to create a function score in ElasticSearch that (after other filters) gives a higher score to those customers who bought a product recently. For this I have a field "lastPurchaseOn".
The query also needs to return customers who did not buy any product, so I cannot filter on the lastPurchaseOn field being present.
"functions": [
{
"exp": {
"lastPurchaseOn": {
"scale": "3d"
}
}
},
...
]
The problem is that when the field "lastPurchaseOn" is missing, the function returns score 1, when I would really want it to return 0.
Is there a way to make the function return 0 for missing values?
Thanks
The official documentation states it pretty clearly:
If the numeric field is missing in the document, the function will return 1.
The main reason is that there's no way for the exponential function that is being used to return 0. You can try to use a linearfunction instead of exp.

Change _type of a document in elasticsearch

I have two TYPES in my elasticsearch index. Both have same mapping. I am using one for active documents, while the other for archived ones.
Now, i want to archive a document i.e. change its _type from active to archived. Both are in same index, so i cannot reindex them as well.
Is there a way to do this in Elasticsearch 5.0 ?
Changing the type is tricky. You would have to remove and then index the document with the new type.
Why not have a field in your document indicating "activeness". Then you can use a bool query to filter by what you want:
{"query": {
"bool": {
"filter": [{"term": {"status", "active"}}],
"query": { /* your query object here */ }
}
}
}
Agree with having a field which indicates the activeness of the document.
(Or)
Use two different indices for "active" and "inactive" types.
Use aliases which map to these indices.
Aliases will give you flexibility to change your indices without downtimes.

how can I sort ElasticSearch result properly

For example I have the following two documents with fields Id and Name(the name field is analyzed):
1,jack-in-box
2,box
When my query was "box", I got the both documents, but actually I only wanna the document 2, or getting document 2 above of document 1.
How can I query this please.
I know that the doc1 was tokenized to jack,in and box, so when I search box I would get the doc1. My current solution is creating another field called name_not_analyzed and it is not analyzed. But I have been wondering if we have the best way via query to solve this such I don't have to reindex. Thanks in advance!
As #jgr pointed out in comment doc2 should be above doc1 by default unless you have your own ranking algorithm or if you are using constant score query or if you are only using filter which would give score of 1 to all documents
Now if you only want doc2, you could use scripting
{
"query": {
"filtered": {
"filter": {
"script": {
"script": "_source.name.toLowerCase()=='box'"
}
}
}
}
}
I am accessing the source itself to check against, also using lowercase to match BOX, Box etc.
Hope this helps!!

Resources