How to queryexactly when a field has long text in Elasticsearch? - sql

I have a filed with 100~300 characters. If I want to query this field, if I just use the sentences like:
GET _search
{
"query": {
"match": {
"question": {
"query":"asdasdasd",
"minimum_should_match": "75%"
}
}
}
}
Even if just tap the keyboard, I also can get some results.But these results are not relevant at all! I don't want to get them. What I can do to prevent the return of these results? Thanks!

Related

Creating a couchdb view to index if item in an array exists

I have the following sample documents in my couchdb. The original table in production has about 2M records.
{
{
"_id": "someid|goes|here",
"collected": {
"tags": ["abc", "def", "ghi"]
}
},
{
"_id": "someid1|goes|here",
"collected": {
"tags": ["abc", "klm","pqr"]
},
},
{
"_id": "someid2|goes|here",
"collected": {
"tags": ["efg", "hij","klm"]
},
}
}
Based on my previous question here, how to search for values when the selector is an array,
I currently have an index added for the collected.tags field, but the search is still taking a long time. Here is the search query I have.
{
"selector": {
"collected.tags": {
"$elemMatch": {
"$regex": "abc"
}
}
}
}
There are about 300k records matching the above condition, there search seems to take a long time. So, I want to create a indexed view to retrieve and lookup faster instead of a find/search. I am new to couchdb and am not sure how to setup the map function to create the indexed view.
Figured the map function out myself. Now all the documents are indexed and retrievals are faster
function (doc) {
if(doc.collected.tags.indexOf('abc') > -1){
emit(doc._id, doc);
}
}

MarkLogic - Xpath on JSON document

MarkLogic Version: 9.0-6.2
I am trying to apply Xpath in extract-document-data (using Query Options) on a JSON document shown below. I need to filter out "Channel" property if the underneath property "OptIn" has a value of "True".
{
"Category":
{
"Name": "Severe Weather",
"Channels":[
{
"Channel":
{
"Name":"Email",
"OptIn": "True"
}
},
{
"Channel":
{
"Name":"Text",
"OptIn": "False"
}
}
]
}
}
I tried below code,
'<extract-document-data selected="include">' +
'<extract-path>//*[OptIn="True"]/../..</extract-path>' +
'</extract-document-data>' +
which is only pulling from "Channel" property as shown below.
[
{
"Channel": {
"Name": "Email",
"OptIn": "True"
}
}
]
But my need is to pull from parent "Category" property, but filter out the Channels that have OptIn value as False.
Any pointers?
If I understand correctly, you'd like to extract 'Category', but only with those 'Channel's that have 'OptIn' equalling 'true', right?
Extract-document-data is not advanced enough for that. You best extract entire Categories which have at least one OptIn equalling true (//Category[//OptIn = 'true']), and use a REST transform on the search response to trim down the unwanted Channels..
HTH!

Search for a numeric range inside string in elastic search

I wanted to search a numeric expression in elastic search.
Example
indent code 4.8663 spaces
indent code 121.232 spaces
indent code 12.3232 spaces
Example query
get all string with "indent code between 1 and 100"
It should get 1st and 3rd but not 2nd.
{
"span_near": {
"in_order": 1,
"clauses": [
{
"span_term": {
"request": "indent"
}
},
{
"span_term": {
"request": "code"
}
}
,
{
"span_multi": {
"match":{
"range": {
"request": {
"to": 100,
"from": 1
}
}
}
}
}
],
"slop": 0,
"collect_payloads": 0
}
}
giving wrong result. as it is comparing using TermRangeQuery rather than NumericRangeQuery.
If you can either replace float numbers by integer numbers (4.8663 => 5) or multiply your float numbers by a chosen power of 10 so that all numbers become integers (4.8663 => 48663), then you might be able to use the regexp query for this.
I've indexed three documents with integer numbers (5, 121 and 12) and I've been able to successfully retrieve the two in the 1-100 interval using the following query.
{
"query": {
"regexp": {
"request": {
"value": "<1-100>"
}
}
}
}
If you absolutely need to keep the precision for other reasons, then this might not work out for you.

elastic search query filter out ids by wildcard

I'm hoping to create a query where it will filter out IDs containing a wildcard. For instance, I would like to search for something everywhere except where the ID contains the word current. Is this possible?
Yes it is possible using Regex Filter/Regex Query. I could not figure a way to directly do it using the Complement option hence I've used bool must_not to solve your problem for the time being. I'll refine the answer later if possible.
POST <index name>/_search
{
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must_not": [
{
"regexp": {
"ID": {
"value": ".*current.*"
}
}
}
]
}
}
}

How do I sort ElasticSearch when it's empty?

Sometimes, I have nothing in the index, sometimes, I have some documents. That's just the nature of my application. When the index does contain documents, I sort by "final_score" descending. My query looks like this:
GET /_search
{
"query": {
"match_all":{}
},
"sort":[
{ "final_score" : "desc" }
]
}
However, this query breaks when there are 0 documents in the index. I would have to remove the sort to make the query work.
How can I make this query work with any amount of documents (0, or more?)
If you don't have field and ask elasticsearch to sort by that field then there is problem,
So,Have mapping for final_score, so that it will not throw error (if nothing is indexed also).
Example:
POST http://localhost:9200/index/type/_mapping
{
"type": {
"properties": {
"final_score": {
"type": "integer"
}
}
}
}