How to use multi_match with indices in elasticsearch? - elasticsearch

I have 2 indices USER and URL. I want to run a query on different fields based on the index.
In USER index, query should search in name and id field.
But in URL search has to be performed on title and id field.
POST /_search
{
"query":{
"indices":[
{
"indices":[
"URL"
],
"query":{
"multi_match":{
"query":"SMU ",
"fields":[
"title",
"id"
]
}
}
},
{
"indices":[
"USER"
],
"query":{
"multi_match":{
"query":"SMU ",
"fields":[
"name",
"id"
]
}
}
}
]
}
}
The above query is not working. What is the change required to make it work.
How can I merge multi_match search with indices search?

The indices query is deprecated in ES 5, but it still works, you just have a bad structure in yours, i.e. you need to put each indices query in a bool/filter clause.
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"indices": {
"indices": [
"URL"
],
"query": {
"multi_match": {
"query": "SMU ",
"fields": [
"title",
"id"
]
}
}
}
},
{
"indices": {
"indices": [
"USER"
],
"query": {
"multi_match": {
"query": "SMU ",
"fields": [
"name",
"id"
]
}
}
}
}
]
}
}
}
Since the indices query is deprecated, the new idea is to use a simple term query on the _index field instead. Try this:
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"filter": [
{
"term": {
"_index": "URL"
}
},
{
"multi_match": {
"query": "SMU ",
"fields": [
"title",
"id"
]
}
}
]
}
},
{
"bool": {
"filter": [
{
"term": {
"_index": "USER"
}
},
{
"multi_match": {
"query": "SMU ",
"fields": [
"name",
"id"
]
}
}
]
}
}
]
}
}
}

Related

Elasticsearch multiple fields wildcard bool query

Currently using bool query which searches for a combination of both input words or either one of input word on field "Name". How to search on multiple fields using wild cards?
POST inventory_dev/_search
{"from":0,"query":{"bool":{"must":[{"bool":{"should":[{"term":{"Name":{"value":"dove"}}},{"term":{"Name":{"value":"3.75oz"}}},{"bool":{"must":[{"wildcard":{"Name":{"value":"*dove*"}}},{"wildcard":{"Name":{"value":"*3.75oz*"}}}]}}]}}]}},"size":10,"sort":[{"_score":{"order":"desc"}}]}
You can use query_string in place of wildcard query, to search on multiple fields
{
"from": 0,
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"term": {
"Name": {
"value": "dove"
}
}
},
{
"term": {
"Name": {
"value": "3.75oz"
}
}
},
{
"bool": {
"must": [
{
"query_string": {
"query": "*dove*",
"fields": [
"field1",
"Name"
]
}
},
{
"query_string": {
"query": "*3.75oz*",
"fields": [
"field1",
"Name"
]
}
}
]
}
}
]
}
}
]
}
},
"size": 10,
"sort": [
{
"_score": {
"order": "desc"
}
}
]
}

how to use SHOULD inside MUST in elasticsearch6?

I am very new to elasticsearch trying to understand things better but struggling a bit. I am trying to write query with AND(must) and OR(should) condition. Below elasticsearch query works good. But I need one more zipcode to be added in the condition, it should be or between 2 zipcodes.
Working Query:
{
"_source": {
"includes": [
"Name",
"Address",
"Contact"
]
},
"query": {
"bool": {
"must": [
{
"query_string": {
"fields": [
"firstName",
"lastName",
"middleName"
],
"query": "*andy*"
}
},
{
"match": {
"zipcode": "55555"
}
}
]
}
}
}
how to add zipcode value match in OR condition?
You can either replace match by terms:
{
"terms": {
"zipcode": ["55555", "55556"]
}
}
Or you can add two match clauses in a bool/should:
"bool": {
"must": [
{
"query_string": {
"fields": [
"firstName",
"lastName",
"middleName"
],
"query": "*andy*"
}
}
],
"minimum_should_match": 1,
"should": [
{
"match": {
"zipcode": "55555"
}
},
{
"match": {
"zipcode": "55556"
}
}
]
}

Elasticsearch 5: Boost 1 field

I have 3 types with the same fields names indexed in Elasticsearch 5:
TypeA
integer id
string name
TypeB
integer id
string name
TypeC
- integer id
- string name
GET myindex/TypeA,TypeB,TypeC/_search
{
"_source": ["id", "name"],
"query": {
"bool": {
"should": [
{
"query_string": {
"fields": [
"_all",
"name^3"
],
"query": "Foo bar*",
"default_operator": "and"
}
}
]
}
}
}
I only want to boost the name field for TypeA. In this scenario the name field for TypeA, TypeB and TypeC are boosted.
How can I only boost the name for TypeA?
I'm looking for something like this:
"fields": [
"_all",
"TypeA.name^3"
]
Thank You.
You need to create one subquery for type A and another subquery for types B and C
GET myindex/_search
{
"_source": [
"id",
"name"
],
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"_type": "TypeA"
}
},
{
"query_string": {
"fields": [
"_all",
"name^3"
],
"query": "Foo baar*",
"default_operator": "and"
}
}
]
}
},
{
"bool": {
"must": [
{
"terms": {
"_type": [
"TypeB",
"TypeC"
]
}
},
{
"query_string": {
"fields": [
"_all",
"name"
],
"query": "Foo baar*",
"default_operator": "and"
}
}
]
}
}
]
}
}
}

Searching in specific fields of types

Consider the following query:
{
"query" : {
"match_phrase" : {
"_all" : "Smith"
}
}
}
How would I specify in which fields of which types it may search, instead of searching in everything? (field names may be non-unique across types)
I've tried the query below, but it didn't work (it doesn't return results, it does when I remove person. from all fields):
{
"query": {
"multi_match": {
"query": "Smith",
"fields": [
"person.first_name",
"person.last_name",
"person.age"
],
"lenient": true
}
}
}
I'm sending these queries to http://localhost:9200/tsf-model/_search.
If you can build your query dynamically, I think you can use a combination of your multi_match query and a type query for each type, in order to achieve what you want:
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"filter": [
{
"type": {
"value": "type1"
}
},
{
"multi_match": {
"query": "Smith",
"fields": [
"field1",
"field3",
"field5"
]
}
}
]
}
},
{
"bool": {
"filter": [
{
"type": {
"value": "type2"
}
},
{
"multi_match": {
"query": "Smith",
"fields": [
"field2",
"field4",
"field6"
]
}
}
]
}
}
]
}
}
}

How to add filter to a more like this query in Elastic Search?

I want to use a More like this query in Elastic Search to find similar documents. However I need to filter the documents on which the query is executed.
Example below: I want to find blog items that are similar to blog with id 123456, but written by author 120 or author 123.
When executing this query, I get back similar blogs from ALL authors and thus not filtered ...
{
"query":{
"more_like_this" : {
"fields" : ["body" ],
"docs" : [
{
"_id" : "123456"
}
],
"percent_terms_to_match" : 0.4,
"min_term_freq" : 1
}
}
},
"filter":{
"and":[
{
"type":{ "value":"blog" }
},
{
"terms":{ "authorId": ["120", "123"] }
}
]
}
}
Try filtered query like this:
{
"query": {
"filtered": {
"query": {
"more_like_this": {
"fields": [
"body"
],
"docs": [
{
"_id": "123456"
}
],
"percent_terms_to_match": 0.4,
"min_term_freq": 1
}
},
"filter": {
"and": [
{
"type": {
"value": "blog"
}
},
{
"terms": {
"authorId": [
"120",
"123"
]
}
}
]
}
}
}
}
Hope it helps...!
The accepted answer is for earlier versions of ElasticSearch. This one works great on 2.x+ also not using any depreciated APIs
{
"query": {
"filtered": {
"query": {
"more_like_this": {
"fields": ["meta.keywords"],
"like": [{"_id": "5732759249d2b21f95641d50"}]
}
},
"filter" : {
"bool": {
"must": [
{"match": { "foo.bar": "A"}},
{"match": { "baz": "new"}}
]
}
}
}
}
}

Resources