Elastic search conversion php - mysql

I want to convert below MySQL query to elastic search in php
SELECT id,username,full_name
FROM users
WHERE id<>4 AND (username LIKE %john% OR full_name LIKE %full_name%)
LIMIT 50 OFFSET 0
Please educate

It'd be something like this:
GET /users/_search?pretty=true
{
"size": 50,
"query": {
"bool": {
"should": [
{
"terms": {
"username": "john"
}
},
{
"terms": {
"full_name": "full_name"
}
}
],
"must_not": [
{
"term": {
"id": "4"
}
}
]
}
}

Related

Elastic Search SQL Equivalent

Im having some problems querying with elasticsearch. So basically i wanted to know the sql equivalent of elasticsearch.
Whats the equivalent elasticsearch syntax for this sql query ?
SELECT *
FROM people
WHERE name LIKE( $name% )
AND ( gender = 0 OR age < 18 )
AND id IN( 1, 2, 3 )
AND id NOT IN( 4, 5, 6 )
AND dead = 0
ORDER BY status desc,
TIME desc
* is just for example
Using boolean query (and nested boolean query ) allows you to express same things than sql request.
Terms query is validated when one of its elements is found (OR).
POST /index_name/people/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "John"
}
}
],
"filter": [
{
"bool": {
"must": [
{
"terms": {
"id": [
1,
2,
3
]
}
},
{
"term": {
"dead": 0
}
},
{
"bool": {
"should": [
{
"term": {
"gender": 0
}
},
{
"range": {
"age": {
"lt": 18
}
}
}
]
}
}
],
"must_not": [
{
"terms": {
"id": [
4,
5,
6
]
}
}
]
}
}
]
}
},
"sort": [
{
"status": {
"order": "desc"
},
"time": {
"order": "desc"
}
}
]
}

Elasticsearch query that use 'terms' and 'missing' at same time

I am trying to use this query to get all documents with a closed or pending status or with no status at all.but this query always return nothing.
"query": {
"bool": {
"should": [
"terms": {
"status": ["closed","pending"]
},
"missing":{
"field":"status"
}
]
}
}
Individually the queries are working.If I use:
"query": {
"bool": {
"should": [
"terms": {
"status": ["closed","pending"]
}
]
}
}
then it returns 2 documents.
And if I use
"query": {
"bool": {
"should": [
"missing":{
"field":"status"
}
]
}
}
it returns 3.
These results are correct for each query.I just want to combine the 2 queries and get the result 5.What is wrong with the first query?
This ES 5.0 query and it should work on version 2.0
POST es1/example/_search
{
"query": {
"bool": {
"should": [
{
"terms": {
"status": [
"closed",
"pending"
]
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "status"
}
}
]
}
}
]
}
}
}

How does elasticsearch find the intersection of two search results like mysql?

query name sets from 20151216 to 20151217
{
"from": 0,
"size": 200,
"query": {
"bool": {
"must": {
"range": {
"DATE": {
"from": 20151216,
"to": 2015121617,
"include_lower": true,
"include_upper": true
}
}
}
}
},
"_source": {
"includes": [
"NAME"
],
"excludes": []
}
}
Another day
{
"from": 0,
"size": 200,
"query": {
"bool": {
"must": {
"range": {
"DATE": {
"from": 20151217,
"to": 2015121618,
"include_lower": true,
"include_upper": true
}
}
}
}
},
"_source": {
"includes": [
"NAME"
],
"excludes": []
}
}
If in MYSQL I will use the following SQL to solve my problem.
SELECT NAME FROM Table1 where DATE between 20151216 and 20151217 intersect SELECT NAME FROM Table1 where DATE between 20151217 and 20151218
How does elasticsearch to find the intersection of two search results like mysql?

filter '_index' same way as '_type' in search across multiple index query elastic search

I have two indexes index1 and index2 and both has two types type1 and type2 with same name in elastic search.(please assume that we have valid business reason behind it)
I would like to search index1 - type1 and index2 -type2
here is my query
POST _search
{
"query": {
"indices": {
"indices": ["index1","index2"],
"query": {
"filtered":{
"query":{
"multi_match": {
"query": "test",
"type": "cross_fields",
"fields": ["_all"]
}
},
"filter":{
"or":{
"filters":[
{
"terms":{
"_index":["index1"], // how can i make this work?
"_type": ["type1"]
}
},
{
"terms":{
"_index":["index2"], // how can i make this work?
"_type": ["type2"]
}
}
]
}
}
}
},
"no_match_query":"none"
}
}
}
You can use the indices, type in a bool filter to filter on type and index
The query would look something on these lines :
POST index1,index2/_search
{
"query": {
"filtered": {
"query": {
"multi_match": {
"query": "test",
"type": "cross_fields",
"fields": [
"_all"
]
}
},
"filter": {
"bool": {
"should": [
{
"indices": {
"index": "index1",
"filter": {
"type": {
"value": "type1"
}
},
"no_match_filter": "none"
}
},
{
"indices": {
"index": "index2",
"filter": {
"type": {
"value": "type2"
}
},
"no_match_filter": "none"
}
}
]
}
}
}
}
}
Passing the index names in the url example : index1,index2/_search is a good practice else you risk executing query across all indices in the cluster.

Match query with multiple values

I have a list of values and I want all documents that have any of these values in their product_code field.
I tried this, but even though it doesn't give an error, it only gives me one of the documents:
"query": {
"match": {
"product_code": {
"query": ["ABC 4", "ABC 5"]
}
}
}
So I'm basically looking for the functionality of the terms filter, but with analysis.
Of course I could do:
"bool": {
"should": [
{
"query": {
"match": {
"product_code": "ABC 4"
}
}
},
{
"query": {
"match": {
"product_code": "ABC 5"
}
}
}
]
}
but this gets rather verbose for long lists.
product_code is defined like this:
"product_code": {
"type": "string",
"analyzer": "product_code",
}
with the product_code analyzer:
"product_code": {
"type": "custom",
"filter": [
"lowercase",
"trim"
],
"tokenizer": "keyword"
}
There isn't an equivalent of the terms query for match AFAIK. Another option is the query_string which is less verbose:
{
"query": {
"query_string": {
"default_field": "product_code",
"query": "\"ABC 4\" OR \"ABC 5\""
}
}
}
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
I might be missing something, but how about:
{
"constant_score" : {
"filter" : {
"terms" : { "product_code" : ["ABC 4", "ABC 5"]}
}
}
}
Could this be what you're looking for?