My sample index and document structure looks like this :
http://localhost:9200/testindex/
{
"settings": {
"analysis": {
"analyzer": {
"autocomplete": {
"tokenizer": "whitespace",
"filter": [
"lowercase",
"autocomplete"
]
},
"autocomplete_search": {
"tokenizer": "whitespace",
"filter": [
"lowercase"
]
}
},
"filter": {
"autocomplete": {
"type": "nGram",
"min_gram": 2,
"max_gram": 40
}
}
}
},
"mappings": {
"table1": {
"properties": {
"title": {
"type": "string",
"index": "not_analyzed"
},
"type": {
"type": "string",
"index": "not_analyzed"
},
"type1": {
"type": "string",
"index": "not_analyzed"
},
"id": {
"type": "string",
"analyzer": "autocomplete",
"search_analyzer": "autocomplete_search"
}
}
}
}
}
http://localhost:9200/testindex/table1/1
{
"title": "mumbai",
"type": "efg",
"type1": "efg1",
"id": "Caryle management"
}
http://localhost:9200/testindex/table1/2
{
"title": "canada",
"type": "abc",
"type1": "abc1",
"id": "labson series 2014"
}
http://localhost:9200/testindex/table1/3/
{
"title": "ny",
"type": "abc",
"type1": "abc1",
"id": "labson series 2012"
}
http://localhost:9200/testindex/table1/4/
{
"title": "pune",
"type": "abc",
"type1": "abc1",
"id": "hybrid management"
}
Query used to get all documents where type = "abc" and "efg" and have id equal to labson and management .
{
"query": {
"bool": {
"filter": {
"query": {
"terms": {
"type": [
"abc",
"efg"
]
}
}
},
"minimum_should_match": 1,
"should": [
{
"query": {
"bool": {
"must": [
{
"term": {
"_type": "table1"
}
},
{
"bool": {
"should": [
{
"match": {
"id": {
"query": "labson ",
"operator": "and"
}
}
},
{
"match": {
"id": {
"query": "management",
"operator": "and"
}
}
}
]
}
}
]
}
}
}
]
}
}
}
"hits": [
{
"_index": "testindex",
"_type": "table1",
"_id": "2",
"_score": 1,
"_source": {
"title": "canada",
"type": "abc",
"type1": "abc1",
"id": "labson series 2014"
}
}
,
{
"_index": "testindex",
"_type": "table1",
"_id": "4",
"_score": 1,
"_source": {
"title": "pune",
"type": "abc",
"type1": "abc1",
"id": "hybrid management"
}
}
,
{
"_index": "testindex",
"_type": "table1",
"_id": "1",
"_score": 1,
"_source": {
"title": "mumbai",
"type": "efg",
"type1": "efg1",
"id": "Caryle management"
}
}
,
{
"_index": "testindex",
"_type": "table1",
"_id": "3",
"_score": 1,
"_source": {
"title": "ny",
"type": "abc",
"type1": "abc1",
"id": "labson series 2012"
}
}
]
So i need help for the issues in this output .
- Why is labson series 2012 as the last document in the result ?Although my search criteria wants to first look into labson and then management .How can i add boost or weight the labson keyword over management .So the output should give me all documents that matches labson and then management based on the order of input in the match clause .
- How can i add a filter at the top which should read has ,give me all documents which has type in ("abc" , "efg") and type1 in ("abc").Right now i am only searching for type in ("abc","efg") ,how can i modify the query to include the IN clause for type1 field.
Please provide some pseudo code for the above 2 query solution as i am new to ES ,that would help me out immensely
Thanks in advance