Seperate results based on type Elasticsearch - elasticsearch

I am new to Elasticsearch and am struggling with a concept that I hope someone would be kind enough to point me in the right direction. I have a query that returns results of a type Document. This works fine. I want to also have a seperate set of results for objects of type Event. How can I accomplish this?
Any help would be greatly appreciated!
Here is the query that works:
"query": {
"bool": {
"should": [
{ "match": {
"sortableTitle": {
"query": query,
"boost": 3,
"operator": "and"
}}},
{ "match": {
"content": {
"query": query,
"boost": 2,
"operator": "or"
}}},
{ "match": {
"description": {
"query": query,
"boost": 1,
"operator": "or"
}}},
],
"must": {
"match": {
"metaType":{
"query": "Document",
"operator": "and"
}}},
},//end bool
},//end query

You need to specify the type of the documents that you want to get back with your request. The way you specify the type depends on the client that you are using, and it's not part of the request. In case of elasticsearch.js the parameter that you are looking for is called type and it should appear on the same level as body where your request goes:
client.search({
index: 'myindex',
type: 'Document',
body: {
query: {

Related

Does the structure of a match query affect the server

I'm writing some code to generate queries and I wondered if there was any one way of generating the queries that was kinder to the server.
So this query:
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"match": {
"Text": {
"query": "Scooby Shaggy corridor",
"fuzziness": 1,
"operator": "AND"
}
}
}
]
}
}
}
is logically equivalent to this:
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"match": {
"Text": {
"query": "Scooby",
"fuzziness": 1
}
}
},
{
"match": {
"Text": {
"query": "Shaggy",
"fuzziness": 1
}
}
},
{
"match": {
"Text": {
"query": "corridor",
"fuzziness": 1
}
}
}
]
}
}
}
but is either one easier for the server to process?
Or does it make no difference?
I realise this is a trivial example but could it make a difference with more complex queries?
If someone who knows a bit about how ElasticSearch behaves under the hood could make an observation I'd be grateful.
Thanks,
Adam.
Elasticsearch will rewrite itself your multi-term match query to the logical equivalent. see here for more details.
The match query is of type boolean. It means that the text provided is
analyzed and the analysis process constructs a boolean query from the
provided text.
But you should keep the multi-term match query and let elasticsearch do the job. Its more maintainable and you can control the rewriting thanks to the rewrite parameter ( see here )

elasticsearch cross fields query alternative for fuzziness?

I have a cross-fields query, and I understand already that you cant use fuzziness with cross-fields queries, but I dont understand the alternative...
this is my simple query:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "John Legend",
"fields": [
"fname^-4.0",
"lname^-1.0",
"city^-1.0",
],
"type": "cross_fields",
"lenient": "true",
"operator": "AND"
}
}
],
"minimum_should_match": "1"
}
},
"from": 0,
"size": 20
}
I want to be able to find:
John Legend
Joh
John Lege
is that possible?

Elasticsearch filter with multi_match

I'm trying to write a query in ElasticSearch where I combine multi_match with filter for an id or a number og ids.
This is what i have so far:
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "Kasper",
"fields": ["name", "first_name", "last_name"]
}
},
"filter": {
"term": {
"user_id": "ea7528f0-1b8a-11e8-a492-13e39bbd17cb"
}
}
}
}
}
The "must" part of the query works perfectly, and when I run it alone, I get two results.
When I pick out the "user_id" from one of the two results and adds the "filter" part of the query with that id, I get nothing.
What I really want to do is have something like in SQL where user_id in ('id1', 'id2'), so the filtering would be something like:
...,
"filter": {
"terms": {
"user_id": ["ea7528f0-1b8a-11e8-a492-13e39bbd17cb"]
}
}
Did I misunderstand something here?
I'm guessing that this is because user_id field is treated as a text and is analyzed. You should use keyword type in this situation (you need just change the mapping of user_id field.
Another way (if you are on Elasticsearch 5+) you can search in keyword subfield. Just try use below query:
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "Kasper",
"fields": ["name", "first_name", "last_name"]
}
},
"filter": {
"term": {
"user_id.keyword": "ea7528f0-1b8a-11e8-a492-13e39bbd17cb"
}
}
}
}
}
I only changed "user_id" to "user_id.keyword" in your query.

Is it possible to use fuzziness for only one field in a multi_match query?

I am using the following multi_match query in Elasticsearch and I am wondering if I can use fuzziness only for "friendly_name field". I have tried different things but doesn't seem to work. I am also wondering if it possible to use an analyzer to get a similar result as the fuzziness does:
"query": {
"multi_match": {
"query": "input query",
"fields": ["code_short", "code_word","friendly_name"],
"minimum_should_match": "2"
} }, "_source": ["code", "friendly_name"]
Any help would be appreciated. Thanks.
If you only need query on one field , you don't need multi match
"match": {
"name": {
"query": "your query",
"fuzziness": "1.5",
"prefix_length": 0,
"max_expansions": 100,
"minimum_should_match": "80%"
}
}
I don't believe that you can fully replace fuzziness, but you have 2 options to explore that might work for you. ngram filter or stemmer filter.
======
Well it wasn't very clear to me what you've intended. But you can do your query that way:
"query": {
"bool": {
"should": [
{
"match": {
"friendly_name": {
"query": "text",
"fuzziness": "1.5",
"prefix_length": 0,
"max_expansions": 100
}
}
},
{
"match": {
"code_word": {
"query": "text"
}
}
},
{
"match": {
"code_short": {
"query": "text"
}
}
}
],
"minimum_should_match" : 2
}
}

ElasticSearch: Search across multiple fields with input strings (NumberFormatException)

I am searching across multiple fields in a query using the * asterisk notation (Ex: I want all fields startings with source so I specify fields source.*) and I specify a query of foobar as string. I am using a Query String type query.
I keep getting a NumberFormatException and I have some fields in there with a mapping type of long and double.
Any idea how to go about this? I need to do a multi-field search.
My query is posted below:
{
"query": {
"bool": {
"must": [{
"query_string": {
"default_field": "source.*",
"query": "foobar"
}
}],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 100000,
"sort": [],
"facets": {}
}
set lenient to true to ignore format based failures
example :
"query_string":
{
"default_field": "source.*",
"query": "foobar",
"lenient": true
}

Resources