elastic search query filter out ids by wildcard - lucene

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.*"
}
}
}
]
}
}
}

Related

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

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!

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);
}
}

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"
}
}
}
}

protect certain phrases for search

I am currently trying to improve the corner cases of my elasticsearch-results. One particular is giving me a headache: "google+" which is simply reduced to "google". Omitting special chars is usually fine but for this one I would want an exception. Any ideas how to achieve this?
I tried the following setup:
{
"index": {
"analysis": {
"analyzer": {
"default": {
"tokenizer": "standard",
"filter": [
"synonym",
"word_delimiter"
]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms_path": "analysis/synonym.txt"
},
"word_delimiter": {
"type": "word_delimiter",
"protected_words_path": "analysis/protected.txt"
}
}
}
}
}
protected.txt contains one line with google+
I guess the Standard tokenizer is stripping out the + from google+. You can check it using the analyze api. I'd use the Whitespace tokenizer instead and properly configure the Word delimiter token filter that you're already using.
I think pattern replace would be a better idea - http://www.elasticsearch.org/guide/reference/index-modules/analysis/pattern_replace-tokenfilter.html

Elasticsearch/Tire text query DSL for excluding certain fields from being searched

I have a elastic search query like the following,
{
"query": {
"bool": {
"must": [
{
"query_string": {
"fields": ["title"],
"query": "test"
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 50,
"sort": [],
"facets": {}
}
I am able to execute an elastic search query on certain fields by giving a fields param to query_string as mentioned above. In my index mapping i have around 50 fields indexed. How do i query for all but one field. Something like an exclude option to query string. Is it possible with Tire/Elastic Search ?
I assumed it cannot be done and proceeded with getting all the mappings and parsing the hash which kinda sucks actually.