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"
}
}
]
}
}
]
}
}
}
Related
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"
}
}
]
}
We're using Elasticsearch 2.4.5. Have an application that can generate fairly complicated queries. I'm trying to add a more_like_this field to the query like so:
{
"query": {
"more_like_this": {
"fields": [
"title"
],
"ids": [
1234
],
"min_term_freq": 1,
"max_query_terms": 25
},
"function_score": {
"query": {
"bool": {
"must": [
{
"query_string": {
"default_operator": "AND",
"fields": [
"title",
"author"
],
"query": "((title:(\"Tale of Two Cities\"^2)))",
"lenient": true
}
}
],
"filter": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"geo_distance": {
"distance": "50mi",
"location": {
"lat": 49.32,
"lon": -45.67
},
"distance_type": "plane",
"_cache": true
}
}
]
}
},
{
"term": {
"merged": 0
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "title_type"
}
}
}
}
]
}
}
}
},
"functions": [
{
"field_value_factor": {
"field": "quality_score",
"factor": 1,
"missing": 0
}
}
]
}
},
"filter": {
"bool": {
"must": []
}
},
"sort": "_score",
"size": 20,
"from": 0
}
I'm getting a failed to parse search source. expected field name but got [START_OBJECT] error when I try to run the above code. When I remove that piece of code the query executes correctly. I've looked at documentation and other examples of more_like_this usage and I can't determine what's wrong with my query. I'm assuming it has something to do with the way the rest of the query is formed.
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"
}
}
]
}
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"
]
}
}
]
}
}
]
}
}
}
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"
]
}
}
]
}
}
]
}
}
}