Elasticsearch one field match different values - elasticsearch

I want to do a query, with one field match different values,In SQL it likes:
select * from location where address like '%California%' and address like '%145%';
I tried using must condition array, it contains several phrase match conditions, but its doesnt work!
{
"from" : 0,
"size" : 10,
"query" : {
"bool" : {
"must" : {
"bool" : {
"must" : [ {
"match" : {
"address" : {
"query" : "California",
"type" : "phrase"
}
}
}, {
"match" : {
"address" : {
"query" : "145",
"type" : "phrase"
}
}
} ]
}
}
}
},
"sort" : [ {
"pageRankScore" : {
"order" : "desc",
"unmapped_type" : "double"
}
} ]
}
Thats my code, it only do a match '145', never match 'California'.
My question is: with several values, how to do a fuzzy match in one field?
Help me, thanks a lot!

Related

Can't create the correct filter for my elasticsearch query

I've a problem to set the correct filter. My query looks like this:
{
"query" : {
"bool" : {
"must" : [
{
"query_string" : {
"query" : "example~",
"analyzer" : "standard",
"default_operator" : "OR",
"fuzziness" : "AUTO"
}
},
{
"term" : {
"client" : {
"value" : "MyClient",
"boost" : 1
}
}
},
{
"range" : {
"dateCreate" : {
"gte" : "2016-01-01T00:00:00+0200",
"lte" : "2016-12-31T23:59:59+0200"
}
}
},
{
"match" : {
"lang" : "php OR java"
}
}
]
}
},
"size" : 10,
"from" : 0,
"sort" : [
{
"_score" : {
"order" : "desc"
}
}
]
}
The "lang" field is of type text.
My expectation is to get all documents with the given query string and then I want select only the documents which have "PHP" or "Java" in their lang field. The lang fields only contain either "PHP" or "Java" but never both strings so I thought about using an exact matching but I can't got it to work.
The result is actually a list of two documents but with total_count=2510.
One of my documents that doesn't match:
{
"id" : "d3295f18-a033-4934-941a-21a8bef901e8",
"client" : "MyClient",
"lang" : "PHP",
"author" : null,
"dateCreate" : "2016-03-31T00:00:00+0200",
"title" : "Sample document",
"content" : "This is a short text describing the deocument."
}
Yes, the client field is also of type text.
client field must be either of keyword type to use term query or change the query for client from term to match:
{
"match" : {
"client" : {
"query" : "MyClient",
"boost" : 1
}
}
}

Elasticsearch filter when term mix letters and numbers

I'm doing the following query to search some itens:
{
"filtered" : {
"query" : {
"match" : {
"name_db" : {
"query" : "Human",
"type" : "boolean"
}
}
},
"filter" : {
"terms" : {
"cat" : [ "B8E" ],
"execution" : "bool"
}
}
}
}
See that "cat" field? When it's something like "B8E" there are no results (even though it should), while when it's something like "320" the results are correct. What could be wrong? Why mixing letters and number would be a problem?
Thanks in advance.
PS: I'm new to elasticsearch
I'm pretty sure your field cat is an analyzed string and hence is being indexed in lowercase (and that makes no difference for numbers). If you try this query instead you'll get results.
{
"filtered" : {
"query" : {
"match" : {
"name_db" : {
"query" : "Human",
"type" : "boolean"
}
}
},
"filter" : {
"terms" : {
"cat" : [ "b8e" ], <--- search in lowercase
"execution" : "bool"
}
}
}
}
UPDATE
If you want to index the cat field in uppercase so that you can search it using uppercase (e.g. "B8E"), you need to change its mapping to being not_analyzed, like this:
"cat": {
"type": "string",
"index": "not_analyzed"
}

multiple match must fields not working in elastic search

below query is fetching result if i give existing record that is fine , but if i change name field from 'John' to 'John1' then still record is fetching.
{
"query" : {
"bool" : {
"must" : [
{ "match" : {"employeeId" : "1234"}},
{ "match" : {"name" : "John"}}
]
}
}
}
I tried another alternative query as well but still giving result.which query is correct in terms of performance?but both are giving results if i change name record from 'John' to 'John1'
{
"filter": {
"bool" : {
"must" : {
"term" : {
"employeeId" : "1234"
}
}
}
},
"query": {
"match" : {
"name" : {
"query" : "John",
"type" : "phrase"
}
}
}
}
This because you are doing match, if you want do exact search you need to use filter
Notice we assuce the mapping of name column is analyzed
{
"query" :{
"filtered" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : {"employeeId" : "1234"}},
{ "term" : {"name" : "john"}}
]
}
}
}
}
}

query_string filter inside a nested filter throws an error

In this example I've placed a full query example that's been paired down to the non-working portion that has me stuck.
Basically we have a document that has a nested document type under datailItems and trying to do a query_string filter against a field in that nested document fails. I take the same portion and run it against a non-nested field and it works. So obviously I'm doing something wrong.
The error I receive is nested: QueryParsingException[[******] [_na] filter malformed, must start with start_object]
So, What is the proper way to do this?
Some caveats. The "and"s are used for further filter requirements that contain bools, ranges, etc... I have stripped out those additional reqs for this example.
{
"size" : 1,
"query" : {
"filtered" : {
"filter" : {
"and" : [{
"nested" : {
"path" : "detailItems",
"filter" : {
"and" : [{
"query" : {
"query_string" : {
"detailItems.name" : {
"query" : "mastersettings",
"minimum_should_match" : 1
}
}
}
}
]
}
}
}
]
}
}
}
}
The query_string in the OP is not syntactically correct, below is an example of how the query string could be re-written:
"size" : 1,
"query" : {
"filtered" : {
"filter" : {
"and" : [{
"nested" : {
"path" : "detailItems",
"filter" : {
"and" : [{
"query" : {
"query_string" : {
"fields": [
"detailItems.name"
],
"query" : "mastersettings",
"minimum_should_match" : 1
}
}
}
]
}
}
}
]
}
}
}

Query with multiple values on a property with one value in Elasticsearch

I am trying to build on this query a little bit. The index I am searching also has a field "entity" with an id. So a few records will have "entity" : 16, "entity" 156 etc, depending on the id of the entity. I need to expand this query in such a way that I can pass an array or some list of values in, such as {:term => {:entity => [1, 16, 100]}} and get back records that have one of these integers as their entity value. I haven't had any luck so far, can someone help me?
{
"query" : {
"bool" : {
"must" : [
{
"term" : {"user_type" : "alpha"}
},
{
"term" :{"area" : "16"}
}
],
"must_not" : [],
"should" : []
}
},
"filter": {
"or" : [{
"and" : [
{ "term" : { "area" : "16" } },
{ "term" : { "date" : "05072013" } }
]
}, {
"and" : [
{ "term" : { "area" : "16" } },
{ "term" : { "date" : "blank" } }
]
}
]
},
"from" : 0,
"size" : 100
}
Use "terms" instead of "term".
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
{ "terms" : { "entity" : [ 123, 1234, ... ] }}

Resources