EzDevInfo.com

Elastica

Elastica is a PHP client for elasticsearch Elastica - Elastica elastica - php client for elasticsearch

How to config Single node for Single Cluster (Standalone Cluster) ElasticSearch

I have my local machine. I want to config elastic search for only single cluster for single node. (Standalone Server)Means whatever I will created new index, it will available for only to me. Its not accesible to other.

My current scenario these indexes are available to other, so they can make any changes.But I don't want it.

I went through some other blogs but not getting best solution. So can you please let me know steps for same?


Source: (StackOverflow)

How to configure correctly FOS Elastica analyzers and filters?

In my symfony2 application, I am using FOS Elastica bundle to perform searches.

I have tried to set up analyzers and filters but it seems they just have no effect. For instance, if I search for the word 'cake', the objects containing the sentence case 'Cake' won't be returned.

How can I configure those analyzers and filters correctly ?

My config :

#Elastic Search
fos_elastica:
    default_manager: orm
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        website:
            client: default
            settings:
                index:
                    analysis:
                        analyzer:
                            custom_index_analyzer :
                                type     :    custom
                                tokenizer:    nGram
                                filter   :    [stopwords, asciifolding ,lowercase, snowball, elision, worddelimiter]
                            custom_search_analyzer :
                                type     :    custom
                                tokenizer:    nGram
                                filter   :    [stopwords, asciifolding ,lowercase, snowball, elision, worddelimiter]
                        tokenizer:
                            nGram:
                                type:     nGram
                                min_gram: 1
                                max_gram: 2
                        filter:
                            snowball:
                                type:     snowball
                                language: French
                            elision:
                                type:     elision
                                articles: [l, m, t, qu, n, s, j, d]
                            stopwords:
                                type:      stop
                                stopwords: [_french_]
                                ignore_case : true
                            worddelimiter :
                                type:      word_delimiter
            index_name: foodmeup
            types:
                recipe:
                    mappings:
                        name:
                            boost: 5
                            index_analyzer : custom_index_analyzer
                            search_analyzer : custom_search_analyzer
                        nickName:
                            index_analyzer : custom_index_analyzer
                            search_analyzer : custom_search_analyzer
                        content:
                            index_analyzer : custom_index_analyzer
                            search_analyzer : custom_search_analyzer
                        recipeIngredients:
                            type: "nested"
                            properties:
                                name:
                                    index_analyzer : custom_index_analyzer
                                    search_analyzer : custom_search_analyzer
                                product:
                                    type: "nested"
                                    properties:
                                        name: { type: string, boost: 10}
                                        nickName: { type: string }
                                        content: { type: string }
                                        tags:
                                            type: "nested"
                                            boost: 5
                                            properties:
                                                name: { type: string }
                        userRecipes:
                            type: "nested"
                            properties:
                                name:
                                    index_analyzer : custom_index_analyzer
                                    search_analyzer : custom_search_analyzer
                                content:
                                    index_analyzer : custom_index_analyzer
                                    search_analyzer : custom_search_analyzer
                        tags:
                            type: "nested"
                            boost: 5
                            properties:
                                name:
                                    index_analyzer : custom_index_analyzer
                                    search_analyzer : custom_search_analyzer
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\FoodAnalytics\Recipe
                        repository: AppBundle\Repository\FoodAnalytics\RecipeRepository
                        provider: ~
                        finder: ~
                        listener: ~ # by default, listens to "insert", "update" and "delete"
                product:
                    mappings:
                        name: { type: string, boost: 10}
                        nickName: { type: string }
                        content: { type: string }
                        userIngredients:
                            type: "nested"
                            properties:
                                name: { type: string }
                                content: { type: string }
                        tags:
                            type: "nested"
                            boost: 5
                            properties:
                                name: { type: string }
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\MarketPlace\Product
                        repository: AppBundle\Repository\MarketPlace\ProductRepository
                        provider: ~
                        finder: ~
                        listener: ~ # by default, listens to "insert", "update" and "delete"
                user:
                    mappings:
                        firstName: { type: string, boost: 3}
                        lastName: { type: string, boost: 10 }
                        content: { type: string }
                        username: { type: string }
                        email: { type: string }
                        jobSeeker:
                            type: "nested"
                            properties:
                                skills:
                                    type: "nested"
                                    properties:
                                        name: { type: string }
                                experiences:
                                    type: "nested"
                                    properties:
                                        position:
                                            type: "nested"
                                            properties:
                                                name: { type: string }
                                        content: { type: string }
                                trainings:
                                    type: "nested"
                                    properties:
                                        name: { type: string }
                                        content: { type: string }
                                        diploma:
                                            type: "nested"
                                            properties:
                                                name: { type: string }
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\User\User
                        repository: AppBundle\Repository\User\UserRepository
                        provider: ~
                        finder: ~
                        listener: ~ # by default, listens to "insert", "update" and "delete"
                organization:
                    mappings:
                        name: { type: string, boost: 10}
                        legalName: { type: string, boost: 10}
                        shortDescription: { type: string, boost: 3}
                        route: { type: string}
                        content: { type: string }
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\User\Organization
                        repository: AppBundle\Repository\User\OrganizationRepository
                        provider: ~
                        finder: ~
                        listener: ~ # by default, listens to "insert", "update" and "delete"
                offer:
                    mappings:
                        name: { type: string, boost: 10}
                        content: { type: string }
                        responsibilities: { type: string }
                        skills:
                            type: "nested"
                            properties:
                                name: { type: string }
                        contractType:
                            type: "nested"
                            properties:
                                name: { type: string }
                        position:
                            type: "nested"
                            properties:
                                name: { type: string, boost: 10 }
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\Job\Offer
                        repository: AppBundle\Repository\Job\OfferRepository
                        provider: ~
                        finder: ~
                        listener: ~
                post:
                    mappings:
                        name: { type: string, boost: 10}
                        content: { type: string }
                        summary: { type: string }
                        tags:
                            type: "nested"
                            boost: 5
                            properties:
                                name: { type: string }
                        comments:
                            type: "nested"
                            properties:
                                content: { type: string }
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\Social\Post
                        repository: AppBundle\Repository\Social\PostRepository
                        provider: ~
                        finder: ~
                        listener: ~

The query is a basic one :

$finder = $this->container->get('website.recipe')
$elasticaResults = $finder->find($search);

Source: (StackOverflow)

Advertisements

How to query using Elastica

This is my first time using Elastica and querying data from the ElasticSearch

For me, as starter I have a question on how to query the code below using Elastica?:

curl 'http://localhost:9200/myindex/_search?pretty=true' -d '{     
  "query" : {
    "term": {
      "click": "true"
    }   },   "facets" : {
    "matches" : {
      "terms" : {
          "field" : "pubid",
          "all_terms" : true,
          "size": 200 
      }
    }   
  } 
}'

Hope someone can lend me an arm here.

Thanks,


Source: (StackOverflow)

Maintain the natural sort of overall results, but sort a subfield array within each result in elasticsearch?

Our defined Type is something like this:

'title' => ... ,
'body' => ... ,
'links' => 'type'=>'object', 'properties'=> array
        'link' => ... ,
'locations' => 'type'=>'object', 'properties'=> array
        'label' => ... ,
        'pin' => ...
)

The records/documents represent businesses that reside in one or more categories, and so the links array will contain all of the potential links to a business, i.e.

[0] => '/Businesses/Hotels/My-Business/',
[1] => '/Businesses/Resorts/My-Business/',
[2] => '/Businesses/Fractional-Ownership/My-Business/'

So when we run a query on the terms Business Resort this listing is included in the result set. At the moment though, we don't know which link would be most appropriate to display on the results page, so we just default to the first, in this case the one with ../Hotels/.. in the path.

Is it possible to order the links according to their own score/relevancy within the search so that the link order on the returned result would instead be:

[0] => '/Businesses/Resorts/My-Business/',
[1] => '/Businesses/Hotels/My-Business/',
[2] => '/Businesses/Fractional-Ownership/My-Business/'

The order of the links should not have any influence on the order of natural results from the overall query.


EDIT : The second use case which I've added above is, we also store locations for each business, and would like to order the location list for each resulting business by their proximity to a set of coordinates. We know how to order the entire result set by _geo_distance but need to know how to do it on a specific field, and like above, without affecting the overall result order.


Source: (StackOverflow)

How to perform a Prefix query with Elastica

I'm attempting to create an autocomplete search using Elastica and Elastic Search (more specifically, FOSElasticaBundle for Symfony2).

I've created a simple index, populated it with objects.

A basic search works, ie:

$finder = $this->container->get('fos_elastica.finder.search.person');
$results = $finder->find('Mike');

I'm having some trouble getting my prefix query to work though and I can't find any examples online. Here's what I'm trying:

$finder = $this->container->get('fos_elastica.finder.search.person');
$prefixQuery = new \Elastica\Query\Prefix();
$prefixQuery->setPrefix('nameFirst','Mik');
$results = $finder->find($prefixQuery);

It doesn't kick any errors, but just doesn't return any results.

The field should be set properly...my configuration looks like this:

...
            types:
                person:
                    mappings:
                        nameFirst: { boost: 10 }
                        nameLast: { boost: 10 }
                        nameMiddle: { boost: 3 }
...

Can anyone tell what I'm doing wrong?

Also: Bonus Question: Is the best way to search the prefix on both the nameFirst AND nameLast fields going to be using a NestedQuery, or using addParam() to add both the nameFirst and nameLast?

Thanks a bunch-


Source: (StackOverflow)

Elasticsearch update same document simultaneous results in VersionConflictEngineException

My workers process objects and the write them to mysql and elasticsearch, now it happens that 2 (or multiple workers) process similar objects and update the same ES document.

Resulting in the following error:

VersionConflictEngineException[[test][4] [seller][2678449]: version conflict, current [18], provided [17]]

I did some research and tried to use the retry_on_conflict option but this resulted in the following error:

ActionRequestValidationException[Validation Failed: 1: can't provide both retry_on_conflict and a specific version;]

I use Elastica version 1.2.1 And Elasticsearch version 1.2.1


Source: (StackOverflow)

How to get existing index object, if index exists in php elastica

I am new to elastic search. I am using php elastica client and facing a problem: If an index exists, I want to get the object of this existing index and not recreate it. How can this be done?

client = new \Elastica\Client($arrServerConf, $callback);

if ( $client->getIndex($name)->exists() ) {
      //do something here to get this existing object -- what to do here???
} else {
      // create a new one
      $index = $client->getIndex($name);
      $index->create(array('index' => array('number_of_shards' => $shards, 'number_of_replicas' => 0)), $delete);
}
$type = $index->getType($typeName);

Or is there some other way of doing this? I need this because, I'll be adding documents in this index and searching among them.


Source: (StackOverflow)

Elastica PHP configure multiple servers

I want configure PHP Elastica Client to connect with multiple elasticsearch instances. I have one master and slave node running on different IPs but same port. The issue is that elastica don't recognize none of them, neither master server nor slave. This is the snapshot of code I'm using to create elastica client.

$servers = [];

//Adding master node host and port
 $servers[] = [ 'host' => getenv('NODE_MASTER_HOST'),'port' =>    getenv('NODE_MASTER_PORT') ];

//Adding slaves node host and port. actually NODE_SLAVES_COUNTS = 1
for($i = 1; $i <= (int) getenv('NODE_SLAVES_COUNTS'); $i++){
    if (!is_null(getenv('NODE_SLAVES'.$i.'_HOST'))){
        $servers[] = ['host' => getenv('NODE_SLAVE'.$i.'_HOST'), 'port' =>   getenv('NODE_SLAVE'.$i.'_PORT')];
     }
}

$client = new ElasticClient(array(
'servers'=>$servers
));


 //Setting elasticserach custom cluster name
$settings = new Settings($client);
$settings->set(['cluster.name' => getenv('CLUSTER_NAME')]);

$client = $settings->getClient();

What I'm missing here ?

EDIT

This is what I'm getting dumping $client object

Client {#295 ▼
#_config: array:14 [▼
"host" => null
"port" => null
"path" => null
"url" => null
"proxy" => null
"transport" => null
"persistent" => true
"timeout" => null
"connections" => []
"roundRobin" => false
"log" => false
"retryOnConflict" => 0
"servers" => array:2 [▼
  0 => array:2 [▼
    "host" => "x.x.x.233"
    "port" => "11103"
  ]
  1 => array:2 [▼
    "host" => "x.x.x.234"
    "port" => "11103"
  ]
]
"connectionStrategy" => "Simple"
]
#_callback: null
#_lastRequest: null
#_lastResponse: null
#_logger: null
#_connectionPool: ConnectionPool {#299 ▼
#_connections: array:2 [▼
  0 => Connection {#296 ▼
    #_params: array:4 [▼
      "config" => []
      "host" => "x.x.x.233"
      "port" => "11103"
      "enabled" => true
    ]
    #_rawParams: []
  }
  1 => Connection {#297 ▼
    #_params: array:4 [▼
      "config" => []
      "host" => "x.x.x.234"
      "port" => "11103"
      "enabled" => true
    ]
    #_rawParams: []
  }
]
#_strategy: Simple {#298}
#_callback: null

} }


Source: (StackOverflow)

elastica with terms and facets

I am faily new to elasticsearch and try to get along with elastica. I try to find out what Items are often togehter in a set of items when one of them is 2 and/or 7. So the index contains a lot of nested sets of items. The mutual items will be extracted with facets. But I don't seem to get the following query converted to elastica:

curl -X POST "http://localhost:9200/ratings/rating/_search?pretty=true" -d '
{
  "query": {
    "terms": {
      "bookid": [2, 7],
      "minimum_match" : 1
    }
  },
  "size": 0,
  "facets": {
    "bookid": {
      "terms": {
        "field": "bookid"
      }
    }
  }
}'

I was trying this:

        // Load index
        $index = $client->getIndex('ratings');
        $type = $index->getType('rating');


        // We want a Terms query.
        $query = new Elastica_Query_Terms();


        // Setting Terms
        $query->setTerms('bookid', $bookids);

        // Facets
        $facet = new Elastica_Facet_Query('matches');
        $facet->setField( 'bookid' )
            ->setSize(100);
        $facet->setQuery($query);

        $resultSet = $type->search($query);
        return $resultSet;

but no luck so far. How do I add the facet properties?


Source: (StackOverflow)

Elasticsearch index search with different limits

I'm using elasticsearch to serve people when they are using dropdown. So far, so good. Right now I'm trying to figure out is it possible to search from different types with different sorts/filters/limits.

So far I've found my way out with different filters per type and kinda with sorts. I've achieved different filters with boolean query, using shoulds and sorting is depending on scores. My goal is to show total 10 rows, where 8 of then should be products, 2 rows should be categories. For categories this limit should be max, if there is no categories to display, 10 products should be displayed. I've read the docs and I can see that size can be set to query body like sorting. But sorting has more flexibility. So yes, question is about the limit - is there a way to achieve it?


Source: (StackOverflow)

PHP Elastica filtered search returns no results when expected

I have created a small dataset (currently only 8 items) in an Elastic Search index.

The entries are structured as follows

{
    "value_a": "Foo",
    "value_b": "Bar",
    "value_c": "Baz"
}

In Elastic Search this then looks as follows (taken from querying my Elastic Search endpoint directly) :

{
    "_index": "foo_bar_bazindex",
    "_type": "foo_bar_baz",
    "_id": "4",
    "_score": 1,
    "_source": {
        "value_a": "Foo",
        "value_b": "Bar",
        "value_c": "Baz"
    }
}

The combinations of value a, b and c are unique.

I want to find the values of "value_c" by performing a bool filtered search with the values a and b.

In my code I have been trying this as follows:

    $filter = new \Elastica\Filter\Bool();
    $query = new \Elastica\Query();

    $aFilter = new \Elastica\Filter\Term(['value_a' => "Foo"]);
    $bFilter = new \Elastica\Filter\Term(['value_b' => "Bar"]);

    $filter->addMust($aFilter);
    $filter->addMust($bFilter);

    $query->setFilter($filter);

    $results = $this->bookingPeriodIndex->search($query)->getResults();

    var_dump($results);

However this returns no results (the var dump outputs an empty array) - also manually trying this query by posting this query directly to the server:

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "value_a": "Foo"
                            }
                        },
                        {
                            "term": {
                                "value_b": "Bar"
                            }
                        }
                    ]
                }
            }
        }
    }
}

This also yields no results - I would however expect it to return one result as I have the following entry:

{
    "value_a": "Foo",
    "value_b": "Bar",
    "value_c": "Baz"
}

Am I missing something? Could it be due to the small dataset? We use the same Bool Filter Elastica queries elsewhere in our codebase and these work as expected, however I cannot seem to get any data to return from this index.

Any help would be appreciated.


Source: (StackOverflow)

Elastica filter not working

I am testing out Elastica and Elastic Search. I am trying to add a filter to my query that only returns results by city location. It is returning empty. I've tried filter by username, and so on and it always returns empty, so it would seem my understanding isn't quite correct. Here's my code to analyse, map, then search with a filter

    $elasticaIndex = $elasticaClient->getIndex('users');
    // Create the index new
    $elasticaIndex->create(
            array(
        'analysis' => array(
            'analyzer' => array(
                'indexAnalyzer' => array(
                    'type' => 'custom',
                    'tokenizer' => 'standard',
                    'filter' => array('lowercase', 'lb_ngram')
                ),
                'searchAnalyzer' => array(
                    'type' => 'custom',
                    'tokenizer' => 'standard',
                    'filter' => array('standard', 'lowercase', 'lb_ngram')
                )
            ),
            'filter' => array(
                'lb_ngram' => array(
                    "max_gram" => 10,
                    "min_gram" => 1,
                    "type" => "nGram"
                )
            )
        )
            ), true
    );
    //Create a type
    $elasticaType = $elasticaIndex->getType('profile');


    // Set mapping
    $mapping->setProperties(array(
        //'id' => array('type' => 'integer', 'include_in_all' => FALSE),
        'firstName' => array('type' => 'string', 'include_in_all' => TRUE),
        'lastName' => array('type' => 'string', 'include_in_all' => TRUE),
        'username' => array('type' => 'string', 'include_in_all' => TRUE),
        'bio' => array('type' => 'string', 'include_in_all' => TRUE),
        'thumbnail' => array('type' => 'string', 'include_in_all' => FALSE),
        'location' => array('type' => 'string', 'include_in_all' => TRUE),
    ));

..... Then to search, I do the following

        $elasticaQueryString = new Elastica\Query\QueryString();

        //'And' or 'Or' default : 'Or'
        $elasticaQueryString->setDefaultOperator('AND');
        $elasticaQueryString->setQuery($term);

        // Create the actual search object with some data.
        $elasticaQuery = new Elastica\Query();
        $elasticaQuery->setQuery($elasticaQueryString);

To add a filter

            $elasticaFilterLocation = new \Elastica\Filter\Term();
            //search 'location' = $region;
            $elasticaFilterLocation->setTerm('location', $region);           
            $elasticaQuery->setFilter($elasticaFilterLocation);
            $elasticaResultSet = $elasticaIndex->search($elasticaQuery);                
            $elasticaResults = $elasticaResultSet->getResults();

If I comment out the filter, I do get the expected results. What am I missing? Does it have to do with the analyzer or mapping?


Source: (StackOverflow)

Search for values of nested facet in elasticsearch

This represents my mapping:

{
    "name": {"type": "string", "include_in_all": true},
    "properties": {
    "type": "nested",
    "properties": {
        "name": {"type": "string"},
        "value": {"type": "string"}
    }
}

How can I use facetted search for the value of 'properties.value'? Here is an example document:

{
    "name": "Testproduct",
    "properties": [{
        "name": "Color",
        "value": "Green"
    }, {
        "name": "Size",
        "value": "M"
    }]
}

I want to build a facetted list (only) by the "Color"-property. So the result should look like this:

Red: 7 times
Green: 5 times
Blue: 1 times

This is what I have tried so far:

{
    "size": 1000,
    "query": {
        "query_string": {
            "query": " ... ",
            "default_operator": "AND"
        }
    },
    "facets": {
        "resolution": {
            "nested": "properties",
            "facet_filter": {
                "term": {
                    "name": "Color"
                }
            },
            "terms_stats": {
                "key_field": "name",
                "value_field": "value"
            }
        }
    }
}

If I execute this search query, I get the following response:

FacetPhaseExecutionException[Facet [resolution]: value_field [value] isn't a number field, but a string];

I barely understand the response, but I have no idea what I have done wrong by concept.


Source: (StackOverflow)

Elastica with FOS : ElasticsearchIllegalArgumentException

In my symfony2 / doctrine 2 application, I get an error when running fos:elastica:populate :

[Elastica\Exception\Bulk\ResponseException]
Error in one or more bulk request actions:
index: /foodmeup/offer/4 caused MapperParsingException[failed to parse [res ponsibilities]]; nested: ElasticsearchIllegalArgumentException[unknown prop erty [0]];

My config is the following, I have set type: string to responsibilities which is actually a doctrine json array:

offer:
    mappings:
        name: { type: string, boost: 10}
        content: { type: string }
        responsibilities: { type: string }
        skills:
            type: "nested"
            properties:
                name: { type: string }
        contractType:
            type: "nested"
            properties:
                name: { type: string }
        position:
            type: "nested"
            properties:
                name: { type: string, boost: 10 }

And my entity mapping :

/**
 * @ORM\Column(type="json_array", nullable=false)
 */
protected $responsibilities = array();

What can I do to resolve this ? I have tried with "~" instead of "string" but the same problem arises and when I just remove the config line for responsibilities, it works well.

Thanks !


Source: (StackOverflow)

ElasticSearch - Searching with hyphens in name

I have a product catalog which I am indexing in ElasticSearch using the Elastica client. I am very new to ElasticSearch BTW.

There are products in my catalog which have 't-shirt' in their names. But, they won't appear in search results if I type 'tshirt'.

What can I do so that 't-shirt' can also pop-up in results?

I have followed this tutorial and implemented the following for indexes:

'analysis' => array(
    'analyzer' => array(
        'indexAnalyzer' => array(
            'type' => 'custom',
            'tokenizer' => 'whitespace',
            'filter' => array('lowercase', 'mySnowball')
        ),
        'searchAnalyzer' => array(
            'type' => 'custom',
            'tokenizer' => 'whitespace',
            'filter' => array('lowercase', 'mySnowball')
        )
    ),
    'filter' => array(
        'mySnowball' => array(
            'type' => 'snowball',
            'language' => 'English'
        )
    )
)

Source: (StackOverflow)